New issue
Advanced search Search tips
Note: Color blocks (like or ) mean that a user may not be available. Tooltip shows the reason.
Starred by 5 users

Issue metadata

Status: WorkingAsIntended
Owner: ----
Closed: Apr 2015
Cc:
HW: ----
NextAction: ----
OS: ----
Priority: ----
Type: ----
ES5



Sign in to add a comment

Javascript 1.6 Array generics don't work?

Project Member Reported by mkte...@gmail.com, Apr 13 2009 Back to list

Issue description

I ran into this issue while trying out someone's userscript in Chrome
2.0.169.1 (the 2.0 Beta).

According to the last two examples on this page:
https://developer.mozilla.org/en/DOM/document.getElementsByClassName

As of Javascript 1.6 it's possible to do something like the following, to
find all div elements that have a class of 'test':

 Array.filter( document.getElementsByClassName('test'), function(elem){
   return elem.nodeName == 'DIV';
 });

In Chrome, however, the Error Console says:
Uncaught TypeError: Object function Array() { [native code] } has no method
'filter'

Not sure if this only happens with Array.filter, or all of the Array extras.

More info here:
https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Working_with_Arrays#Working_with_Array-like_objects
 
Status: WorkingAsIntended
It happens for all proprietary Mozilla extensions to ECMAScript that are not also 
implemented by Safari. Array.filter does not exist in Safari.

V8 is created to be compatible with the ECMAScript standard and the extensions from the 
JavaScript engine of Safari, and does not implement other non-standard extensions, 
e.g., from Mozilla.

Comment 2 by mkte...@gmail.com, Apr 13 2009

Hmm, okay.  I have another question... this page provides a prototype function
("exactly the one used in Firefox and SpiderMonkey") which can be inserted at the
beginning of scripts for compatibility:
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter

I added it to the userscript, but the Error Console still says the same thing:
Uncaught TypeError: Object function Array() { [native code] } has no method
'filter'

Do you know why that isn't working?
Works for me.  I ran the attached file in the console version of V8 with the expected 
result.
filter-test.js
745 bytes View Download
The prototype function is attached to Array.prototype. I.e., it creates a function 
on all array objects (or, incidentally, it doesn't, as this is a non-standard 
function that Safari and Chrome already support).

The function you call, if I parse the error message correctly, is Array.filter. 

You can try creating this yourself:

  Array.filter = function filter(array, test) {
    return Array.prototype.filter.call(array, test);
  }

This might do what you need (not tested).

Comment 5 by mkte...@gmail.com, Apr 14 2009

Thanks, erik; filter-test.js works for me too.  However, if you add this to the script:

 Array.filter( document.getElementsByClassName('test'), function(elem){
   return elem.nodeName == 'DIV';
 });

It generates the "has no method 'filter'" error.  Try the attached file. (Same as
yours, just with the above code added before the "print(filtered)" command.)
filter-test2.js
854 bytes View Download

Comment 6 by mkte...@gmail.com, Apr 14 2009

Thanks, lrn, that seems to work!

Comment 7 by erights@google.com, May 22 2010

This issue should be marked with the ES5 label.

Comment 8 by ager@chromium.org, May 25 2010

Labels: ES5

Sign in to add a comment