Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: function returning an object

Tags:

javascript

I'm taking some JavaScript/jQuery lessons at codecademy.com. 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.

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

like image 304
BrainLikeADullPencil Avatar asked Sep 04 '12 22:09

BrainLikeADullPencil


People also ask

Can a JavaScript function return an object?

JavaScript doesn't support functions that return multiple values. However, you can wrap multiple values into an array or an object and return the array or the object. Use destructuring assignment syntax to unpack values from the array, or properties from objects.

Can we return an object from a function?

A function can also return objects either by value or by reference. When an object is returned by value from a function, a temporary object is created within the function, which holds the return value. This value is further assigned to another object in the calling function.

Can you return an object from a function give an example?

Returning Object as argumentobject = return object_name; Example: In the above example we can see that the add function does not return any value since its return-type is void. In the following program the add function returns an object of type 'Example'(i.e., class name) whose value is stored in E3.


1 Answers

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

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. 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. 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.

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

  1. Creates a new object which inherits from the .prototype of the constructor
  2. Calls the constructor passing this object as the this value
  3. It returns the value returned by the constructor if it's an object, or the object created at step 1 otherwise.
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. 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. Then, you can define properties there and make them available in all instances

like image 144
Oriol Avatar answered Sep 20 '22 18:09

Oriol