Leopard won’t pounce until October Calling and comparing the UI for GOOG-411
Apr 12

var self = this;

JavaScript, Tech Add comments
$.fn.delayHide(time, callbackArgument) {
var self = this;
setTimeout(function() {
callbackArgument.call(self);
}, time);
}

I find that I have to use the var self = this; pattern far too often in JavaScript.

The darn magic scoping of this really gets ya.

I also had someone ask me why foo never ended up containing something:

var foo = bar(function() {
....
return something;
});

It made me more worried about adding closures to Java :)

4 Responses to “var self = this;”

  1. Stephen Colebourne Says:

    Not all closures proposals are equal. In this example:

    public void process() {
    foo = bar( { =>
    ….
    return something;
    });
    }

    The return will return from process() in BGGA closures as you hint at, and foo will not be set.

    But in FCM closures – http://docs.google.com/Doc?id=ddhp95vd_0f7mcns – the return will return back to the invoker of the closure, as with inner classes, and foo will be set.

    Which semantic do you prefer?

  2. carmen Says:

    crockford calls it a ‘design error’ i think. i mean technically an inner anonymous function _is_ a new object, so it should get a new ‘this’, no? id definitely like it to inherit via lexical scoping as well, but what can you do.

    are they fixing this bug in 2.0?

  3. replicahandbags Says:

    thanks for posting this.

  4. replica handbags Says:

    thanks!

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: Type in the word 'cricket'

Loading...