JavaScript:返回对象的函数

Bra*_*cil 69 javascript

我正在codecademy.com上学习一些JavaScript/jQuery课程.通常课程提供答案或提示,但对于这个课程,它没有给出任何帮助,我对说明有点困惑.

它说使makeGamePlayer函数返回一个带有三个键的对象.

//First, the object creator
function makeGamePlayer(name,totalScore,gamesPlayed) {
    //should return an object with three keys:
    // name
    // totalScore
    // gamesPlayed
}
Run Code Online (Sandbox Code Playgroud)

我不确定我是否应该这样做

//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;
}
Run Code Online (Sandbox Code Playgroud)

或类似的东西

 //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;
          }
    }
Run Code Online (Sandbox Code Playgroud)

我必须能够在创建对象后修改它的属性.

Ori*_*iol 119

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

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

var player = makeGamePlayer("John Smith", 15, 3);
Run Code Online (Sandbox Code Playgroud)

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

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

另外,当你调用一个函数时,你也会在引擎盖下传递一个额外的参数,它决定this了函数内部的值.在上面的例子中,由于makeGamePlayer未作为方法调用,因此该this值将是sloppy模式下的全局对象,或者是严格模式下的undefined.

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

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

上面的代码创建了一个实例,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
}
Run Code Online (Sandbox Code Playgroud)

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

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

  • @OP还要注意,当你打算用`new`关键字调用它时,我建议用一个大写:`MakeGamePlayer`来启动它. (3认同)
  • @PeeHaa很好的建议,使用构造函数时更典型的命名约定是`new GamePlayer()`. (3认同)
  • @Oriol-检查你的对象文字,它有语法错误. (2认同)

Pee*_*Haa 41

您只需使用对象文字就可以这样做:

function makeGamePlayer(name,totalScore,gamesPlayed) {
    return {
        name: name,
        totalscore: totalScore,
        gamesPlayed: gamesPlayed
    };
}
Run Code Online (Sandbox Code Playgroud)

  • 您好,您如何访问返回属性,当我使用 makeGamePlayer.name 时它不起作用 (3认同)
  • var player1 = makeGamePlayer("鲍比·费舍尔", 15, 5); 玩家1.姓名; (2认同)

Rob*_*Rob 7

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

let makeGamePlayer = (name, totalScore, gamesPlayed) => ({
    name,
    totalScore,
    gamesPlayed
})
Run Code Online (Sandbox Code Playgroud)


Jer*_*her 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);
Run Code Online (Sandbox Code Playgroud)

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

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

  return {
    name: name,
    age: age
  };
}
Run Code Online (Sandbox Code Playgroud)