473,668 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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).t oUpperCase() + str.substr(1);
}

var Attr = {
makeGet: function(constr uctor, prop) {
constructor.pro totype['get'+capitaliz e(prop)] = function() {
return this[prop];
}
}
};

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

var ted = new Person('ted', 'nugent')
alert(ted.getFi rstName()); // --'ted'

So if "this" is lexically bound will ted.getFirstNam e() look for a
Attr.makeGet.fi rstName property?

I'm worried. Any ideas out there?

Thanks,
Peter

May 31 '07 #1
1 1345
On May 31, 3:05 pm, Peter Michaux <petermich...@g mail.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.getFirstNam e, 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
11152
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 type=button onclick=" function myobject(a) { function c() { return this; } var prop1=a; this.youpla = c(); } var i = new myobject();
10
2142
by: solosnake | last post by:
Whilst browsing Flipcode I noticed this code: class CConsole: public I_TextOutput { public: ... void Release() { delete this; } ... };
4
2729
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 data from one table that has 1 column. Each of the three tables will each up with 1 record with 255 columns. This is the code I wrote (in brief) to demonstare whate I want to do. Is there an easier way? I don't really want to type 500 lines of code...
7
4801
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 AddDomainUserToLocalGroup(string computerName, string groupName, string domainName, string userName) { Hashtable htRet = new Hashtable(); IADsContainer groupComputer = (IADsContainer)Win32.GetObject("WinNT://" + computerName + ",computer");
5
2468
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); When attempting to compile this code, a message is returned that states that the "this" keyword is not available in this context.
7
2233
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 me. So, I'm here for an answer (more of a confirmation), hopefully. First let me say that I know people keep saying; it doesn't work because the member "is a private". I believe there's more to it than just simply that... Theory: You inherit,...
9
1489
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
3073
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 find it a bit annoying) TIA, JS
1
1764
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
508
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
8371
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8889
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8572
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2782
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.