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

P

Peter Michaux

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
 
P

Peter Michaux

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Qooxdoo--garbage? 8

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top