我正在使用Javascript Koans - 关于对象 - 问题#3 并且无法理解对象内的battleCry函数的工作原理。 具体地,部分Javascript Koans - 关于对象 - 这个函数是如何工作的#3
阵列(noOfBrains + 1)。加入(”“+ this.mastermind)
当调用函数用参数(4),并不意味着( noOfBrains)= 4。因此
阵列(4 + 1)。加入(”“+ this.mastermind)
莫非此 array有5个未定义的属性? 然后加入一个空间和主脑名称“Brain”。 我不明白下面的正确答案如何产生4个“大脑”,那里没有循环功能吗?以及它不是5倍“大脑”?
下面是带正确答案的代码。
it("should know properties that are functions act like methods",function() {
var megalomaniac = {
mastermind : "Brain",
henchman: "Pinky",
battleCry: function (noOfBrains) {
return "They are " + this.henchman + " and the" +
Array(noOfBrains + 1).join(" " + this.mastermind);
}
};
var battleCry = megalomaniac.battleCry(4);
expect("They are Pinky and the Brain Brain Brain Brain").toMatch(battleCry);
});
我明白了!谢谢你解释,我现在明白了! – Tinah