Calling built-in methods on host objects

R

RobG

The following works provided obj is a native object and sufficiently
array-like:

var obj = {'0':'0','1':'1','2':'2', length: 3};
var a = Array.prototype.slice.call(obj);

However, if obj is a host object that is array-like (e.g. a NodeList),
it works in some browsers and not in others - it fails in IE 6 with a
NodeList.

Can someone tell me if it works in some later version of IE, and if
there are other browsers in which it also fails?
 
M

Martin Honnen

RobG said:
The following works provided obj is a native object and sufficiently
array-like:

var obj = {'0':'0','1':'1','2':'2', length: 3};
var a = Array.prototype.slice.call(obj);

However, if obj is a host object that is array-like (e.g. a NodeList),
it works in some browsers and not in others - it fails in IE 6 with a
NodeList.

Can someone tell me if it works in some later version of IE, and if
there are other browsers in which it also fails?

var elements1 = document.getElementsByTagName('*');
var elements2 = Array.prototype.slice.call(elements1, 2);
elements1.length + '\r\n' + elements2.length

throws an error (German message "JScript-Objekt erwartet", my
translation "JScript object expected") in IE 8.

IE 9 has a new JScript engine specifically written to work inside IE so
it might work there, but I haven't yet tested.
 
M

Martin Honnen

Martin said:
IE 9 has a new JScript engine specifically written to work inside IE so
it might work there, but I haven't yet tested.

Tested

var elements1 = document.getElementsByTagName('*');
var elements2 = Array.prototype.slice.call(elements1, 2);
elements1.length + '\r\n' + elements2.length

in IE 9 release candidate, it works flawlessly without throwing any
errors. At least in standards mode, in quirks mode it throws the
following error "Message: Array.prototype.slice: 'this' is not a
JavaScript object".
 
R

RobG

Tested

var elements1 = document.getElementsByTagName('*');
var elements2 = Array.prototype.slice.call(elements1, 2);
elements1.length + '\r\n' + elements2.length

in IE 9 release candidate, it works flawlessly without throwing any
errors. At least in standards mode, in quirks mode it throws the
following error "Message: Array.prototype.slice: 'this' is not a
JavaScript object".

Thanks, I guess that means IE 9 in quirks mode and IE < 9 will all
throw an error. No doubt the mobile version will give similar results
(if the IE 9 script engine is available there).
 
D

David Mark

Thanks, I guess that means IE 9 in quirks mode and IE < 9 will all
throw an error. No doubt the mobile version will give similar results
(if the IE 9 script engine is available there).

See the QSA add-on for My Library for a basic cross-browser solution
(converts host objects to arrays). It's the only place I used that as
I didn't want try-catch in the core back then (though not really
concerned now).
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top