• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javascript call()

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34229
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At today's NY Java Sig, the topic was "JavaScript for Java developers." Another attendee and I both attempted to ask a question. I'm curious both the answer to the question and if the question is unclear.

Person 1:
func = function()....;
How is func.call(obj, param) different than func(obj, param)?

The presenter said they aren't if everything is defined in the same file. I would have thought they would be different because the former would be called on obj and the later pass obj as a parameter.

This inspired my question:
In some scripting languages func(obj, param) and obj.func(param) are equivalent. Is JavaScript one of those languages?

The presenter didn't understand my question. Granted it is harder to follow verbally than online.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
How is func.call(obj, param) different than func(obj, param)?

The presenter said they aren't if everything is defined in the same file.

The presenter is wrong.

func.call() sets the first argument as the function context of the function invocation (what's pointed to by this) and the rest of the arguments get bound to parameters. It isn't the same as calling the function directly, and whether they are in the same file or not is completely irrelevant.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
This inspired my question:
In some scripting languages func(obj, param) and obj.func(param) are equivalent. Is JavaScript one of those languages?

Again, the difference is in the function context. When a function is called in the global context, as in func(whatever) the function context is the global context. For browsers, that's window.

When called as a method of an object, as in obj.func(whatever), the "owning" object becomes the function context.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I go into excruciating detail on all this in chapter 3 of Secrets of the JavaScript Ninja.

Understanding JavaScript as a functional language is the key to writing good JavaScript. Those coming from the OO world (like us JavaHeads) can sometimes have a hard time because we take too much OO baggage with us.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34229
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear. I had a feeling that was going to be the case because one of the slides had and a comment that means the function gets called in the future. And on the *same slide* was a javascript debugger/warning that the quotes are missing around the function so it doesn't do what is expected. Even I know the function gets executed immediately in this case.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eeesh. Wonder what else was wrong...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic