Array构造函数的合法使用 [英] Legitimate uses of the Array constructor

查看:91
本文介绍了Array构造函数的合法使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢这个问题 - <一个href=\"http://stackoverflow.com/questions/3026089/legitimate-uses-of-the-function-constructor\">Legitimate Function构造的使用 - 所以我想创建关于阵列构造一个类似的问题。

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.

所以

arr = new Array( 7 );

等同于

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 '*****'

这是很酷,但希望一些更高级的应用。 (有什么这将使新阵列标记中的JavaScript程序合法工具。)

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.)

更新:我注意到了jQuery库使用新阵列(LEN)的一个实例符号 - 它里面的功能(搜索时:

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.

他们用它来初始化 pValues​​ 局部变量,这是在本地函数中使用了code进一步下跌:

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 = [],

将打破......(是新阵列(长度)要求 notifyWith 程序才能正常工作? )

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

推荐答案

如何作为替代的方式来浅克隆阵列?

How about as an alternate way to shallow clone an Array?

var arr = [1,2,3];

var arr2 = Array.apply( null, arr );

arr === arr2; // false;

arr.length === arr2.length; // true

是的,我在这里深远,因为你只需使用 .slice(),但当时我真的没有看到使用阵列作为首位非法的,只要你知道自己在做什么。

Yes, I'm reaching here because you'd just use .slice(), but then I really don't see using Array as illegitimate in the first place, as long as you know what you're doing.

下车的话题,但使该利用阵列共同作用的一种方式。需要绑定懒得现在要更新旧的浏览器。

Getting off topic, but one way to make a common function that utilizes Array or slice. Requires bind. Too lazy right now to update for older browsers.

http://jsfiddle.net/fdgBU/1/

var slicer,
    apply = Function.prototype.apply;

if( apply.bind ) {
    try {
        (slicer = apply.bind( Array, null ))({length:0});
    } catch( e ) {
        slicer = apply.bind([].slice);
    }
} else {
   slicer = function( coll ) {
        if( !coll || coll.length !== +coll.length ) return;
        var res = [], i = 0, len = coll.length >>> 0;
        for( ; i < len; ++i ) {
            res[i] = coll[i];
        }
        return res;
    };
}


var arr_like = {'0':true,'1':true,'2':true,length:3},
    arr = slicer( arr_like );

console.log( arr ); // [true, true, true]

console.log( arr.length === arr_like.length); // true

这篇关于Array构造函数的合法使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆