Calling built-in methods on host objects

Discussion in 'Javascript' started by RobG, Mar 15, 2011.

  1. RobG

    RobG Guest

    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?


    --
    Rob
     
    RobG, Mar 15, 2011
    #1
    1. Advertising

  2. RobG wrote:
    > 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.



    --

    Martin Honnen
    http://msmvps.com/blogs/martin_honnen/
     
    Martin Honnen, Mar 15, 2011
    #2
    1. Advertising

  3. Martin Honnen wrote:
    > RobG wrote:
    >> 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?



    > 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".




    --

    Martin Honnen
    http://msmvps.com/blogs/martin_honnen/
     
    Martin Honnen, Mar 15, 2011
    #3
  4. RobG

    RobG Guest

    On Mar 16, 3:03 am, Martin Honnen <> wrote:
    > Martin Honnen wrote:
    > > RobG wrote:
    > >> 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?

    > > 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".


    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).


    --
    Rob
     
    RobG, Mar 15, 2011
    #4
  5. RobG

    David Mark Guest

    On Mar 15, 6:53 pm, RobG <> wrote:
    > On Mar 16, 3:03 am, Martin Honnen <> wrote:
    >
    >
    >
    > > Martin Honnen wrote:
    > > > RobG wrote:
    > > >> 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?
    > > > 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".

    >
    > 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).
     
    David Mark, Mar 21, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Orpheus66
    Replies:
    0
    Views:
    4,483
    Orpheus66
    Jul 30, 2003
  2. Oliver Wong
    Replies:
    14
    Views:
    1,654
    Chris Uppal
    Jun 13, 2006
  3. Jerry Qu
    Replies:
    1
    Views:
    508
    lee atkinson
    Feb 20, 2009
  4. Oltmans
    Replies:
    6
    Views:
    362
    Terry Reedy
    Mar 11, 2009
  5. Kenneth McDonald
    Replies:
    5
    Views:
    345
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page