Prototype bug or feature?

A

a.riser

Hello

When using *this* in an Array#each or Hash#each-loop this references
*window*. Is this intentional (I can't believe it)? An example here:
http://www.banza.net/jstest/this.html .

The code there is:

var Test = Class.create({
initialize: function(){
this.location = '----------- eeehhhh ---------------';
this.array = $A([1])
this.hasch = $H({key_one:'val_one'});
},
procede: function(){
document.writeln(' this.location: "'+this.location+'"<br />');
this.array.each( function( item ){
document.writeln(' this.location: "'+this.location+'"<br />');
});
document.writeln(' this.location: "'+this.location+'"<br />');
this.hasch.each( function( item ){
document.writeln(' this.location: "'+this.location+'"<br />');
});
}
});
x = new Test();
x.procede();

I'm using of course Prototype 1.6.0.2.

Greets, a.
 
J

Joost Diepenmaat

Hello

When using *this* in an Array#each or Hash#each-loop this references
*window*. Is this intentional (I can't believe it)? An example here:
http://www.banza.net/jstest/this.html .

Dunno if it's intentional; I don't use Prototype, but it matches the
behaviour of Array.prototype.forEach in JavaScript 1.6:

<http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_
Reference:Objects:Array:forEach>

array.forEach(callback[, thisObject]);

IOW, in JS 1.5 you must pass a second argument specifying the <<this>>
object, otherwise it'll be the global object (window).
 

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

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top