Home:ALL Converter>JavaScript: function returning an object

JavaScript: function returning an object

Ask Time:2012-09-05T06:30:38         Author:BrainLikeADullPencil

Json Formatter

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.

Author:BrainLikeADullPencil,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/12272239/javascript-function-returning-an-object
Oriol :

In JavaScript, most functions are both callable and instantiable: they have both a [[Call]] and [[Construct]] internal methods.\n\nAs 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.\n\nvar player = makeGamePlayer(\"John Smith\", 15, 3);\n\n\nThe 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:\n\nfunction makeGamePlayer(name, totalScore, gamesPlayed) {\n // Define desired object\n var obj = {\n name: name,\n totalScore: totalScore,\n gamesPlayed: gamesPlayed\n };\n // Return it\n return obj;\n}\n\n\nAdditionally, 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.\n\nAs 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:\n\n\nCreates a new object which inherits from the .prototype of the constructor\nCalls the constructor passing this object as the this value\nIt returns the value returned by the constructor if it's an object, or the object created at step 1 otherwise.\n\n\nvar player = new GamePlayer(\"John Smith\", 15, 3);\n\n\nThe 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:\n\nfunction GamePlayer(name,totalScore,gamesPlayed) {\n // `this` is the instance which is currently being created\n this.name = name;\n this.totalScore = totalScore;\n this.gamesPlayed = gamesPlayed;\n // No need to return, but you can use `return this;` if you want\n}\n\n\nBy convention, constructor names begin with an uppercase letter.\n\nThe 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",
2012-09-04T22:34:21
PeeHaa :

You can simply do it like this with an object literal:\n\nfunction makeGamePlayer(name,totalScore,gamesPlayed) {\n return {\n name: name,\n totalscore: totalScore,\n gamesPlayed: gamesPlayed\n };\n}\n",
2012-09-04T22:33:10
Rob :

The latest way to do this with ES2016 JavaScript\n\nlet makeGamePlayer = (name, totalScore, gamesPlayed) => ({\n name,\n totalScore,\n gamesPlayed\n})\n",
2018-12-10T09:13:34
Jeremy J Starcher :

Both styles, with a touch of tweaking, would work.\n\nThe first method uses a Javascript Constructor, which like most things has pros and cons.\n\n // By convention, constructors start with an upper case letter\nfunction MakePerson(name,age) {\n // The magic variable 'this' is set by the Javascript engine and points to a newly created object that is ours.\n this.name = name;\n this.age = age;\n this.occupation = \"Hobo\";\n}\nvar jeremy = new MakePerson(\"Jeremy\", 800);\n\n\nOn the other hand, your other method is called the 'Revealing Closure Pattern' if I recall correctly.\n\nfunction makePerson(name2, age2) {\n var name = name2;\n var age = age2;\n\n return {\n name: name,\n age: age\n };\n}\n",
2012-09-04T22:36:26
yy