Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Legitimate uses of the Array constructor

I liked this question - Legitimate uses of the Function constructor - so I wanted to create a similar question regarding the Array constructor.

Of course, the array literal notation is the correct way to create arrays. This would mean that the new Array notation should not be used. And "case closed".

However, there is one specificity of the new Array form. If a natural number is passed in, an empty array is created and its length property is set to that number.

So

arr = new Array( 7 );

is equivalent to

arr = [];
arr.length = 7;

This can be considered a feature. I was wondering if this "feature" has real-world uses. I recently stumbled upon one such (simple) use:

new Array( n + 1 ).join( '*' ) // returns string containing n stars

// e.g.
new Array( 3 ).join( '*' ) // returns '**'
new Array( 6 ).join( '*' ) // returns '*****'

This is cool, but was hoping for some more advanced uses. (Something that would make the new Array notation a legitimate tool in JavaScript programs.)


Update: I've noticed that the jQuery library uses the new Array( len ) notation in one instance - it's inside the when function (search for "when:"):

when: function( firstParam ) {
    var args = sliceDeferred.call( arguments, 0 ),
        i = 0,
        length = args.length,
        pValues = new Array( length ),
        count = length,
        pCount = length,
        // etc.

They use it to initialize the pValues local variable, which is used in a local function further down in the code:

function progressFunc( i ) {
    return function( value ) {
        pValues[ i ] = arguments.length > 1 ?
                sliceDeferred.call( arguments, 0 ) : value;
        deferred.notifyWith( promise, pValues );
    };
}

I would love to know if changing the assignment to just

pValues = [],

would break the program... (Is new Array( length ) required for notifyWith to work properly?)

like image 434
Šime Vidas Avatar asked Dec 09 '11 23:12

Šime Vidas


People also ask

What does array constructor do?

The Array() constructor is used to create Array objects.

What is the use of array constructor in JavaScript?

The JavaScript Array constructor property is used to return the constructor function for an array object. It only returns the reference of the function and not returns the name of the function. So, In JavaScript arrays, it returns the function Array() { [native code] }.

What is the practical use of array?

Arrays are the simplest data structures that store items of the same data type. A basic application of Arrays can be storing data in tabular format. For example, if we wish to store the contacts on our phone, then the software will simply place all our contacts in an array.

Can we use constructor in array?

An array constructor can be used to define only an ordinary array with elements that are not a row type. An array constructor cannot be used to define an associative array or an ordinary array with elements that are a row type. Such arrays can only be constructed by assigning the individual elements.


1 Answers

I don't know if this counts, but one case where you'd use the constructor is if you need to be sure you have clean, unmodified Array constructor. You can create an "iframe sandbox"

var iframe = document.createElement("iframe");
iframe.style.display = "none";
document.body.appendChild(iframe);
var Safe = frames[frames.length - 1];

And then create arrays like this...

var things = new Safe.Array('x', 'y', 'z');

...knowing that your arrays will not have any foreign stuff in the prototype put there by other scripts.

That's not making use of that single-parameter-as-array-length feature, though. The only thing that's probably really good for is setting up huge arrays to benchmark stuff.

like image 167
Dagg Nabbit Avatar answered Nov 15 '22 16:11

Dagg Nabbit