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

strange for-in loop output

Hi, i found this strange thing in firefox.

if i alert the output of a simple 'for in' loop, it returns first the right value and then:
Expand|Select|Wrap|Line Numbers
  1. function forEach(){ 
  2. [native code]
why is it doing this? it gives an error and its not in the array at all..?
Dec 11 '06 #1
1 1182
gits
5,390 Expert Mod 4TB
hi ...

with javascript 1.6 (gecko-engine 1.8b2 or greater) the Array will have a native forEach()-method ... to which you may refer here and there you will see how you could implement it in a enviroment where you don't have that native method. using a custom implementation for the array results in your observed behaviour ... a for-in loop will loop over the 'array'-keys and ALL methods that are 'custom' extensions for the native array-object ... so you just have to use the 'determined' for loop - have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. var a  = [1, 3, 4];
  2.  
  3. var cb = function(val) {
  4.     alert(val);
  5. };
  6.  
  7. // newer FF will alert the array values here
  8. if (typeof a.forEach != 'undefined') {
  9.     a.forEach(cb);
  10. }
  11.  
  12. // we extend the native array now
  13. Array.prototype.test = function() {
  14. }
  15.  
  16. // you will get 0...2 AND test alerted
  17. for (var i in a) {
  18.     alert(i);
  19. }
  20.  
  21. // using the 'determined' loop behaves 'correct' regarding
  22. // the array-values
  23. for (var i = 0; i < a.length; i++) {
  24.     alert(i);
  25. }
  26.  
kind regards
Apr 8 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Bruce A. Julseth | last post by:
My MySQL.user table (user, host, password) looks like the following: +---------+-----------+---------------------+ | user | host | password |...
6
by: Eric Boutin | last post by:
Hi ! I have a strange problem with a std::ostringstream.. code : #include <sstream> /*...*/ std::ostringstream ss(); ss << "\"\"" << libpath << "\"\" \"\"" << argfilename << "\"\"...
1
by: davidw | last post by:
I encountered strange issues. I have code like this sqlReader = SqlHelper.ExecuteReader(connString, System.Data.CommandType.Text,sql); It calls Microsoft.ApplicationBlocks.Data to execute a...
0
by: ivb | last post by:
Hi all, I am using DB2 8.1.11.1 on NT with ASP.NET 1.1 When application make connection to database (via ADO.NET), it set "Connection timeout" parameter to 30 seconds. After, when my webpage...
28
by: charlie | last post by:
Hi, I found an article on informit.com that talks about C++ casts http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=285&rl=1 The strange thing is the author says the following...
9
by: Larry | last post by:
I was testing the buffer size of system call, read(), and found a strange error on Ubuntu 7.10 server. The code is attached. If the BUFFSIZE is set to from 128 to 255, the code will produce an...
9
by: Elroy Flynn | last post by:
Decimal a = 42.485M; Decimal b = 48M; Decimal c = a / b; Console.WriteLine(c.ToString()); gives the result 0.8851041666666666666666666667 The correct, exact answer is .885. I know that...
1
by: raylopez99 | last post by:
The below did not set off my compiler, perhaps because I set the warnings to a higher (less nag) level. Strange. FYI, no question being asked. RL double d2030;
4
by: Mr.SpOOn | last post by:
Hi, I'm trying the nose testing package. I've just started reading the tutorial and I had a problem with the first simple example. This is the test: def test_b(): assert 'b' == 'b' In the...
3
by: djpaul | last post by:
Hello, I run a script to get some info from another site. But on that page some 'strange' character like é. When i grab them an display it on my site i see strange characters like Beyoncé will be...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.