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 7700675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:45:51+00:00 2026-05-31T22:45:51+00:00

How can I create a custom array constructor, which is an extended version of

  • 0

How can I create a custom array constructor, which is an extended version of the native Array constructor?

jQuery, for example, looks like an array with additional methods, such as $().addClass. However, it didn’t modify Array.prototype, because new Array().hasClass is undefined.

So, how can I create an extended array implementation, without modifying Array.prototype?

Example:

Employees( ... )          //-> [{name: 'John', age: 32}, {name: 'Bob', age: 29}];
Employees( ... ).byAge(32)//-> [{name: 'John', age: 32}];
// and
Array().byAge             //-> undefined
  • 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-05-31T22:45:52+00:00Added an answer on May 31, 2026 at 10:45 pm

    jQuery is not a true Array implementation: jQuery instanceof Array is false!

    If you want to create a true instance of an array, and add custom methods, use this code. It uses Function.prototype.bind to call a constructor with an arbitrary number of parameters.

    The implementation behaves exactly as a true array, except at one point:

    • When the Array constructor is called with a single argument, it’s creating an array with a length of this argument.
    • Since this feature is often a source of bugs, I have decided to omit it in the implementation. You can still set a length of n by setting the length property.

    Code: http://jsfiddle.net/G3DJH/

    function Employees() {
        // Deal with missing "new"
        if (!(this instanceof Employees)) {
            // Convert arguments to an array, because we have to shift all index by 1
            var args = Array.prototype.slice.call(arguments);
            args.unshift(this); // Shift all indexes, set "this" 
            return new (Function.prototype.bind.apply(Employees, args));
        } else {
            // Set length property.
            var len = arguments.length,
                /*
                 * fn_internalLength: Internal method for calculating the length
                 **/
                fn_internalLength,
                /*
                 * explicitLength: Deals with explicit length setter
                 **/
                explicitLength = 0;
    
            // Setting all numeric keys
            while (len--) {
                this[len] = arguments[len];
            }
    
            // Internal method for defining lengths
            fn_internalLength = function() {
                var allKeys = Object.keys(this).sort(function(x, y) {
                    // Sort list. Highest index on top.
                    return y - x;
                }), i=-1, length = allKeys.length, tmpKey,
                foundLength = 0;
    
                // Loop through all keys
                while (++i < length && (tmpKey = allKeys[i]) >= 0) {
                    // Is the key is an INTEGER?
                    if (tmpKey - tmpKey === 0 && tmpKey % 1 === 0) {
                        foundLength = 1*tmpKey + 1;
                        break;
                    }
                }
                // Return MAX(actual length, explictly set length)
                return foundLength > explicitLength ? foundLength : explicitLength;
            }.bind(this);
    
            // Define the magic length property
            Object.defineProperty(this, 'length',
            {
                get: fn_internalLength,
                set: function(newLength) {
                    var length = fn_internalLength();
                    if (newLength < length) {
                        for (var i=newLength; i<length; i++) {
                            delete this[i];
                        }
                    }
                    // Set explicit length
                    explicitLength = newLength;
                },
                enumerable: false,
                configurable: false
            });
        }
    }
    Employees.prototype = new Array;
    
    
    // Example: Custom method:
    Employees.prototype.print = function() {
        return this.join('--'); // Using inherit Array.prototype.join
    };
    
    // Just like the Array, `new` is optional
    console.log(new Employees(1,2).print());
    console.log(Employees(1,2).print());
    
    // Is the object an array?
    console.log(new Employees() instanceof Array);    // True!
    // Can't believe it?
    console.log(new Employees() instanceof Employees); // True!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning PyQt and wonder if one can create custom/owner draw control like
I need to create a custom constraint annotation which can access the value of
When I create a custom component, I define a property which is array that
I have a web app where people can create custom content on the web
Can i create custom touch events for iPhone?? will the device support for creating
How can I create custom variable in helper.php file set it right here and
In Drupal you can create your own nodetype in a custom module. Doing this
I have create a custom contextMenu using AS3 and can apply that to the
Is it possible to create a custom configuration file (other than app.config) that can
Is it possible to create a custom UIPopoverController? Can you give me some suggestions

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.