返回的Array.push推动价值? [英] Array.push return pushed value?

查看:93
本文介绍了返回的Array.push推动价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何实质性的原因修改的Array.push()返回对象推,而不是新的数组可能是一个坏主意的长度?

如果这已经提出或之前问我不知道;谷歌搜索只返回有关的当前功能的Array.push()问题无数号。

下面是此功能的一个示例实现,随时纠正它:

 ;(函数(){
    VAR _push = Array.prototype.push;
    Array.prototype.push =功能(){
        返回此[_push.apply(这一点,参数) - 1];
    }
}());

您随后将能够做这样的事:

  VAR =的someArray [],
    值=Hello World的;函数someFunction(值,OBJ){
    OBJ [someKey] =值;
}someFunction(值,someArray.push({}));

其中, someFunction 修改传递作为第二个参数,例如对象。现在的内容的someArray [{someKey:世界你好}]

有什么缺点,以这种方式?


解决方案

  

是否有任何实质性的原因修改的Array.push()返回对象推,而不是新的数组可能是一个坏主意的长度?


当然还有一个:其他code将期望阵列::推表现为说明书中定义,即返回新的长度。和其他开发商会发现你的code INCOM prehensible如果你没有重新定义内建函数行为异常。

至少为方法选择一个不同的名称。


  

您随后将能够做这样的事: someFunction(值,someArray.push({}));


嗯,什么?是啊,我的第二点已经罢工: - )

不过,即使您没有使用这不会在你想做的事情搞定。你应该EX preSS是增加它由一个键和一个值到一个数组的对象组成。有了一个更实用的风格,让 someFunction 返回这个对象,你可以写

  VAR =的someArray [],
    值=Hello World的;函数someFunction(值,OBJ){
    OBJ [someKey] =值;
    返回OBJ;
}someArray.push(someFunction(值,{}));

Are there any substantial reasons why modifying Array.push() to return the object pushed rather than the length of the new array might be a bad idea?

I don't know if this has already been proposed or asked before; Google searches returned only a myriad number of questions related to the current functionality of Array.push().

Here's an example implementation of this functionality, feel free to correct it:

;(function() {
    var _push = Array.prototype.push;
    Array.prototype.push = function() {
        return this[_push.apply(this, arguments) - 1];
    }
}());

You would then be able to do something like this:

var someArray = [],
    value = "hello world";

function someFunction(value, obj) {
    obj["someKey"] = value;
}

someFunction(value, someArray.push({}));

Where someFunction modifies the object passed in as the second parameter, for example. Now the contents of someArray are [{"someKey": "hello world"}].

Are there any drawbacks to this approach?

解决方案

Are there any substantial reasons why modifying Array.push() to return the object pushed rather than the length of the new array might be a bad idea?

Of course there is one: Other code will expect Array::push to behave as defined in the specification, i.e. to return the new length. And other developers will find your code incomprehensible if you did redefine builtin functions to behave unexpectedly.

At least choose a different name for the method.

You would then be able to do something like this: someFunction(value, someArray.push({}));

Uh, what? Yeah, my second point already strikes :-)

However, even if you didn't use push this does not get across what you want to do. The composition that you should express is "add an object which consist of a key and a value to an array". With a more functional style, let someFunction return this object, and you can write

var someArray = [],
    value = "hello world";

function someFunction(value, obj) {
    obj["someKey"] = value;
    return obj;
}

someArray.push(someFunction(value, {}));

这篇关于返回的Array.push推动价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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