Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.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函数作为数组方法调用_Javascript - Fatal编程技术网

将javascript函数作为数组方法调用

将javascript函数作为数组方法调用,javascript,Javascript,更新:我知道forEach方法已有一些实现。这样做的目的是学习和提高我的Javascript技能 我正在尝试为数组对象实现一个forEach方法。我现在做的是: var list = ['one', 'two', 'three']; function forEach(callback){ for(var n = 0; n < this.length; n++){ callback.call(this[n], n); } }; list.forEach(f

更新:我知道forEach方法已有一些实现。这样做的目的是学习和提高我的Javascript技能

我正在尝试为数组对象实现一个forEach方法。我现在做的是:

var list = ['one', 'two', 'three'];

function forEach(callback){
    for(var n = 0; n < this.length; n++){
        callback.call(this[n], n);
    }
};

list.forEach(function(index){
        console.log(index);
        console.log(this);
    }
);
var list=['1','2','3'];
函数forEach(回调){
对于(var n=0;n
我对javascript不是很在行,我正在努力变得更好,所以我读了一些书,现在我知道如果我做这种事情,“forEach”函数的上下文将是调用它的对象,在本例中是“list”

当这段代码运行时,我得到一个错误:“uncaughttypeerror:objectone,two,three没有方法‘forEach’”

我不明白的是什么

谢谢

var list=['1','2','3'];
var list = ['one', 'two', 'three'];

list.forEach = function(callback){
    for(var n = 0; n < this.length; n++){
        callback.call(this[n], n);
    }
};

list.forEach(function(index){
        console.log(index);
        console.log(this);
    }
);​
list.forEach=函数(回调){ 对于(var n=0;n

似乎您正试图在该列表对象上创建自己的列表。如果是这样,您需要将其设置为该对象的属性。

您可以使用以下命令设置上下文对象()

另一种可能是调用list对象的函数:

list.myForEach = function(callback) { … };
list.myForEach(function(index) { … });
或者通过将其添加到所有对象的原型来启用此功能:


注意:阵列上应该已经有一个。在那里为古代浏览器使用兼容性垫片,以提供完全相同的功能。

我认为在实例化数组之前,您需要定义forEach

Array.prototype.forEach = function (callback) {
    var n;
    for(n = 0; n < this.length; n++){
        callback.call(this[n], n);
    }
};
Array.prototype.forEach=函数(回调){
var n;
对于(n=0;n
我认为您要做的可能不是将foreach函数仅附加到您这里的一个数组,而是使它适用于所有数组。要做到这一点,您必须编辑数组原型(有些人对此有非常强烈的意见,因为您无法防止将来可能发生的名称空间冲突,但其他人觉得非常有用)。一旦
Array.prototype
对象添加了一个函数,您就可以在任何数组上调用该函数。像这样:

var list = ['one', 'two', 'three'];

Array.prototype.myForEach = function(callback){
    for(var n = 0; n < this.length; n++){
        callback.call(this[n], n);
    }
};

list.myForEach(function(index){
    console.log(index);
    console.log(this);
});
var list=['1','2','3'];
Array.prototype.myForEach=函数(回调){
对于(var n=0;n

*注意:避免与现有forEach功能冲突(https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach)我已将函数命名为
myForEach
,我希望它不会发生冲突。

您需要将forEach方法添加到数组的原型中:

/*
* pay attention to the fact that if you use forEach aas method name you'll override
* the built default forEach method, try to use some prefix to distinguish between
* the built in methods and your own ones 
*/
Array.prototype.my_forEach = function(callback){ /* your code here */ };

数组上已存在一个属性…您没有将函数
forEach
list
对象的
forEach
属性相关联。不,您不需要这样做。JavaScript是动态的。
var list = ['one', 'two', 'three'];

Array.prototype.myForEach = function(callback){
    for(var n = 0; n < this.length; n++){
        callback.call(this[n], n);
    }
};

list.myForEach(function(index){
    console.log(index);
    console.log(this);
});
/*
* pay attention to the fact that if you use forEach aas method name you'll override
* the built default forEach method, try to use some prefix to distinguish between
* the built in methods and your own ones 
*/
Array.prototype.my_forEach = function(callback){ /* your code here */ };