Javascript 从JS中的其他对象借用函数
我有以下代码:Javascript 从JS中的其他对象借用函数,javascript,Javascript,我有以下代码: var gameController = { scores: [20, 34, 55, 46, 77], avgScore: 112, players: [ {name: "Ruth", playerId: 100, age: 22}, {name: "Shawnee", playerId: 101, age: 21} ] }; var appController = { scores: [900, 845,
var gameController = {
scores: [20, 34, 55, 46, 77],
avgScore: 112,
players: [
{name: "Ruth", playerId: 100, age: 22},
{name: "Shawnee", playerId: 101, age: 21}
]
};
var appController = {
scores: [900, 845, 809, 950],
avgScore: null,
avg: function () {
var sumOfScores = this.scores.reduce(function (prev, cur, index, array) {
return prev + cur;
});
this.avgScore = sumOfScores / this.scores.length;
}
};
gameController.avgScore = appController.avg();
console.log(gameController.avgScore);
我尝试借用appController中定义的avg方法来为gameController进行计算。我理解在
gameController.avgScore=appController.avg()之后
,avg方法中的这个关键字仍然指向appController,因为appController调用了avg方法,所以我希望gameController中的avgScore应该保持不变,但输出未定义,为什么?avg
不返回任何内容,所以它隐式返回未定义的。你基本上是在做gameController.avgScore=undefined代码>
如果要将avg
方法应用于gameController
,可以使用:
最好将avg
作为一个独立函数,接受一个数字数组作为输入(参数)并返回平均值。avg
不返回任何内容,因此它隐式返回未定义的。你基本上是在做gameController.avgScore=undefined代码>
如果要将avg
方法应用于gameController
,可以使用:
最好将avg
作为一个独立函数,接受一个数字数组作为输入(参数)并返回平均值。avg
不返回任何内容,因此它隐式返回未定义的。你基本上是在做gameController.avgScore=undefined代码>
如果要将avg
方法应用于gameController
,可以使用:
最好将avg
作为一个独立函数,接受一个数字数组作为输入(参数)并返回平均值。avg
不返回任何内容,因此它隐式返回未定义的。你基本上是在做gameController.avgScore=undefined代码>
如果要将avg
方法应用于gameController
,可以使用:
最好将avg
作为一个独立函数,接受一个数字数组作为输入(参数)并返回平均值。更新:
将函数指定给对象时,调用函数时,会将this关键字设置为对象。这仅适用于未绑定的函数引用。如果函数引用绑定到另一个对象,则必须使用function.bind()
方法来确保将其设置为正确的对象
回答
与其每次都使用call,不如将函数分配给对象。当调用函数时,此
设置为对象
gameController.avg = appController.avg; // unbound function reference.
// now when you need to get the average for gameController
// just call its avg function
gameController.avg(); // this is automatically set to gameControler
最好还是在控制器外部创建函数,并在创建时分配它们
// define the avg and refer it to the unbound function that you will share
var avg = function () {
var sumOfScores = this.scores.reduce(function (prev, cur, index, array) {
return prev + cur;
});
this.avgScore = sumOfScores / this.scores.length;
console.log(this.avgScore);
}
// create the objects as normal
var gameController = {
scores: [20, 34, 55, 46, 77],
avgScore: 112,
players: [
{name: "Ruth", playerId: 100, age: 22},
{name: "Shawnee", playerId: 101, age: 21}
],
avg:avg // avg is automatically bound to gameController when it is called
};
var appController = {
scores: [900, 845, 809, 950],
avgScore: null,
avg:avg // avg is automatically bound to appController when it is called
};
// or if you want to get really lazy.
var otherController = {
scores: [900, 900, 900, 900],
avgScore: null,
avg // avg is automatically bound to otherController when it is called
// and it is automatically named avg as well
};
appController.avg(); // 46.4
gameController.avg(); // 876
otherController.avg(); // 900
还有六种以上的其他方法可以达到同样的效果。更新:
将函数指定给对象时,调用函数时,会将this关键字设置为对象。这仅适用于未绑定的函数引用。如果函数引用绑定到另一个对象,则必须使用function.bind()
方法来确保将其设置为正确的对象
回答
与其每次都使用call,不如将函数分配给对象。当调用函数时,此
设置为对象
gameController.avg = appController.avg; // unbound function reference.
// now when you need to get the average for gameController
// just call its avg function
gameController.avg(); // this is automatically set to gameControler
最好还是在控制器外部创建函数,并在创建时分配它们
// define the avg and refer it to the unbound function that you will share
var avg = function () {
var sumOfScores = this.scores.reduce(function (prev, cur, index, array) {
return prev + cur;
});
this.avgScore = sumOfScores / this.scores.length;
console.log(this.avgScore);
}
// create the objects as normal
var gameController = {
scores: [20, 34, 55, 46, 77],
avgScore: 112,
players: [
{name: "Ruth", playerId: 100, age: 22},
{name: "Shawnee", playerId: 101, age: 21}
],
avg:avg // avg is automatically bound to gameController when it is called
};
var appController = {
scores: [900, 845, 809, 950],
avgScore: null,
avg:avg // avg is automatically bound to appController when it is called
};
// or if you want to get really lazy.
var otherController = {
scores: [900, 900, 900, 900],
avgScore: null,
avg // avg is automatically bound to otherController when it is called
// and it is automatically named avg as well
};
appController.avg(); // 46.4
gameController.avg(); // 876
otherController.avg(); // 900
还有六种以上的其他方法可以达到同样的效果。更新:
将函数指定给对象时,调用函数时,会将this关键字设置为对象。这仅适用于未绑定的函数引用。如果函数引用绑定到另一个对象,则必须使用function.bind()
方法来确保将其设置为正确的对象
回答
与其每次都使用call,不如将函数分配给对象。当调用函数时,此
设置为对象
gameController.avg = appController.avg; // unbound function reference.
// now when you need to get the average for gameController
// just call its avg function
gameController.avg(); // this is automatically set to gameControler
最好还是在控制器外部创建函数,并在创建时分配它们
// define the avg and refer it to the unbound function that you will share
var avg = function () {
var sumOfScores = this.scores.reduce(function (prev, cur, index, array) {
return prev + cur;
});
this.avgScore = sumOfScores / this.scores.length;
console.log(this.avgScore);
}
// create the objects as normal
var gameController = {
scores: [20, 34, 55, 46, 77],
avgScore: 112,
players: [
{name: "Ruth", playerId: 100, age: 22},
{name: "Shawnee", playerId: 101, age: 21}
],
avg:avg // avg is automatically bound to gameController when it is called
};
var appController = {
scores: [900, 845, 809, 950],
avgScore: null,
avg:avg // avg is automatically bound to appController when it is called
};
// or if you want to get really lazy.
var otherController = {
scores: [900, 900, 900, 900],
avgScore: null,
avg // avg is automatically bound to otherController when it is called
// and it is automatically named avg as well
};
appController.avg(); // 46.4
gameController.avg(); // 876
otherController.avg(); // 900
还有六种以上的其他方法可以达到同样的效果。更新:
将函数指定给对象时,调用函数时,会将this关键字设置为对象。这仅适用于未绑定的函数引用。如果函数引用绑定到另一个对象,则必须使用function.bind()
方法来确保将其设置为正确的对象
回答
与其每次都使用call,不如将函数分配给对象。当调用函数时,此
设置为对象
gameController.avg = appController.avg; // unbound function reference.
// now when you need to get the average for gameController
// just call its avg function
gameController.avg(); // this is automatically set to gameControler
最好还是在控制器外部创建函数,并在创建时分配它们
// define the avg and refer it to the unbound function that you will share
var avg = function () {
var sumOfScores = this.scores.reduce(function (prev, cur, index, array) {
return prev + cur;
});
this.avgScore = sumOfScores / this.scores.length;
console.log(this.avgScore);
}
// create the objects as normal
var gameController = {
scores: [20, 34, 55, 46, 77],
avgScore: 112,
players: [
{name: "Ruth", playerId: 100, age: 22},
{name: "Shawnee", playerId: 101, age: 21}
],
avg:avg // avg is automatically bound to gameController when it is called
};
var appController = {
scores: [900, 845, 809, 950],
avgScore: null,
avg:avg // avg is automatically bound to appController when it is called
};
// or if you want to get really lazy.
var otherController = {
scores: [900, 900, 900, 900],
avgScore: null,
avg // avg is automatically bound to otherController when it is called
// and it is automatically named avg as well
};
appController.avg(); // 46.4
gameController.avg(); // 876
otherController.avg(); // 900
还有六种以上的其他方法可以实现同样的效果。不需要绑定<代码>gameController.avg=appController.avg代码>也可以。“avg自动绑定到gameController”请注意,这些函数实际上并没有“绑定”。@Felix Kling您是对的,不,需要绑定。但MDN建议它们是绑定的“在下面的示例中,当调用o.f()(作为对象方法)时,在函数内部,这是绑定到o对象的。”但我将进一步研究并确定绑定的语义含义,以及它与“函数引用”和“函数调用”的关系。我会相应地更新我的答案。谢谢你的提醒。好的。。因此正确地说,调用函数调用时,这个
可以说是绑定的。对于对象调用函数时,此
自动绑定,除非函数引用绑定到另一个对象。绑定函数是指通过函数显式绑定的方法。bind()
,或'this.func=function(){}',或??如此正确