问题描述
var player1 = {name:Chris,score:1000 ,等级:1};
var player2 = {name:Kristofer,得分:100000,等级:2};
function playerDetails(){
alert(玩家的名字是+ this.name +。+他的分数是:+ this.score +和他的等级:+ this.rank);
}
player1.logDetails = playerDetails;
player2.logDetails = playerDetails;
player1.logDetails();
player2.logDetails();
据我所知< player1.logDetails 是玩家1的属性或玩家1的方法。
所以我不明白作者如何给一个函数分配一个属性。
另外我不明白为什么你会这样写,而不是:
player1.logDetails = playerDetails();
我尝试过并且无法工作。
如果有人可以帮忙?
提前感谢您
如果代码是这样写的,我敢打赌你明白了:
var player1 = {
名称:Chris,
得分:1000,
排名:1 ,
playerDetails:function(){alert('The name is'+ this.name)}
};
var player2 = {
name:Kristofer,
得分:10000,
等级:2,
playerDetails:function(){alert( '这个名字是'+ this.name)}
};
代码的作者想要定义一次playerDetails()函数。
以简化方式显示此内容的另一种方式是:
var player1 = {
name:Chris,
score:1000,
rank:1
};
player1.playerDetails = function(){alert('name is'+ this.name)}
var player2 = {
name:Kristofer ,
得分:10000,
等级:2
};
player2.playerDetails = function(){alert('name is'+ this.name)}
所以,如果你想通过只写一次playerDetails函数来优化上面的代码,它看起来就像你的文章中的代码。
如果我写了代码块,我可能会这样写:(这很容易阅读)
function playerDetailsFunc( ){alert('name is'+ this.name)}
var player1 = {
name:Chris,
score:1000,
rank :1,
playerDetails:playerDetailsFunc
};
var player2 = {
name:Kristofer,
得分:10000,
等级:2,
playerDetails:playerDetailsFunc
} ;
I was looking and struggling to the following example:
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); } player1.logDetails= playerDetails; player2.logDetails= playerDetails; player1.logDetails(); player2.logDetails();
As far as I know player1.logDetails is a property of player1 or a method of player1.So I can't understand how the author assigns a property to a function.Also I don't get why you would write it like that instead of : player1.logDetails= playerDetails();which I have tried and doesn't work.
Then he calls player1.logDetails() which is a function but not declared anywhere.(?)
If anyone could help??Thank you in advance
If the code was written like this, I bet you understand it:
var player1 = { name: "Chris", score: 1000, rank: 1, playerDetails: function() { alert('The name is '+ this.name) } }; var player2 = { name: "Kristofer", score: 10000, rank: 2, playerDetails: function() { alert('The name is '+ this.name) } };
The author of the code wanted to define the "playerDetails()" function once.
Another way of showing this in a simplified manner is:
var player1 = { name: "Chris", score: 1000, rank: 1 }; player1.playerDetails=function() { alert('The name is '+ this.name) } var player2 = { name: "Kristofer", score: 10000, rank: 2 }; player2.playerDetails=function() { alert('The name is '+ this.name) }
So if you wanted to optimize the code above by only writing the playerDetails function once, it would look like the code in your post.
If I had written the code block, I might have written it like this: (which is easy to read)
function playerDetailsFunc() {alert('The name is '+ this.name) } var player1 = { name: "Chris", score: 1000, rank: 1, playerDetails: playerDetailsFunc }; var player2 = { name: "Kristofer", score: 10000, rank: 2, playerDetails: playerDetailsFunc };
这篇关于Javascript对象将属性赋值给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!