Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7997547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:59:22+00:00 2026-06-04T14:59:22+00:00

Very glad to learn of this way of basically subclassing a JavaScript Array (code

  • 0

Very glad to learn of this way of basically subclassing a JavaScript Array (code copied from link):

function SomeType() {
    this.push(16);
}

SomeType.prototype = [];
SomeType.prototype.constructor = SomeType; // Make sure there are no unexpected results

console.log(new SomeType()); // Displays in console as [16]

But this isn’t quite complete. Is there a way to fake subclass the Array like this and get the [] method?

var a = [];
a[3]  = true;
console.log(a.length); //=> 4

var s = new SomeType();
s[3]  = true;
console.log(s.length); //=> 1

This way you can still treat it as an array when doing a for loop:

for (var i = 0; i < s.length; i++) {
  var item = s[i];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:59:23+00:00Added an answer on June 4, 2026 at 2:59 pm

    Only works with browsers with __proto__ (which is deprecated ), so it’s not cross-browser :

    var CustomArray = function ( ) {
      var array = [ 16 ];
      array.__proto__ = this.__proto__;
      return array;
    };
    
    CustomArray.prototype = [];
    CustomArray.prototype.constructor = CustomArray;
    
    var array = new CustomArray( );
    console.log( array instanceof Array );       // true
    console.log( array instanceof CustomArray ); // true
    
    array[ 3 ] = 42;
    console.log( array.length );                 // 4
    

    I don’t think there is any other way to do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

there's this interesting problem i can not solve myself. I will be very glad,
Very frustrating problem I have here. I have this code: Button b = findViewById(android.R.id.button1);
Very odd problem. This function has randomly started hanging and timing out when I
Very short question. Can anyone say why this query select LENGTH(' '::char || '
Very simplified version of my code: #include <iostream> #include <string> using namespace std; int
I would be very glad if someone could show me a good example of
I use django-paginaton app and I'm very glad to use it. Now I need
I have a very weird problem, when storing my session in Memcached. From time
This is supposedly a very easy question, but I just can't seem to find
Just new to Monotouch! :D Very glad with it but still in the discovery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.