473,320 Members | 1,862 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,320 software developers and data experts.

Prototype bug or feature?

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.
Jun 27 '08 #1
3 1068
a.*****@web.de writes:
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...avaScript_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).
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #2
On 3 Mai, 19:13, Joost Diepenmaat <jo...@zeekat.nlwrote:
<http://developer.mozilla.org/en/docs...avaScript_1.5_
Reference:Objects:Array:forEach>

* array.forEach(callback[, thisObject]);
Thank you, it's indeed the same in Prototype (now I found it in the
docs - shame on me):

...
this.hasch.each( function( item ){
document.writeln(' this.location: "'+this.location+'"<br />');
}, this);
...

works as I would have expected without the second parameter.

Jun 27 '08 #3
In fact Prototype uses the Native Array#forEach when available:
if (Object.isFunction(Array.prototype.forEach))
Array.prototype._each = Array.prototype.forEach;

Also for future reference the Prototype user mailing list is here:
http://groups.google.com/group/rubyonrails-spinoffs

- JDD
Jun 27 '08 #4

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

Similar topics

4
by: jemptymethod | last post by:
http://htmatters.net/htm/1/2006/01/EIBTI-for-Javascript-explicit-is-better-than-implicit.cfm
12
by: petermichaux | last post by:
Hi, I've been reading the recent posts and older archives of comp.lang.javascript and am surprised by the sentiments expressed about the prototype.js library for a few reasons: 1) The library...
13
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
2
by: Reinhold Schrecker | last post by:
Hi there, I am using the JavaScript prototype library (http://prototype.conio.net/) and want to check if the library is running properly, because javascript prototype is not supported by IE...
17
by: Chaprasi | last post by:
Hi I need help with prototype while doing ajax calls. So this is my JS which does a ajax calls var ajax1 = new Ajax.Request ( url, { method: 'get', parameters: params, onComplete:...
45
by: bigdadro | last post by:
I've created a new class using prototype.js. After I make the ajax.request all references to this.myClassMethodorVariable are lost. Does the ajax method blow out the object persistance? I'm fairly...
8
by: Chad Burggraf | last post by:
Hi all, I'm brand new to this newsgroup. I've been reading the FAQ's and some other documentation submitted by regulars of the group (such as "Javascript Best Practices" at...
23
by: Dautkhanov | last post by:
Hello ! Does anybody have cutted version of prototype.js with the AJAX functionality only? I am a new in prototype.js topic, so I think this task should be done by other developers. Maybe...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
5
by: jirkap | last post by:
I'm trying to understand how the prototype property works, but there is one thing I donť get: As far as I've got it, when I have one custom object with some methods and properties, all instances of...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
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...
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)...
0
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...
0
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.