2017-05-24 91 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());