this-keyword in anonymous functions

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

    this-keyword in anonymous functions

    Hi,

    given this code:

    function test() {
    ({
    call: function(fn) {
    fn();
    },

    test: function() {

    /* Position A */
    Logger.log(this );
    this.call(funct ion() {

    /* Position B */
    Logger.log(this );
    });

    }
    }).test();
    }

    Obviously, "this" at Position A points to a different object, than at
    Position B. At Position B it's the DOMWindow-Object ( at least in
    Safari ) or a the global Context in a standalone SpiderMonkey-Engine.

    This makes writing code, that deals alot with Prototype-Library-like
    iterators pretty error-prone. Up to now I handle this by defining a
    local variable "self", and let the closure-mechanics deal with the
    rest, but I hate the way that looks ("this" here, "self" there ), and
    are really wondering, if there is a better way/best practice to access
    object-properties in an anonymous function.

    Greets
    Sebastian

  • RobG

    #2
    Re: this-keyword in anonymous functions

    On Apr 11, 1:35 am, Sebastian Morawietz
    <sebastian.mora wi...@googlemai l.comwrote:
    Hi,
    >
    given this code:
    >
    function test() {
    ({
    >
    call: function(fn) {
    fn();
    },
    >
    test: function() {
    >
    /* Position A */
    Logger.log(this );
    this.call(funct ion() {
    >
    /* Position B */
    Logger.log(this );
    });
    }
    }).test();
    }
    >
    Obviously, "this" at Position A points to a different object, than at
    Position B. At Position B it's the DOMWindow-Object ( at least in
    Safari ) or a the global Context in a standalone SpiderMonkey-Engine.
    >
    This makes writing code, that deals alot with Prototype-Library-like
    iterators pretty error-prone. Up to now I handle this by defining a
    local variable "self", and let the closure-mechanics deal with the
    rest, but I hate the way that looks ("this" here, "self" there ), and
    are really wondering, if there is a better way/best practice to access
    object-properties in an anonymous function.
    In this case, object properties of an anonymous object - the obvious
    solution is don't use an anonymous object where it would be much
    easier to use one with a name. :-)

    Dunno about "best practice", but a solution (not necessarily best, or
    perhaps even good) is:

    function test() {
    ({

    _call: function(that, fn) {
    fn.call(that);
    },

    test: function() {

    /* Position A */
    console.log(thi s);

    // Just for the example
    var self = this;

    this._call(this , function() {

    /* Position B */
    console.log(thi s == self); // true
    });
    }
    }).test();
    }


    --
    Rob

    Comment

    • RobG

      #3
      Re: this-keyword in anonymous functions

      On Apr 11, 4:35 pm, Sebastian Morawietz
      <sebastian.mora wi...@googlemai l.comwrote:
      On 11 Apr., 05:22, RobG <rg...@iinet.ne t.auwrote:
      >
      >
      >
      On Apr 11, 1:35 am, Sebastian Morawietz
      >
      Dunno about "best practice", but a solution (not necessarily best, or
      perhaps even good) is:
      >
      function test() {
      ({
      >
      _call: function(that, fn) {
      fn.call(that);
      },
      >
      test: function() {
      >
      /* Position A */
      console.log(thi s);
      >
      // Just for the example
      var self = this;
      >
      this._call(this , function() {
      >
      /* Position B */
      console.log(thi s == self); // true
      });
      }
      }).test();
      >
      }
      >
      Thanks for the answer. A closer look to Prototype's sources revealed
      to me, that this is basically, how they do it with their iterators
      too.
      Do they? Dang, that's almost certain to mean it's not in the "best
      practice" list of anyone here. :-)

      One way to get anonymous iterators to call themselves is with
      arguments.calle e, but that doesn't get the "parent" object, which is
      what you seem to be after.


      --
      Rob

      Comment

      Working...