JavaScript:返回对象的函数 [英] JavaScript: function returning an object

查看:212
本文介绍了JavaScript:返回对象的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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;
}

或类似的东西

 //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中,大多数函数都是可调用的和可实例化的:它们都有一个 [[Call]] [[Construct]] 内部方法。

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

上面的代码调用函数 makeGamePlayer 和商店变量 player 中返回的值。在这种情况下,您可能希望定义如下函数:

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;
}

此外,当你调用一个函数时,你也传递了一个额外的参数。引擎盖,它决定了 this 功能内部。在上面的例子中,由于 makeGamePlayer 未被调用为方法,值将是草率的全局对象模式,或在严格模式下未定义。

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.

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

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. 创建一个继承自<$ c的新对象$ c> .prototype 构造函数

  2. 调用传递此对象的构造函数为 this value

  3. 如果是对象,则返回构造函数返回的值,否则返回步骤1中创建的对象。

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

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

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.

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

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

这篇关于JavaScript:返回对象的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆