473,657 Members | 2,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

this confusion

Simplified example code which can be copy/pasted and will exhibit the
problem.

var ob={id:2};
ob.alertid=func tion(){alert("i d="+this.id); }
ob.funcs=[ob.alertid];
ob.testalert=fu nction(){
this.alertid(); //this works
this.funcs[0](); //this doesn't
}

ob.testalert();

I have an object with an id property. The alertid function alerts that
id property if it's called directly, but not if it's called via an array
of function pointers. I don't understand the difference. Why is 'this'
different in those two cases. It's the same object method isn't it.

The output is:

id=2
id=undefined
Aug 8 '07 #1
10 1210
On Aug 9, 7:23 am, Stevo <ple...@spam-me.comwrote:
Simplified example code which can be copy/pasted and will exhibit the
problem.

var ob={id:2};
ob.alertid=func tion(){alert("i d="+this.id); }
ob.funcs=[ob.alertid];
ob.testalert=fu nction(){
this.alertid(); //this works
this.funcs[0](); //this doesn't

}

ob.testalert();

I have an object with an id property. The alertid function alerts that
id property if it's called directly, but not if it's called via an array
of function pointers. I don't understand the difference. Why is 'this'
different in those two cases. It's the same object method isn't it.
I think the best post I've seen about the this keyword is the one here
by Mike Winter:

<URL:
http://groups.google.com.au/group/co...26cd885e01c292
>

The first part is about closures, the second part on this is
excellent.
--
Rob

Aug 8 '07 #2
On Aug 8, 5:23 pm, Stevo <ple...@spam-me.comwrote:
Simplified example code which can be copy/pasted and will exhibit the
problem.

var ob={id:2};
ob.alertid=func tion(){alert("i d="+this.id); }
ob.funcs=[ob.alertid];
ob.testalert=fu nction(){
this.alertid(); //this works
Right. Because you called a method of the ob object. In this case,
"this" is ob, which has an id property.
this.funcs[0](); //this doesn't
funcs[0] holds a reference to an anonymous function. The fact that ob
has a property (alertid method) that references it as well is
meaningless. In this case, "this" refers to the anonymous function,
which has no id property.
Aug 9 '07 #3
David Mark wrote:
In this case, "this" refers to the anonymous function,
which has no id property.
Thanks David, Randy, Thomas and Rob. You all said the same thing
basically, this differs depending on how it's called. Excellent link
Rob, that's one to bookmark.

I've altered the alertid function to add 'that' and it now works.

var ob={id:2};
ob.alertid=func tion(){var that=ob;alert(" id="+that.id); }
ob.funcs=[ob.alertid];
ob.testalert=fu nction(){
this.alertid();
this.funcs[0]();
}
ob.testalert();

That has the name ob hard-coded in the alertid function though. I wonder
if it's possible to create 'that' without knowing the name of the
object? Take the following example with ??? where I don't know what to set:

var ob_a={id:"a"};
var ob_b={id:"b"};
var alertid=functio n(){var that=???;alert( "id="+that.id); }
ob_a.alertid=al ertid;
ob_b.alertid=al ertid;

I'm guessing that there's no way to define 'that' which would work for
both objects there, and I'd have to define an anonymous function for
each object, with hard-coded references to ob_a and ob_b in them like so:

var ob_a={id:"a"};
var ob_b={id:"b"};
ob_a.alertid=fu nction(){var that=ob_a;alert ("id="+that.id) ;};
ob_b.alertid=fu nction(){var that=ob_b;alert ("id="+that.id) ;}
Aug 9 '07 #4
Stevo wrote:
[...] I wonder if it's possible to create 'that' without knowing the name of the
object? Take the following example with ??? where I don't know what to set:

var ob_a={id:"a"};
var ob_b={id:"b"};
var alertid=functio n(){var that=???;alert( "id="+that.id); }
ob_a.alertid=al ertid;
ob_b.alertid=al ertid;

I'm guessing that there's no way to define 'that' which would work for
both objects there, and I'd have to define an anonymous function for
each object, with hard-coded references to ob_a and ob_b in them like so:

var ob_a={id:"a"};
var ob_b={id:"b"};
ob_a.alertid=fu nction(){var that=ob_a;alert ("id="+that.id) ;};
ob_b.alertid=fu nction(){var that=ob_b;alert ("id="+that.id) ;}
var alertid = function(that){ alert("id=" + that.id); };
// ...
ob_a.alertid = function() { alertid(this); };
ob_b.alertid = function() { alertid(this); };

or

var alertid = function(){ alert("id=" + this.id); }
// ...

and then either

ob_a.alertid = function() { alertid.call(th is); };
ob_b.alertid = function() { alertid.call(th is); };

or

ob_a.alertid = function() { alertid.apply(t his); };
ob_b.alertid = function() { alertid.apply(t his); };
HTH

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8************ *******@news.de mon.co.uk>
Aug 9 '07 #5
Stevo wrote:
[...] I wonder if it's possible to create 'that' without knowing the name of the
object? Take the following example with ??? where I don't know what to set:

var ob_a={id:"a"};
var ob_b={id:"b"};
var alertid=functio n(){var that=???;alert( "id="+that.id); }
ob_a.alertid=al ertid;
ob_b.alertid=al ertid;

I'm guessing that there's no way to define 'that' which would work for
both objects there, and I'd have to define an anonymous function for
each object, with hard-coded references to ob_a and ob_b in them like so:

var ob_a={id:"a"};
var ob_b={id:"b"};
ob_a.alertid=fu nction(){var that=ob_a;alert ("id="+that.id) ;};
ob_b.alertid=fu nction(){var that=ob_b;alert ("id="+that.id) ;}
var alertid = function(that){ alert("id=" + that.id); };
// ...
ob_a.alertid = function() { alertid(this); };
ob_b.alertid = function() { alertid(this); };

or

var alertid = function(){ alert("id=" + this.id); }
// ...

and then either

ob_a.alertid = function() { alertid.call(th is); };
ob_b.alertid = function() { alertid.call(th is); };

or

ob_a.alertid = function() { alertid.apply(t his); };
ob_b.alertid = function() { alertid.apply(t his); };
HTH

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8************ *******@news.de mon.co.uk>
Aug 9 '07 #6
Stevo wrote:
[...] I wonder if it's possible to create 'that' without knowing the name of the
object? Take the following example with ??? where I don't know what to set:

var ob_a={id:"a"};
var ob_b={id:"b"};
var alertid=functio n(){var that=???;alert( "id="+that.id); }
ob_a.alertid=al ertid;
ob_b.alertid=al ertid;

I'm guessing that there's no way to define 'that' which would work for
both objects there, and I'd have to define an anonymous function for
each object, with hard-coded references to ob_a and ob_b in them like so:

var ob_a={id:"a"};
var ob_b={id:"b"};
ob_a.alertid=fu nction(){var that=ob_a;alert ("id="+that.id) ;};
ob_b.alertid=fu nction(){var that=ob_b;alert ("id="+that.id) ;}
var alertid = function(that){ alert("id=" + that.id); };
// ...
ob_a.alertid = function() { alertid(this); };
ob_b.alertid = function() { alertid(this); };

or

var alertid = function(){ alert("id=" + this.id); }
// ...

and then either

ob_a.alertid = function() { alertid.call(th is); };
ob_b.alertid = function() { alertid.call(th is); };

or

ob_a.alertid = function() { alertid.apply(t his); };
ob_b.alertid = function() { alertid.apply(t his); };
HTH

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8************ *******@news.de mon.co.uk>
Aug 9 '07 #7
Stevo wrote:
[...] I wonder if it's possible to create 'that' without knowing the name of the
object? Take the following example with ??? where I don't know what to set:

var ob_a={id:"a"};
var ob_b={id:"b"};
var alertid=functio n(){var that=???;alert( "id="+that.id); }
ob_a.alertid=al ertid;
ob_b.alertid=al ertid;

I'm guessing that there's no way to define 'that' which would work for
both objects there, and I'd have to define an anonymous function for
each object, with hard-coded references to ob_a and ob_b in them like so:

var ob_a={id:"a"};
var ob_b={id:"b"};
ob_a.alertid=fu nction(){var that=ob_a;alert ("id="+that.id) ;};
ob_b.alertid=fu nction(){var that=ob_b;alert ("id="+that.id) ;}
var alertid = function(that){ alert("id=" + that.id); };
// ...
ob_a.alertid = function() { alertid(this); };
ob_b.alertid = function() { alertid(this); };

or

var alertid = function(){ alert("id=" + this.id); }
// ...

and then either

ob_a.alertid = function() { alertid.call(th is); };
ob_b.alertid = function() { alertid.call(th is); };

or

ob_a.alertid = function() { alertid.apply(t his); };
ob_b.alertid = function() { alertid.apply(t his); };
HTH

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8************ *******@news.de mon.co.uk>
Aug 9 '07 #8
Stevo wrote:
[...] I wonder if it's possible to create 'that' without knowing the name of the
object? Take the following example with ??? where I don't know what to set:

var ob_a={id:"a"};
var ob_b={id:"b"};
var alertid=functio n(){var that=???;alert( "id="+that.id); }
ob_a.alertid=al ertid;
ob_b.alertid=al ertid;

I'm guessing that there's no way to define 'that' which would work for
both objects there, and I'd have to define an anonymous function for
each object, with hard-coded references to ob_a and ob_b in them like so:

var ob_a={id:"a"};
var ob_b={id:"b"};
ob_a.alertid=fu nction(){var that=ob_a;alert ("id="+that.id) ;};
ob_b.alertid=fu nction(){var that=ob_b;alert ("id="+that.id) ;}
var alertid = function(that){ alert("id=" + that.id); };
// ...
ob_a.alertid = function() { alertid(this); };
ob_b.alertid = function() { alertid(this); };

or

var alertid = function(){ alert("id=" + this.id); }
// ...

and then either

ob_a.alertid = function() { alertid.call(th is); };
ob_b.alertid = function() { alertid.call(th is); };

or

ob_a.alertid = function() { alertid.apply(t his); };
ob_b.alertid = function() { alertid.apply(t his); };
HTH

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8************ *******@news.de mon.co.uk>
Aug 9 '07 #9
On Aug 16, 3:50 pm, ron.h.h...@gmai l.com wrote:
<snip>
In other words, a function invoked as f(...) will have a
"this" value of the global object,
function f(){
var x = (this+'');
}

with(
{
f:f,
toString:functi on(){alert('I am not the global object');}
}
){
f(); // alerts: I am not the global object
// So the - this - value when - f() - is
// executed is _not_ the global object.
}

You appear to have failed to take into account the role of variable
objects and the scope chain in determining the - this - value.

<snip>
So the second problem with Mike's post is that it over-complicates
the description of what one should expect of the "this" value to
be by involving the variable object, scope chain and the "new"
operator[1] in the description.
<snip>

Aug 16 '07 #10

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

Similar topics

1
1991
by: Mathias Mamsch | last post by:
Hi, I have some confusion concerning the weakref module. I am trying to save a weak reference to a bound member function of a class instance for using it as a callback function. But I always get dead references, when I try to create a reference to a bound member function. It seems as if an instance of a class owns no reference to its memberfunctions, so the the reference count is always zero. How can I come behind that?
10
1939
by: Kamilche | last post by:
I'm trying to pack two characters into a single byte, and the shifting in Python has me confused. Essentially, it should be possible to use a 'packed string' format in Python, where as long as the characters you're sending are in the ASCII range 0 to 127, two will fit in a byte. Here's the code. Can you tell what I'm doing wrong? |import types
3
1854
by: Simon Johnson | last post by:
I'm trying to create an RSS 2.0 compliant feed but I'm meeting some confusion over the pubDate element. It clearly states next to the pubDate that the date should be formatted in compliance with the specification given in RFC 822. They even give the following date as an example: "Sat, 07 Sep 2002 00:00:01 GMT" However RFC 822 clearly states that the year portion of the date
8
1435
by: Martin Gieseking | last post by:
Hello I've the following piece of code that compiles fine: typedef int (*(*T)); T *t = new T; Now I would like to avoid the typedef but don't really know how to do this. Is it possible at all?
8
3318
by: gg | last post by:
I am confused regarding what the line in the following function does. It seems to work ok. It seems to be creating a new T object using the pointer to an existing T object. But which function is it calling to create the new T object? void f ( auto_ptr < T > & aPT ) { auto_ptr < T > newPT ( new T ( aPT.get ( ) ) ); }
4
2537
by: JMCN | last post by:
object invalid or no longer set - confusion of the recordset in access 2003. i am currently converting from access 97 to access 2003. majority of the codes converted over perfectly fine, though my recordset did not. i read from pasts posts that some references will not convert. so i tried to change the dao 3.51 reference to dao 3.6 in access 97. then i imported all of the objects into an access 2003. it did not work. so my second...
4
1508
by: FDude | last post by:
Hello folks: I am just starting to delve into the DUMPS from the ILDASM utility. Can I do anything useful with the outputted IL code? Can someone give me some references to working with .IL files? Thanks
5
1286
by: Peter Oliphant | last post by:
I was thinking it might be a good idea to split this newsgroup into different newsgroups, depending on the version of VS C++.NET being discussed. Thus, there would be 2002, 2003, and 2005 newsgroups instead of just one for all. This would at least end any confusion if someone asks a question and doesn't state which version they are using (which of course can change the answer to such questions). Just a thought...
14
1405
by: bbawa1 | last post by:
It says invalid expression term && protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if ((Convert.ToInt32(e.Row.Cells.Text)>=15))&&(Convert.ToInt32(e.Row.Cells.Text)>=17); {
40
1983
by: Dave | last post by:
Hello, I'm teaching myself C by working my way through Steve Summit's tutorial (http://www.eskimo.com/~scs/cclass/cclass.html). In one of the questions (assignment 6, exercise 7), you have to write a function to read lines of arbitrary length from the command line, using malloc() and realloc() to allocate the necessary memory to hold the lines. I came up with this: char *getline(char *line) {
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8825
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
8732
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
8503
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,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6163
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
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.