wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes javascript call() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "javascript call()" Watch "javascript call()" New topic
Author

javascript call()

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

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.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

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

Joined: Jan 10, 2002
Posts: 60748
    
  65

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

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

Joined: Jan 10, 2002
Posts: 60748
    
  65

Eeesh. Wonder what else was wrong...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: javascript call()
 
Similar Threads
MVP approach
Handling threading issues when listening to model events with ModelViewPresenter
Abstract Class vs. Interface
How Much Is Java Really Being Used On The Web?
Handling EDT issues when listening to model events with Model View Presenter