gpt4 book ai didi

javascript - 有没有办法在 Javascript 中将多个函数分配给一个函数?

转载 作者:行者123 更新时间:2023-11-30 15:28:05 24 4
gpt4 key购买 nike

在 Javascript 中是否有可能在调用 1 个函数时调用分配给它的多个函数?

下面是我想做的:

var player1= {name: "Chris", score: 1000, rank: 1};
var player2= {name: "Kristofer", score: 100000, rank: 2};

function playerDetails(){
alert("The name of the player is "+ this.name + "."+ " His score is : "+ this.score + "and his rank : "+ this.rank);
}

function playerDetailsSecond()
{
//Do something else
}

player1.logDetails= playerDetails;
player2.logDetails= playerDetails;

//Is it possible to do this?
player1.logDetails += playerDetailsSecond;
player2.logDetails += playerDetailsSecond;

player1.logDetails(); //calls both playerDetails and playerDetailsSecond
player2.logDetails(); //calls both playerDetails and playerDetailsSecond

+= 特别是我想要做的。

我知道一个选择是做类似的事情:

function combined()
{
playerDetails();
playerDetailsSecond();
}

player1.logDetails = combined;

但我希望看看第一个是否是查看可能的语法/用例的选项。

谢谢!

最佳答案

更新

我已经编写了一个更抽象的语法来实现相同的功能。查看以前的答案以获得解释。

var player1 = { name: "Chris", score: 1000, rank: 1 };

function playerDetails(arg1, arg2) {
console.log("The name of the player is " + this.name + "." + " His score is : " + this.score + "and his rank : " + this.rank + arg1 + arg2);

//make sure you add this line at the end
arguments.callee.invokeExtras(this, arguments);
}


//you do not need to look into this code.
// this code adds functionality of pushing extra functions.
playerDetails.addFunction = function(fn) {
this.extraFns?this.extraFns.push(fn):this.extraFns = [fn];
}
playerDetails.invokeExtras = function(context, args) {
this.extraFns?this.extraFns.forEach(function(fn) { fn.apply(context, args) }):null;
}
//upto here

function playerDetailsSecond(arg1, arg2) {
console.log("The name of the player is " + this.name + arg1 + arg2);
}

function playerDetailsThird(arg1, arg2) {
console.log("new extra funtion " + this.name + arg1 + arg2);
}


player1.logDetails = playerDetails;

player1.logDetails.addFunction(playerDetailsSecond);
player1.logDetails.addFunction(playerDetailsThird);

player1.logDetails("test1", "test2");

上一个答案

这就是您实现它的方法。这里的概念是将函数视为一流对象,这意味着函数可以具有属性。

现在您要拥有的第一个函数必须在末尾有两行额外的代码。

 var self = this;
arguments.callee.extraFns.forEach(function (extraFunction) { extraFunction.call(self) });

在函数声明之后,您需要向函数添加一个空数组extraFns

完整代码如下:

var player1 = { name: "Chris", score: 1000, rank: 1 };

function playerDetails() {
console.log("The name of the player is " + this.name + "." + " His score is : " + this.score + "and his rank : " + this.rank);

var self = this;
arguments.callee.extraFns.forEach(function (extraFunction) { extraFunction.call(self) });
}

playerDetails.extraFns = [];

function playerDetailsSecond() {
console.log("The name of the player is " + this.name);
}

function playerDetailsThird() {
console.log("new extra funtion " + this.name);
}


player1.logDetails = playerDetails;

player1.logDetails.extraFns.push(playerDetailsSecond);
player1.logDetails.extraFns.push(playerDetailsThird);

player1.logDetails();

让我知道这是否解决了您想要做的事情,或者您是否不明白这里的任何内容。

关于javascript - 有没有办法在 Javascript 中将多个函数分配给一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42662110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com