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.
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.
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.
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.
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:
.prototype
of the constructorthis
valuevar 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With