Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何添加新的数组方法;不';“t触发器”;为了……在?_Javascript_Arrays - Fatal编程技术网

Javascript 如何添加新的数组方法;不';“t触发器”;为了……在?

Javascript 如何添加新的数组方法;不';“t触发器”;为了……在?,javascript,arrays,Javascript,Arrays,在将for..in循环与我添加到array.prototype的函数一起使用时,for..in循环也会遍历所有这些函数 但是我如何添加到阵列原型中,而不在for..in中触发它呢 示例代码: Array.prototype.sum = function (prop) { // Do something } for (item in myArray) { // Would actually loop myArray.length times + 1 for the 'sum'

在将for..in循环与我添加到array.prototype的函数一起使用时,for..in循环也会遍历所有这些函数

但是我如何添加到阵列原型中,而不在for..in中触发它呢

示例代码:

Array.prototype.sum = function (prop) {
    // Do something
}

for (item in myArray) {
    // Would actually loop myArray.length times + 1 for the 'sum'
}
我正在使用一个用于..in循环的库,我无法更改它。

使用y,您可以向
数组添加一些内容。prototype
,同时使其不可
枚举
,因此不能成为
in
循环的目标

不过,作为最后的手段,如果可能的话,应该避免使用
for in

//这将在中显示
Array.prototype.sum=函数(prop){
//做点什么
}
//这不会出现在for-in循环中。
Object.defineProperty(Array.prototype,'add'{
值:函数(){
console.log('OOPSS');
},
//enumerable默认为false,它决定attr是否为目标
//是否在循环中。
可枚举:false
});
var myArray=[1,2,3];
用于(myArray中的项){
控制台日志(项目);
//将实际循环myArray.length乘以'sum'的+1
}
//选中'add'已添加到array.prototype。
myArray.add()
使用y,您可以向
数组中添加一些东西。原型
,同时使其不可枚举,从而不成为
for in
循环的目标

不过,作为最后的手段,如果可能的话,应该避免使用
for in

//这将在中显示
Array.prototype.sum=函数(prop){
//做点什么
}
//这不会出现在for-in循环中。
Object.defineProperty(Array.prototype,'add'{
值:函数(){
console.log('OOPSS');
},
//enumerable默认为false,它决定attr是否为目标
//是否在循环中。
可枚举:false
});
var myArray=[1,2,3];
用于(myArray中的项){
控制台日志(项目);
//将实际循环myArray.length乘以'sum'的+1
}
//选中'add'已添加到array.prototype。
myArray.add()
但是我如何添加到阵列原型中,而不在for..in中触发它呢

通过将其添加为不可枚举属性,通过。默认情况下为不可枚举,因此:

Object.defineProperty(Array.prototype, "sum", {
    value: function() {
        // ...implementation of sum
    }
});
…但是如果我们想要明确,我们就必须
可枚举:false

Object.defineProperty(Array.prototype, "sum", {
    enumerable: false, // <== Not necessary, false is the default
    value: function() {
        // ...implementation of sum
    }
});

但是我如何添加到阵列原型中,而不在for..in中触发它呢

通过将其添加为不可枚举属性,通过。默认情况下为不可枚举,因此:

Object.defineProperty(Array.prototype, "sum", {
    value: function() {
        // ...implementation of sum
    }
});
…但是如果我们想要明确,我们就必须
可枚举:false

Object.defineProperty(Array.prototype, "sum", {
    enumerable: false, // <== Not necessary, false is the default
    value: function() {
        // ...implementation of sum
    }
});


你不应该,我不明白你为什么要这么做。@Chrillewoodz:我不应该什么?这样使用原型?我不熟悉Object.defineProperty,现在是了。@webdeb:n3行-chart@AlexD:我会向库作者提出一个错误,没有理由在数组上使用
for in
,至少不检查
hasOwnProperty
。或者他们最好使用length,如果它始终是数组,因为in返回索引或键,不是这个值,所以它可以做得更便宜->var l=arr.length;而(!(0>l)){l--;}你不应该,我不明白你为什么要。@Chrillewoodz:我不应该什么?这样使用原型?我不熟悉Object.defineProperty,现在是了。@webdeb:n3行-chart@AlexD:我会向库作者提出一个错误,没有理由在数组上使用
for in
,至少不检查
hasOwnProperty
。或者他们最好使用length,如果它始终是数组,因为in返回索引或键,不是这个值,所以它可以做得更便宜->var l=arr.length;而(!(0>l)){l--;}