JavaScript:使用函数返回的对象而没有对象的对象

时间:2017-08-16 02:24:05

标签: javascript jquery

我是JavaScript / jQuery的新手。想知道是否有更好的方法通过函数返回对象。

//this is the Object creator.
function makeNewPlayer(name, score) {
    var player = {};
    player.name = name;
    player.score = score;

    return player;
};

这是我使用该功能的方式。

var player_info = makeNewPlayer('Bob' ,100);

然而,当我使用该对象时,我需要像这样调用它:

player_info.player.name
player_info.player.score

它看起来很愚蠢,任何直接使用这个对象的方式都可以吗?

player_info.name
player_info.score

修改

因为我不知道该函数将创建多少个对象。 假设有一个for循环来获得score_1,score_2等。

function makeNewPlayer(name, score) {
    var player = {};
    player.name = name;

    for(i=0, i<number_of_score, i++){
        eval("player.score_" + i) = score[i];
    };

    return player;
};

6 个答案:

答案 0 :(得分:1)

您可以使用销毁赋值来声明全局或本地范围的变量

&#13;
&#13;
//this is the Object creator.
function makeNewPlayer(name, score) {
  var player = {};
  player.name = name
  player.score = score

  return player;
};

var player_info = makeNewPlayer("Bob", 100);

{
  let {name: _name, score} = player_info;
  // do stuff with `_name`, `score`
  console.log(_name, score);
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

function makeNewPlayer(name, score) {
   return {name, score};
};

let players = [];
// in for loop -- sample code only, you need to supply name & score
{
   players.push(makeNewPlayer(name, score));
}

答案 2 :(得分:0)

除了&#34; Bob&#34;应该在引号中,它对我来说很好。

protected override void OnResume()
{
     base.OnResume();
     SetRecyclerAdaptor();
}

private void SetRecyclerAdaptor()
{
     mainMenuRecyclerAdapter = new MainMenuRecyclerAdapter(lstMenu);
     mainMenuRecyclerAdapter.ItemClick += (sender, e) =>
     {
          Toast.MakeText(this,"Click happened here!",ToastLength.Short).Show();
     };

     recyclerView.SetAdapter(mainMenuRecyclerAdapter);
}

然后我可以像这样访问它:

var player_info = makeNewPlayer('Bob', 100);

答案 3 :(得分:0)

使用new

我从my blog复制了一些可以解决您疑惑的文字:

  

构造函数是用于创建对象的特殊函数:

     

示例:

function StarkIndustries(name, birth) {
  this.name = name;
  this.birth = birth;
}
var iron1 = new StarkIndustries('iron01', 2017);
var iron2 = new StarkIndustries('iron02', 2017);
alert(iron1.name);
alert(iron2.name);
  

结果:

iron01
iron02
  

通过在函数之前添加new,函数变为a   构造对象的构造函数。

但是调用一个设计为构造函数的函数是危险的 没有new,因为它会修改全局对象window。因此需要在构造函数内部进行健全性检查:

if (!(this instanceof StarkIndustries)) {
  warn("StarkIndustries is a constructor and should be called with `new`");
}

回到你的代码,它应该是:

function makeNewPlayer(name, score) {
    this.name = name
    this.score = score
};

var iron1 = new makeNewPlayer('iron01', 2017);

答案 4 :(得分:0)

您可以在函数内部使用临时构造函数:

function makeNewPlayer(name, score){
  const Player = function(name, score){
    this.name = name;
    this.score = score;
  };

  return new Player(name, score);
}

您也可以使用外部函数而不是嵌套函数。

答案 5 :(得分:0)

如果您的功能没有变化,我可以按您希望的方式获得输出

player_info.name
player_info.score

查看下面的代码段

//this is the Object creator.
function makeNewPlayer(name, score) {
  var player = {};
  player.name = name
  player.score = score

  return player;
};

var player_info = makeNewPlayer("Bob", 100);
console.log(player_info.name);
console.log(player_info.score);