473,326 Members | 2,192 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,326 software developers and data experts.

this-keyword in anonymous functions

Hi,

given this code:

function test() {
({
call: function(fn) {
fn();
},

test: function() {

/* Position A */
Logger.log(this);
this.call(function() {

/* Position B */
Logger.log(this);
});

}
}).test();
}

Obviously, "this" at Position A points to a different object, than at
Position B. At Position B it's the DOMWindow-Object ( at least in
Safari ) or a the global Context in a standalone SpiderMonkey-Engine.

This makes writing code, that deals alot with Prototype-Library-like
iterators pretty error-prone. Up to now I handle this by defining a
local variable "self", and let the closure-mechanics deal with the
rest, but I hate the way that looks ("this" here, "self" there ), and
are really wondering, if there is a better way/best practice to access
object-properties in an anonymous function.

Greets
Sebastian

Apr 10 '08 #1
2 1735
On Apr 11, 1:35 am, Sebastian Morawietz
<sebastian.morawi...@googlemail.comwrote:
Hi,

given this code:

function test() {
({

call: function(fn) {
fn();
},

test: function() {

/* Position A */
Logger.log(this);
this.call(function() {

/* Position B */
Logger.log(this);
});
}
}).test();
}

Obviously, "this" at Position A points to a different object, than at
Position B. At Position B it's the DOMWindow-Object ( at least in
Safari ) or a the global Context in a standalone SpiderMonkey-Engine.

This makes writing code, that deals alot with Prototype-Library-like
iterators pretty error-prone. Up to now I handle this by defining a
local variable "self", and let the closure-mechanics deal with the
rest, but I hate the way that looks ("this" here, "self" there ), and
are really wondering, if there is a better way/best practice to access
object-properties in an anonymous function.
In this case, object properties of an anonymous object - the obvious
solution is don't use an anonymous object where it would be much
easier to use one with a name. :-)

Dunno about "best practice", but a solution (not necessarily best, or
perhaps even good) is:

function test() {
({

_call: function(that, fn) {
fn.call(that);
},

test: function() {

/* Position A */
console.log(this);

// Just for the example
var self = this;

this._call(this, function() {

/* Position B */
console.log(this == self); // true
});
}
}).test();
}
--
Rob
Apr 11 '08 #2
On Apr 11, 4:35 pm, Sebastian Morawietz
<sebastian.morawi...@googlemail.comwrote:
On 11 Apr., 05:22, RobG <rg...@iinet.net.auwrote:
On Apr 11, 1:35 am, Sebastian Morawietz
Dunno about "best practice", but a solution (not necessarily best, or
perhaps even good) is:
function test() {
({
_call: function(that, fn) {
fn.call(that);
},
test: function() {
/* Position A */
console.log(this);
// Just for the example
var self = this;
this._call(this, function() {
/* Position B */
console.log(this == self); // true
});
}
}).test();
}

Thanks for the answer. A closer look to Prototype's sources revealed
to me, that this is basically, how they do it with their iterators
too.
Do they? Dang, that's almost certain to mean it's not in the "best
practice" list of anyone here. :-)

One way to get anonymous iterators to call themselves is with
arguments.callee, but that doesn't get the "parent" object, which is
what you seem to be after.
--
Rob
Apr 11 '08 #3

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

Similar topics

4
by: James | last post by:
I have a from with 2 fields: Company & Name Depening which is completed, one of the following queries will be run: if($Company){ $query = "Select C* From tblsample Where ID = $Company...
5
by: Scott D | last post by:
I am trying to check and see if a field is posted or not, if not posted then assign $location which is a session variable to $location_other. If it is posted then just assign it to...
2
by: Nick | last post by:
Can someone please tell me how to access elements from a multiple selection list? From what ive read on other posts, this is correct. I keep getting an "Undefined variable" error though... Form...
2
by: Alexander Ross | last post by:
I have a variable ($x) that can have 50 different (string) values. I want to check for 7 of those values and do something based on it ... as I see it I have 2 options: 1) if (($x=="one") ||...
0
by: Dan Foley | last post by:
This script runs fine, but I'd like to know why it's so slow.. Thanks for any help out there on how i can make it faster (it might take up to 5 min to write these 3 export files whith 15 records...
5
by: Lee Redeem | last post by:
Hi there I've created abd uploaded this basic PHP script: <html> <head> <title>PHP Test</title> </head> <body> <H1 align="center">
5
by: christopher vogt | last post by:
Hi, i'm wondering if there is something like $this-> to call a method inside another method of the same class without using the classname in front. I actually use class TEST { function...
6
by: Phil Powell | last post by:
Ok guys, here we go again! SELECT s.nnet_produkt_storrelse_navn FROM nnet_produkt_storrelse s, nnet_produkt_varegruppe v, nnet_storrelse_varegruppe_assoc sv, nnet_produkt p WHERE...
1
by: Michel | last post by:
a site like this http://www.dvdzone2.com/dvd Can you make it in PHP and MySQL within 6 weeks? If so, send me your price 2 a r a (at) p a n d o r a . b e
11
by: Maciej Nadolski | last post by:
Hi! I can`t understand what php wants from me:( So: Cannot send session cache limiter - headers already sent (output started at /home/krecik/public_html/silnik.php:208) in...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...

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.