如何将另一个对象的属性函数传递到函数中并在javascript中调用它?

如何将另一个对象的属性函数传递到函数中并在javascript中调用它?,javascript,function,object,Javascript,Function,Object,如果我在Javascript中有一个对象,其属性之一是函数: function cow() { this.timesMooed = 0; this.sayMoo = function () { this.timesMooed++; return "moo"; }; } 假设我还有另一个函数,它将某个函数作为参数,调用它并记录结果: var actionResults = []; function doAction(action) {

如果我在Javascript中有一个对象,其属性之一是函数:

function cow() {

    this.timesMooed = 0;

    this.sayMoo = function () {
        this.timesMooed++;
        return "moo";
    };
}
假设我还有另一个函数,它将某个函数作为参数,调用它并记录结果:

var actionResults = [];
function doAction(action) {
    actionResults.push(action());
}
现在让我们将其付诸实践,看看会发生什么:

var jerry = new cow();
doAction(jerry.sayMoo);
console.log(actionResults);
// Outputs ["moo"] -this is correct
console.log(jerry.timesMooed);
// Outputs 0 -uh oh

我如何传入函数,让Jerry运行函数?

当您传递对函数doAction的引用,然后使用action调用它时,调用上下文会发生变化,这就是决定函数值的原因。您需要使用bind将此的值锁定为:

功能奶牛{ this.timesMooed=0; this.sayMoo=函数{ 这个.timesMooed++; 返回moo; } } var actionResults=[]; 函数doActionaction{ actionResults.pushaction; } var jerry=新奶牛; //使用bind,可以正确设置带有'this'的函数 doActionjerry.sayMoo.bindjerry; console.logactionResults;
console.logjerry.timesMooed 当您将引用传递给函数doAction,然后用action调用它时,调用上下文会发生变化,这就是决定此函数值的原因。您需要使用bind将此的值锁定为:

功能奶牛{ this.timesMooed=0; this.sayMoo=函数{ 这个.timesMooed++; 返回moo; } } var actionResults=[]; 函数doActionaction{ actionResults.pushaction; } var jerry=新奶牛; //使用bind,可以正确设置带有'this'的函数 doActionjerry.sayMoo.bindjerry; console.logactionResults; console.logjerry.timesMooed 问题是,这个关键字在一个方法中使用,我们从一个接收方对象调用该方法,但它并没有绑定到我们希望它绑定到的对象,即在本例中的jerry

请注意,当我们需要绑定到函数的this值的特定对象时,必须显式设置方法和函数中的this值

解决方案

解决方案很容易使用正确的上下文

i、 我们希望这是指jerry,因此在使用jerry对象的sayMoo方法调用doAction方法时,使用绑定函数调用并传递jerry作为此参数

doAction(jerry.sayMoo.bind(jerry));
问题是,这个关键字在一个方法中使用,我们从一个接收方对象调用该方法,但它并没有绑定到我们希望它绑定到的对象,即在本例中的jerry

请注意,当我们需要绑定到函数的this值的特定对象时,必须显式设置方法和函数中的this值

解决方案

解决方案很容易使用正确的上下文

i、 我们希望这是指jerry,因此在使用jerry对象的sayMoo方法调用doAction方法时,使用绑定函数调用并传递jerry作为此参数

doAction(jerry.sayMoo.bind(jerry));

正如@Mark_M所说的,更好的方法是使用arrow函数,但是这个特性迫使您使用ES6而不是ES5,因为它引入了ES5。 另一种方法是使用可变范围的好处,如下所示:

        function cow() {
            this.timesMooed = 0;
            var that = this;
            this.sayMoo = function (){
                that.timesMooed++;
                return "moo";
            }
        }

        ...

正如@Mark_M所说的,更好的方法是使用arrow函数,但是这个特性迫使您使用ES6而不是ES5,因为它引入了ES5。 另一种方法是使用可变范围的好处,如下所示:

        function cow() {
            this.timesMooed = 0;
            var that = this;
            this.sayMoo = function (){
                that.timesMooed++;
                return "moo";
            }
        }

        ...

尝试创建一个构造函数

类{ 建造师{ this.timesMooed=; } 赛慕{ 这个.timesMooed+=1; 返回moo; } } var actionResults=[]; 函数doActionaction{ actionResults.pushaction; } var jerry=新奶牛; 杰瑞·赛穆多; console.logactionResults;
console.logjerry.timesMooed 尝试创建一个构造函数

类{ 建造师{ this.timesMooed=; } 赛慕{ 这个.timesMooed+=1; 返回moo; } } var actionResults=[]; 函数doActionaction{ actionResults.pushaction; } var jerry=新奶牛; 杰瑞·赛穆多; console.logactionResults;
console.logjerry.timesMooed;doActionjerry.sayMoo.bindjerry;这个问题已经被问了几十次,甚至几百次了。更努力地搜索和研究;这个问题已经被问了几十次,甚至几百次了。更努力地搜索和研究。精彩的答案!这正是我想要的深度;非常感谢你给出了一个清晰而彻底的答案!回答得真好!这正是我想要的深度;非常感谢你给出了一个清晰而彻底的答案!这是好的,除了1它是不必要的,2它解决了OP的问题,他想要的是推动函数调用本身,稍后用正确的方法执行,这不是
函数调用。这是好的,除了1它是不必要的,2它解决了OP的问题,他想要的是推动函数调用本身,稍后用正确的方法执行,而不是函数调用的结果。