JavaScript: function returning an object

I am doing some JavaScript / jQuery tutorials at codecademy.com. Usually the lessons give answers or tips, but for this he does not give any help, and I'm a little confused by the instructions.

It says that the makeGamePlayer function returns an object with three keys.

//First, the object creator function makeGamePlayer(name,totalScore,gamesPlayed) { //should return an object with three keys: // name // totalScore // gamesPlayed } 

I'm not sure I should do 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 that

  //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 should be able to change the properties of an object after it is created.

+64
javascript
Sep 04 '12 at 22:30
source share
6 answers

In JavaScript, most functions are callable and realistic: they have both [[Call]] and [[Construct]] internal methods.

As called 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 above code calls the makeGamePlayer function and stores the return value in the player variable. In this case, you can define the function as follows:

 function makeGamePlayer(name, totalScore, gamesPlayed) { // Define desired object var obj = { name: name, totalScore: totalScore, gamesPlayed: gamesPlayed }; // Return it return obj; } 

In addition, when calling a function, you also pass an additional argument under the hood, which defines the value of this inside the function. In the above case, since makeGamePlayer not called as a method, this value will be a global object in inactive mode or undefined in strict mode.

As constructors, you can use the new operator to create them. This statement uses the internal [[Construct]] method (available only in constructors), which does something like this:

  • Creates a new object that inherits from the .prototype constructor
  • Invokes a constructor that passes this object as the value of this
  • Returns the value returned by the constructor if it is an object or an object created in step 1 otherwise.
 var player = new GamePlayer("John Smith", 15, 3); 

The above code creates an instance of GamePlayer and stores the return value in the player variable. In this case, you can define the function as follows:

 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, the names of the constructors begin with a capital letter.

The advantage of using constructors is that instances inherit from GamePlayer.prototype . Then you can define the properties there and make them available in all instances.

+110
Sep 04
source share

You can simply do this with an object literal :

 function makeGamePlayer(name,totalScore,gamesPlayed) { return { name: name, totalscore: totalScore, gamesPlayed: gamesPlayed }; } 
+37
Sep 04 '12 at 22:33
source share

Both styles, with easy customization, will work.

The first method uses the Javascript Constructor, which, like most things, has its pros and cons.

  // 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 remember correctly.

 function makePerson(name2, age2) { var name = name2; var age = age2; return { name: name, age: age }; } 
+5
04 Sep
source share

I would call these directions:

  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; } 
+2
04 Sep
source share

The latest way to do this with ES2016 JavaScript

 let makeGamePlayer = (name, totalScore, gamesPlayed) => ({ name, totalScore, gamesPlayed }) 
+2
Dec 10 '18 at 9:13
source share
 const upadteAgeAndCount = async (id,age) => { const user = await User.findByIdAndUpdate(id,{age},{new:true}) const count = await User.countDocuments({age}) return ({user,count}) } 
0
Jul 15 '19 at 15:31
source share



All Articles