SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Feb 2002
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Inheriting Array doesn't work in IE

    Here's a piece of code that works fine in FireFox, but not in IE. What am I doing wrong?
    PHP Code:
      <html>
      <
    head>
          <
    title>Inheriting Array class</title>
          <
    script>
      function 
    ExtArray() {}
      
    ExtArray.prototype = new Array();
     
      var 
    sources = new ExtArray();
      
    sources.push('First'); 
      
    sources.push('Second');
      
    alert(sources.length ': [' sources[0] + ',' sources[1] + ']');
          
    </script>
      </head>
      <body>
      </body>
      </html> 

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    instead of:

    Code:
    function ExtArray() {}
    try:

    Code:
    function ExtArray() { Array.call(this); }

  3. #3
    SitePoint Member
    Join Date
    Feb 2002
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Didn't work.
    And neither did
    function ExtArray() { Array.apply(this, arguments); }

  4. #4
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IE doesn't iterate the 'native object code' the way firefox does.
    Your code would work for an object you created, but not the 'built in' objects.
    You have to enable each prototype property explicitly.

    This code returns '2:[First,Second] ' in IE:

    Code:
    function ExtArray() {
    this.push=Array.prototype.push;
    this.length=Array.prototype.length;
    }
     
      var sources = new ExtArray();
      sources.push('First'); 
      sources.push('Second');
      alert(sources.length + ': [' + sources[0] + ',' + sources[1] + ']');
    You can branch your code or use the IE version in any browser.

  5. #5
    SitePoint Member
    Join Date
    Feb 2002
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, mrhoo! That works like a charm!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •