102

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.

4

5 回答 5

160

在 JavaScript 中,大多数函数都是可调用和可实例化的:它们同时具有[[Call]][[Construct]]内部方法。

作为可调用对象,您可以使用括号来调用它们,可选地传递一些参数。作为调用的结果,函数可以返回一个值

var player = makeGamePlayer("John Smith", 15, 3);

上面的代码调用函数makeGamePlayer并将返回值存储在变量中player。在这种情况下,您可能希望像这样定义函数:

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

此外,当你调用一个函数时,你还在底层传递了一个额外的参数,它决定了this函数内部的值。在上述情况下,由于makeGamePlayer没有作为方法调用,因此this值将是草率模式下的全局对象,或严格模式下的 undefined。

作为构造函数,您可以使用new运算符来实例化它们。该运算符使用[[Construct]]内部方法(仅在构造函数中可用),它执行如下操作:

  1. 创建一个继承自.prototype构造函数的新对象
  2. 调用将这个对象作为this值传递的构造函数
  3. 如果它是一个对象,则返回构造函数返回的值,否则返回在步骤 1 中创建的对象。
var player = new GamePlayer("John Smith", 15, 3);

上面的代码创建了一个实例GamePlayer并将返回的值存储在变量中player。在这种情况下,您可能希望像这样定义函数:

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
}

按照惯例,构造函数名称以大写字母开头。

使用构造函数的优点是实例继承自GamePlayer.prototype. 然后,您可以在那里定义属性并使它们在所有实例中可用

于 2012-09-04T22:34:21.713 回答
53

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

function makeGamePlayer(name,totalScore,gamesPlayed) {
    return {
        name: name,
        totalscore: totalScore,
        gamesPlayed: gamesPlayed
    };
}
于 2012-09-04T22:33:10.387 回答
8

使用 ES2016 JavaScript 执行此操作的最新方法

let makeGamePlayer = (name, totalScore, gamesPlayed) => ({
    name,
    totalScore,
    gamesPlayed
})
于 2018-12-10T09:13:34.630 回答
5

两种风格,稍加调整,都会起作用。

第一种方法使用 Javascript 构造函数,它与大多数事物一样有利有弊。

 // 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);

另一方面,如果我没记错的话,您的另一种方法称为“显示闭包模式”。

function makePerson(name2, age2) {
  var name = name2;
  var age = age2;

  return {
    name: name,
    age: age
  };
}
于 2012-09-04T22:36:26.043 回答
3

我认为这些指示的意思是:

  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;
    }
于 2012-09-04T22:34:23.383 回答