473,322 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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

May 31 '07 #1
1 1319
On May 31, 3:05 pm, Peter Michaux <petermich...@gmail.comwrote:
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

Jun 1 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Armel Asselin | last post by:
Hello, I'm working on a Javascript interpreter; when I execute this code I cannot figure out why the inner "c" function returns the input widget as "this"... could someone tell ?? <input...
10
by: solosnake | last post by:
Whilst browsing Flipcode I noticed this code: class CConsole: public I_TextOutput { public: ... void Release() { delete this; } ... };
4
by: Kathy | last post by:
Hi All, I am using Access 2000. I would like to streamline this code by using a variable for the column name. I have three tables with 255 columns each that I would like to populate with the...
7
by: Peter Steele | last post by:
I have code to add a domain user to a local group but I'm not sure if it will work with NT domains or whether it will only work with Active Directory based systems. Here's the code: public void...
5
by: ChrisB | last post by:
Hello: An object that is a field in another object has a constructor that requires a reference to the containing object: // object fields ChildObject childObject = new ChildObject(this); ...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
9
by: bhavik.patel | last post by:
Hi I have a rather simple question: I have following class definitions: class Base { public: virtual void display ()
11
by: Joseph S. | last post by:
Hi all, how do I avoid typing the keyword "$this->" every time I need to reference a member of a class inside the class? (coming from a world of cozy auto-complete enabled Java / .Net IDEs I...
1
by: piotr.korzeniewski | last post by:
I'm really confused with this one, first please take a look at code below: function Class(obj) { if(!obj) obj = new Object(); obj.Extend = function(obj) { this.__parent__ = new Object();
30
by: Bill Reid | last post by:
#define MAX_VALUES 64 typedef struct { unsigned value_1; double value_2; double value_3; double value_4; } VALUES; typedef struct {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.