Kevin Reid (kpreid) wrote,

A note on JavaScript method call semantics

So. In JavaScript, an “object” is a set of “properties”: associations from strings to values. A method is just a property whose value is a function. Functions are called like “foo()”, properties are accessed like “bar.foo”, and methods are called like “bar.foo()”. Looks straightforward enough, right?

Now, how does a method access the state of its object? Without inheritance, you could just have the method functions of a given object all close over some variables; but JavaScript does have prototype inheritance, so the necessary access is provided by binding the variable “this”, in the method body, to the object the method was invoked on.

And when does this happen? When you use the method call syntax. bar.foo() is not the same as

var m = bar.foo;
m();

(It is the same as m.call(bar)call is a method on function objects which invokes them with this bound — but that's beside the point.)

So, the syntax is non-compositional.

Not only that, but it is enthusiastically so: bar.foo() is the same as (bar.foo)() — the parentheses do not break up the method call construct!

Tags: javascript, programming, web
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded  

  • 6 comments

laurent_atl

January 3 2009, 20:58:07 UTC 4 years ago

how javascript specific is it?

this is true for python, and I thin R and ruby as well

kpreid

January 3 2009, 21:30:54 UTC 4 years ago

Re: how javascript specific is it?

Python, at least, binds at property access time, and there is no significance to the combination of . and ():
>>> m = {1:2}.values
>>> m()
[2]

I have no knowledge of Ruby.

dougo

January 4 2009, 08:16:34 UTC 4 years ago

So what happens to "this" when you call m() as a function? Undefined, and an error when you dereference it?

kpreid

January 4 2009, 15:13:15 UTC 4 years ago

I don't know what the general rule is. In a test I just tried, I got the "window" object (in general, the window also serves as the "global namespace" for JS code).

Anonymous

January 4 2009, 16:42:54 UTC 4 years ago

You are talking about method closures.
http://web.2point1.com/2008/11/24/javascript-closures/ (http://web.2point1.com/2008/11/24/javascript-closures/)

This has been a legacy of ECMAScript for years. ActionScript 2 programmers got used to using a method known as Delegate.create - But now AS3 has fixed this, and so has JASPA (http://jaspa.org.uk/) - http://jaspa.org.uk/wiki/Method_closure (http://jaspa.org.uk/wiki/Method_closure)

Anonymous

January 29 2009, 11:59:55 UTC 4 years ago

Tuesday Night

Hi Kevin, It was great meeting and talking with you the other night at the UAUG meeting. I used to create webpages in raw html and then when java came along, it was too complex for me to devote the time to it though I gave it a shot for a while, but I am hoping to learn more as time goes by, so I will be sure to check in on your blog. Any recommendations for good resources to learn would be much appreciated.