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

  • Home
  • SEARCH
  • 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 8120659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:12:55+00:00 2026-06-06T05:12:55+00:00

According to this answer I want to create my own subclass of Array QArray

  • 0

According to this answer I want to create my own subclass of Array

QArray = function() {
    Array.apply(this, arguments);
};
QArray.prototype = new Array();
QArray.prototype.constructor = QArray;

which works as expected, method invocation works, but the constructor does not chain to Array.

// test constructor chaining
q = new QArray('1', '2', '3');
assert.equals('1', q[0]);      // => undefined
assert.equals(3, q.length);    // => 0
q = new QArray(10);
assert.equals(10, q.length);   // => 0

This tests pass if I replace QArray with plain Array. Somehow Array seems to be a special case. (I run this in Rhino 1.6 which is Javascript 1.5.) How can I fix my custom subclass of Array?

  • 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-06T05:12:57+00:00Added an answer on June 6, 2026 at 5:12 am

    Since your method already inherits the array methods, you can use array methods which modify the keys, such as Array.prototype.push:

    QArray = function() {
        if (arguments.length == 1) this.length = arguments[0]; // Array constructor
        else this.push.apply(this, arguments);
    };
    QArray.prototype = new Array();
    QArray.prototype.constructor = QArray;
    
    q = new QArray('1', '2', '3');   // => [1,2,3]
    assert.equals('1', q[0]);        // => true
    assert.equals(3, q.length);      // => true
    q = new QArray(10);              // => [,,,,,,,,,]
    assert.equals(10, q.length);     // => true
    q instanceof QArray;             // => true
    q instanceof Array;              // => true
    

    I’ve once written a custom array implementation, which behaves like a true array (including length setter/getter). If you need inspiration, have a look at this answer.

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

Sidebar

Related Questions

According to this answer , I can make C++ mode think that underscores are
according to this code if i want to access imageView1 and imageView2 how can
I want to create a function that can take different types of iterators which
I want to sort files by modification time ascending and descending. According to this
According to this answer boost and STL headers belong into the precompiled header file
I have a SQLite database. According to this answer you need version 3.6.19+, which
I didnt find anything according this issue. Can jaas be used to secure my
I have made a custom suface button according this example: <Button> <Button.Template> <ControlTemplate TargetType=Button>
According to this post , i ask $title. Which are your favorite PHP coding
According to this feature matrix , Visual Studio 2010 Premium (RC) includes static code

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.