0

I'm studying for an HTML certification an ran across a syntax like this:

 foo.click() += some_other_function;   // supposedly to invoke another function
                                       // when the foo HTML element is clicked.

I can't find any information on this. Can this be done in JavaScript?

6
  • 1
    So, you want to learn how to bind event handlers? See quirksmode.org/js/introevents.html . += is for summation and string concatenation. Commented May 18, 2016 at 19:18
  • 1
    Looks like C#-ish syntax for adding event handlers to event collections, but this can't be done natively in JS. Commented May 18, 2016 at 19:18
  • 2
    I... don't think thats valid javascript Commented May 18, 2016 at 19:18
  • Where exactly have you seen that syntax? Commented May 18, 2016 at 19:19
  • This is a syntax error. This would be equivalent to foo.click() = foo.click() + some_other_function, and you (obviously) can't assign a value to foo.click(). Commented May 18, 2016 at 19:19

1 Answer 1

2

In general, the x += y operation in javascript is just shorthand for x = x + y

Using this with a function call, makes little sense. There is probably some contrived way to make the code you posted not error, but its hard to imagine where it could do something meaningful.

So:

Can this be done in JavaScript?

No, not really.

Sign up to request clarification or add additional context in comments.

1 Comment

Not the fact that it will return NaN because you're mixing undefined with some_other_function. And it is also illegal to assign value to a function in JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.