2017-05-24 90 views
0

請問誰能告訴我下面的代碼有什麼問題?我對javascript完全陌生,並且我知道某處存在缺少逗號。謝謝。什麼是我的JavaScript代碼的語法錯誤?

var cake = { 
 
    firstIngredient: "milk", 
 
    secondIngredient: "eggs", 
 
    thirdIngredient: "cakemix", 
 
    bakeTime: 22 
 
    bakeTemp: 420 
 
    mixingInstructions: function() { 
 
    return "Add " 
 
    this.firstIngredient + " to " + this.secondIngredient + " and stir with " + this.thirdIngredient + " and bake at " + bakeTemp + " for " + bakeTime + " minutes."; 
 
    } 
 
};

+1

如果你看到在你的開發者控制檯這個錯誤,你可以簡單地點擊鏈接的行號,它會帶你到確切的行的問題上。從那裏,你應該真的能夠看到那條線與它之前的線不同,它不會產生錯誤。 –

+0

缺失:22之後的逗號,420之後的逗號以及'「添加」之後的'+'符號。 –

回答

2

bakeTime和bakeTemp都失蹤後,他們的逗號。 mixInstructions功能也是如此,但它不是必需的。

+0

我明白了,但是我把它放到那裏後它仍然不能正常運行。 – user230517

+0

@ user230517「正常運行」是什麼意思?你的問題是語法錯誤,Stephen L向你解釋了這個錯誤。 – nem035

+0

更具體地說,數字22和420.我添加了逗號,它的工作原理。 –

2

你剛纔bakeTimebakeTemp後失蹤逗號,和你在你的函數這裏" and bake at " + bakeTemp + " for " + bakeTime失蹤this

var cake = { 
 
    firstIngredient: "milk", 
 
    secondIngredient: "eggs", 
 
    thirdIngredient: "cakemix", 
 
    bakeTime: 22, 
 
    bakeTemp: 420, 
 
    mixingInstructions: function() { 
 
    return "Add " + 
 
    this.firstIngredient + " to " + this.secondIngredient + " and stir with " + this.thirdIngredient + " and bake at " + this.bakeTemp + " for " + this.bakeTime + " minutes."; 
 
    } 
 
}; 
 

 
console.log(cake.mixingInstructions())
上@Stephen大號

+0

失蹤'this'? – nem035

+0

是的,他沒有在這裏使用'this'在這裏烘烤'+ bakeTem' – julekgwa

+0

哦,我明白了,我的壞,你可能還會提到缺少的'+'符號,那麼 – nem035

2

後續行動,

你忘了加上後>>>>> return "Add " +

2

你還缺少一個 「+」 後返回「添加「

2

你有這3個錯誤:

  1. 缺少逗號後 bakeTime:22 bakeTemp:420
  2. 缺少+"Add"
  3. 在返回字符串缺少bakeTempthis.bakeTime

    return "Add " + this.firstIngredient + " to " + this.secondIngredient + " and stir with " + this.thirdIngredient + " and bake at " + this.bakeTemp + " for " + this.bakeTime + " minutes.";

var cake = { 
 
     firstIngredient: "milk", 
 
     secondIngredient: "eggs", 
 
     thirdIngredient: "cakemix", 
 
     bakeTime: 22, 
 
     bakeTemp: 420, 
 
     mixingInstructions: function() { 
 
     return "Add "+ 
 
this.firstIngredient + " to " + this.secondIngredient + " and stir with " + this.thirdIngredient + " and bake at " + this.bakeTemp + " for " + this.bakeTime + " minutes."; 
 
     } 
 
    }; 
 
console.log(cake.mixingInstructions());