[英]JavaScript: function returning an object
I'm taking some JavaScript/jQuery lessons at codecademy.com. 我正在codecademy.com上学习一些JavaScript / jQuery课程。 Normally the lessons provide answers or hints, but for this one it doesn't give any help and I'm a little confused by the instructions.
通常课程提供答案或提示,但对于这个课程,它没有给出任何帮助,我对说明有点困惑。
It says to make the function makeGamePlayer return an object with three keys. 它说使makeGamePlayer函数返回一个带有三个键的对象。
//First, the object creator
function makeGamePlayer(name,totalScore,gamesPlayed) {
//should return an object with three keys:
// name
// totalScore
// gamesPlayed
}
I'm not sure if i should be doing this 我不确定我是否应该这样做
//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;
}
or something like this 或类似的东西
//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;
}
}
I have to be able to modify the properties of the object after its created. 我必须能够在创建对象后修改它的属性。
In JavaScript, most functions are both callable and instantiable: they have both a [[Call]] and [[Construct]] internal methods. 在JavaScript中,大多数函数都是可调用的和可实例化的:它们同时具有[[Call]]和[[Construct]]内部方法。
As callable objects, you can use parentheses to call them, optionally passing some arguments. 作为可调用对象,您可以使用括号来调用它们,可选地传递一些参数。 As a result of the call, the function can return a value .
作为调用的结果,该函数可以返回一个值 。
var player = makeGamePlayer("John Smith", 15, 3);
The code above calls function makeGamePlayer
and stores the returned value in the variable player
. 上面的代码调用函数
makeGamePlayer
并将返回的值存储在变量player
。 In this case, you may want to define the function like this: 在这种情况下,您可能希望定义如下函数:
function makeGamePlayer(name, totalScore, gamesPlayed) {
// Define desired object
var obj = {
name: name,
totalScore: totalScore,
gamesPlayed: gamesPlayed
};
// Return it
return obj;
}
Additionally, when you call a function you are also passing an additional argument under the hood, which determines the value of this
inside the function. 此外,当调用函数你也通过一个额外的参数的发动机罩,其确定的值下
this
里面的功能。 In the case above, since makeGamePlayer
is not called as a method, the this
value will be the global object in sloppy mode, or undefined in strict mode. 在上面的例子中,由于
makeGamePlayer
不是作为方法调用的,因此this
值将是sloppy模式下的全局对象,或者是严格模式下的undefined。
As constructors, you can use the new
operator to instantiate them. 作为构造函数,您可以使用
new
运算符来实例化它们。 This operator uses the [[Construct]] internal method (only available in constructors), which does something like this: 此运算符使用[[Construct]]内部方法(仅在构造函数中可用),其执行如下操作:
.prototype
of the constructor .prototype
的新对象 this
value this
值传递的构造函数 var player = new GamePlayer("John Smith", 15, 3);
The code above creates an instance of GamePlayer
and stores the returned value in the variable player
. 上面的代码创建了一个
GamePlayer
实例, GamePlayer
返回的值存储在变量player
。 In this case, you may want to define the function like this: 在这种情况下,您可能希望定义如下函数:
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
}
By convention, constructor names begin with an uppercase letter. 按照惯例,构造函数名称以大写字母开头。
The advantage of using constructors is that the instances inherit from GamePlayer.prototype
. 使用构造函数的优点是实例继承自
GamePlayer.prototype
。 Then, you can define properties there and make them available in all instances 然后,您可以在那里定义属性并使其在所有实例中可用
You can simply do it like this with an object literal : 您只需使用对象文字就可以这样做:
function makeGamePlayer(name,totalScore,gamesPlayed) {
return {
name: name,
totalscore: totalScore,
gamesPlayed: gamesPlayed
};
}
Both styles, with a touch of tweaking, would work. 这两种风格都有一定的调整作用。
The first method uses a Javascript Constructor, which like most things has pros and cons. 第一种方法使用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);
On the other hand, your other method is called the 'Revealing Closure Pattern' if I recall correctly. 另一方面,如果我没记错的话,你的另一种方法被称为'揭示封闭模式'。
function makePerson(name2, age2) {
var name = name2;
var age = age2;
return {
name: name,
age: age
};
}
I would take those directions to mean: 我会把这些指示意思是:
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;
}
The latest way to do this with ES2016 JavaScript 使用ES2016 JavaScript执行此操作的最新方法
let makeGamePlayer = (name, totalScore, gamesPlayed) => ({
name,
totalScore,
gamesPlayed
})
const upadteAgeAndCount = async (id,age) => {
const user = await User.findByIdAndUpdate(id,{age},{new:true})
const count = await User.countDocuments({age})
return ({user,count})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.