[removed] function returning an object

前端 未结 5 1185
一个人的身影
一个人的身影 2020-12-07 08:36

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 con

相关标签:
5条回答
  • 2020-12-07 08:50

    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;
        }
    
    0 讨论(0)
  • 2020-12-07 08:51

    Both styles, with a touch of tweaking, would work.

    The first method uses a Javascript Constructor, which like most things has 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 recall correctly.

    function makePerson(name2, age2) {
      var name = name2;
      var age = age2;
    
      return {
        name: name,
        age: age
      };
    }
    
    0 讨论(0)
  • 2020-12-07 08:52

    The latest way to do this with ES2016 JavaScript

    let makeGamePlayer = (name, totalScore, gamesPlayed) => ({
        name,
        totalScore,
        gamesPlayed
    })
    
    0 讨论(0)
  • 2020-12-07 09:03

    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

    0 讨论(0)
  • 2020-12-07 09:11

    You can simply do it like this with an object literal:

    function makeGamePlayer(name,totalScore,gamesPlayed) {
        return {
            name: name,
            totalscore: totalScore,
            gamesPlayed: gamesPlayed
        };
    }
    
    0 讨论(0)
提交回复
热议问题