我正在codecademy.com上学习一些JavaScript / jQuery课程。通常课程提供答案或提示,但对于这个课程,它没有给出任何帮助,我对说明有点困惑。
它说使makeGamePlayer函数返回一个带有三个键的对象。
//First, the object creator
function makeGamePlayer(name,totalScore,gamesPlayed) {
//should return an object with three keys:
// name
// totalScore
// gamesPlayed
}
我不确定我是否应该这样做
//First, the object creator
function makeGamePlayer(name,totalScore,gamesPlayed) {
//should return an object with three keys:
// name
// totalScore
// gamesPlayed
this.name = name;
this.totalScore = totalScore;
this.gamesPlayed = gamesPlayed;
}
或类似的东西
//First, the object creator
function makeGamePlayer(name,totalScore,gamesPlayed) {
//should return an object with three keys:
// name
// totalScore
// gamesPlayed
var obj = {
this.name = name;
this.totalScore = totalScore;
this.gamesPlayed = gamesPlayed;
}
}
我必须能够在创建对象后修改它的属性。
答案 0 :(得分:104)
在JavaScript中,大多数functions都是可调用的和可实例化的:它们同时具有[[Call]]和[[Construct]]内部方法。
作为可调用对象,您可以使用括号来调用它们,也可以选择传递一些参数。作为调用的结果,函数可以return a value。
var player = makeGamePlayer("John Smith", 15, 3);
上面的代码调用函数makeGamePlayer
并将返回的值存储在变量player
中。在这种情况下,您可能希望定义如下函数:
function makeGamePlayer(name, totalScore, gamesPlayed) {
// Define desired object
var obj = {
name: name,
totalScore: totalScore,
gamesPlayed: gamesPlayed
};
// Return it
return obj;
}
此外,当你调用一个函数时,你也会在引擎盖下传递一个额外的参数,它决定了函数内this
的值。在上面的例子中,由于makeGamePlayer
不是作为方法调用的,this
值将是sloppy模式下的全局对象,或者是严格模式下的未定义。
作为构造函数,您可以使用new
operator来实例化它们。此运算符使用[[Construct]]内部方法(仅在构造函数中可用),其执行如下操作:
.prototype
的新对象this
值var player = new GamePlayer("John Smith", 15, 3);
上面的代码创建了GamePlayer
的实例,并将返回的值存储在变量player
中。在这种情况下,您可能希望定义如下函数:
function GamePlayer(name,totalScore,gamesPlayed) {
// `this` is the instance which is currently being created
this.name = name;
this.totalScore = totalScore;
this.gamesPlayed = gamesPlayed;
// No need to return, but you can use `return this;` if you want
}
按照惯例,构造函数名称以大写字母开头。
使用构造函数的优点是实例继承自GamePlayer.prototype
。然后,您可以在那里定义属性并使它们在所有实例中都可用
答案 1 :(得分:36)
你可以使用object literal
这样做function makeGamePlayer(name,totalScore,gamesPlayed) {
return {
name: name,
totalscore: totalScore,
gamesPlayed: gamesPlayed
};
}
答案 2 :(得分:4)
这两种风格都有一定的调整作用。
第一种方法使用Javascript构造函数,它与大多数东西一样有利有弊。
// By convention, constructors start with an upper case letter
function MakePerson(name,age) {
// The magic variable 'this' is set by the Javascript engine and points to a newly created object that is ours.
this.name = name;
this.age = age;
this.occupation = "Hobo";
}
var jeremy = new MakePerson("Jeremy", 800);
另一方面,如果我没记错的话,你的另一种方法叫做'揭示封闭模式'。
function makePerson(name2, age2) {
var name = name2;
var age = age2;
return {
name: name,
age: age
};
}
答案 3 :(得分:2)
我会采取这些指示:
function makeGamePlayer(name,totalScore,gamesPlayed) {
//should return an object with three keys:
// name
// totalScore
// gamesPlayed
var obj = { //note you don't use = in an object definition
"name": name,
"totalScore": totalScore,
"gamesPlayed": gamesPlayed
}
return obj;
}
答案 4 :(得分:1)
使用ES2016 JavaScript的最新方法
let makeGamePlayer = (name, totalScore, gamesPlayed) => ({
name,
totalScore,
gamesPlayed
})
答案 5 :(得分:0)
const upadteAgeAndCount = async (id,age) => {
const user = await User.findByIdAndUpdate(id,{age},{new:true})
const count = await User.countDocuments({age})
return ({user,count})
}