簡體   English   中英

JavaScript:返回對象的函數

[英]JavaScript: function returning an object

我正在codecademy.com上學習一些JavaScript / jQuery課程。 通常課程提供答案或提示,但對於這個課程,它沒有給出任何幫助,我對說明有點困惑。

它說使makeGamePlayer函數返回一個帶有三個鍵的對象。

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

我不確定我是否應該這樣做

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

我必須能夠在創建對象后修改它的屬性。

在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值將是sloppy模式下的全局對象,或者是嚴格模式下的undefined。

作為構造函數,您可以使用new運算符來實例化它們。 此運算符使用[[Construct]]內部方法(僅在構造函數中可用),其執行如下操作:

  1. 創建一個繼承自構造函數的.prototype的新對象
  2. 調用將此對象作為this值傳遞的構造函數
  3. 如果構造函數是對象,則返回構造函數返回的值,否則返回在步驟1中創建的對象。
var player = new GamePlayer("John Smith", 15, 3);

上面的代碼創建了一個GamePlayer實例, 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 然后,您可以在那里定義屬性並使其在所有實例中可用

您只需使用對象文字就可以這樣做:

function makeGamePlayer(name,totalScore,gamesPlayed) {
    return {
        name: name,
        totalscore: totalScore,
        gamesPlayed: gamesPlayed
    };
}

這兩種風格都有一定的調整作用。

第一種方法使用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
  };
}

我會把這些指示意思是:

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

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

let makeGamePlayer = (name, totalScore, gamesPlayed) => ({
    name,
    totalScore,
    gamesPlayed
})
const upadteAgeAndCount = async (id,age) => {
    const user = await User.findByIdAndUpdate(id,{age},{new:true})
    const count = await User.countDocuments({age})
    return ({user,count})
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM