Are they really going to lexically bind "this" for inner functions and break my code?

Discussion in 'Javascript' started by Peter Michaux, May 31, 2007.

  1. Hi,

    I saw Brendan Eich in an online conference video say that in
    JavaScript 2 that they will lexically bind the "this" keyword of inner
    functions. He said that currently the execution-time resolution of
    "this" is considered a bug by some. I often take advantage of the fact
    that "this" is resolved during execution. Is JavaScript 2 going to
    break all my (our?) code?

    For example, one time, to save typing and download time, I automated
    the creation of simple getter functions

    function capitalize(str) {
    return str.charAt(0).toUpperCase() + str.substr(1);
    }

    var Attr = {
    makeGet: function(constructor, prop) {
    constructor.prototype['get'+capitalize(prop)] = function() {
    return this[prop];
    }
    }
    };

    function Person(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    }
    Attr.makeGet(Person, 'firstName', 'lastName');

    var ted = new Person('ted', 'nugent')
    alert(ted.getFirstName()); // --> 'ted'

    So if "this" is lexically bound will ted.getFirstName() look for a
    Attr.makeGet.firstName property?

    I'm worried. Any ideas out there?

    Thanks,
    Peter
    Peter Michaux, May 31, 2007
    #1
    1. Advertising

  2. On May 31, 3:05 pm, Peter Michaux <> wrote:
    > Hi,
    >
    > I saw Brendan Eich in an online conference video say that in
    > JavaScript 2 that they will lexically bind the "this" keyword of inner
    > functions. He said that currently the execution-time resolution of
    > "this" is considered a bug by some. I often take advantage of the fact
    > that "this" is resolved during execution. Is JavaScript 2 going to
    > break all my (our?) code?


    Brendan was kind enough to send me a reply. Hopefully he doesn't mind
    me pasting his email. here it is...

    You are calling the computed "get" function via an explicit object
    reference, ted.getFirstName, so |this| must bind to ted in that call.
    This can't change, it would break the world of course.

    What is changing is the |this| binding rule for calls from outer to
    inner functions by their defined names (not via object references,
    via lexical references):

    function f(x) {
    function g(y) {
    this.z = x * y;
    }
    return g(4);
    }
    o = {m: f};
    o.m(3); // does this set o.z to 12, or global z to 12?

    Currently this script sets global z (|this.z| evaluated outside of
    any function) to 12. But that's not what people want, because the
    call g(4) inside f is expected to receive the same |this| value that
    f received. Instead, due entire to a hairy optimization concern in
    ECMA-262 Edition 3, the |this| binding for such a call to g first
    censors the Activation object to which |this| would normally bind
    (because it's the base object of the reference to the name g) by
    replacing it with null; and then replaces null with "the global
    object".

    So, don't worry!

    Hope this helps,

    /be
    Peter Michaux, Jun 1, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Sunshine

    Lexically scoped variables

    Eric Sunshine, Dec 19, 2003, in forum: Ruby
    Replies:
    6
    Views:
    128
    Robert Klemme
    Dec 26, 2003
  2. Gavin Sherlock
    Replies:
    1
    Views:
    91
    Uri Guttman
    Dec 10, 2003
  3. Alex Hart
    Replies:
    0
    Views:
    76
    Alex Hart
    Jan 24, 2005
  4. Alex Hart
    Replies:
    1
    Views:
    93
    Anno Siegel
    Jan 24, 2005
  5. Derek Basch
    Replies:
    6
    Views:
    160
    Michele Dondi
    May 29, 2006
Loading...

Share This Page