this keyword and closures

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff

    this keyword and closures

    I have a question about closures. Take for example the addBehavior
    method below from the Low Pro library

    Event.addBehavi or = function(rules) {
    var ab = this.addBehavio r;
    Object.extend(a b.rules, rules);

    if (!ab.responderA pplied) {
    Ajax.Responders .register({
    onComplete : function() {
    if (Event.addBehav ior.reassignAft erAjax)
    setTimeout(func tion() { ab.reload() }, 10);
    }
    });
    ab.responderApp lied = true;
    }

    if (ab.autoTrigger ) {
    this.onReady(ab .load.bind(ab, rules));
    }

    };

    var ab is initialized to this.addBehavio r. It is then extended using
    Object.extend(a b.rules.rules). However, in the Ajax.Responders
    onComplete function, addBehavior is referenced using
    Event.addBehavi or.reassignAfte rAjax instead of ab.reassignAfte rAjax.
    Then, immediately after, ab.reload is referenced. Why is this? Instead
    of ab.rules, why wasn't
    Object.extend(E vent.addBehavio r.rules, rules), or even
    Object.extend(t his.addBehavior .rules, rules)? Does it have to do with
    closures?
  • Joost Diepenmaat

    #2
    Re: this keyword and closures

    Jeff <jknight12882@g mail.comwrites:
    I have a question about closures. Take for example the addBehavior
    method below from the Low Pro library
    Never heard of that library, but ok.
    Event.addBehavi or = function(rules) {
    var ab = this.addBehavio r;
    Object.extend(a b.rules, rules);
    >
    if (!ab.responderA pplied) {
    Ajax.Responders .register({
    onComplete : function() {
    if (Event.addBehav ior.reassignAft erAjax)
    setTimeout(func tion() { ab.reload() }, 10);
    }
    });
    ab.responderApp lied = true;
    }
    >
    if (ab.autoTrigger ) {
    this.onReady(ab .load.bind(ab, rules));
    }
    >
    };
    >
    var ab is initialized to this.addBehavio r. It is then extended using
    Object.extend(a b.rules.rules).
    yes.
    However, in the Ajax.Responders onComplete function, addBehavior is
    referenced using Event.addBehavi or.reassignAfte rAjax instead of
    ab.reassignAfte rAjax.
    It's not clear from the code that Event.addBehavi our.reassignAft erAjax
    === ab.reassignAfte rAjax, though it probably is.
    Then, immediately after, ab.reload is referenced. Why is this? Instead
    of ab.rules, why wasn't
    Object.extend(E vent.addBehavio r.rules, rules), or even
    because it's shorter?
    Object.extend(t his.addBehavior .rules, rules)? Does it have to do with
    closures?
    since you can't close over this, yes, it has to do with closures. this
    is NOT a variable.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

    Comment

    Working...