" var x = "theBomb" in megalomaniac; 是做什么的?
What does " var x = "theBomb" in megalomaniac; " do?
我正在研究来自 GitHub 的 JavaScript 个 koans 并被这个停止:
it("should have the bomb", function () {
var hasBomb = "theBomb" in megalomaniac;
expect(hasBomb).toBe(FILL_ME_IN);
});
没见过这种建筑
var x = "y" in object;
之前,我不确定它在做什么。
公案期望 hasBomb 为真。
a in b
检查对象 b
是否有 属性 a
。
注:也是通过原型链来检查的
如果您不需要检查整个原型链,您可以使用此代码。
b.hasOwnProperty(a);
直接来自 documentation
// Custom objects
var mycar = {make: "Honda", model: "Accord", year: 1998};
"make" in mycar // returns true
"model" in mycar // returns true
所以在你的情况下,对象 megalomaniac
可能有也可能没有 属性 theBomb
并且这段代码(可能是某种单元测试)正在检查它。
声明由两部分组成:
"theBomb" in megalomaniac;
检查对象 megalomaniac
(或其原型链)中是否存在名为 theBomb
的 属性。参见 in Operator。
var hasBomb = "theBomb" in megalomaniac;
将该检查的值(true
或 false
)分配给变量 hasBomb
.
示例:
var megalomaniac = {theBomb: 'boom'};
var hasBomb = "theBomb" in megalomaniac;
console.log(hasBomb); // true
我正在研究来自 GitHub 的 JavaScript 个 koans 并被这个停止:
it("should have the bomb", function () {
var hasBomb = "theBomb" in megalomaniac;
expect(hasBomb).toBe(FILL_ME_IN);
});
没见过这种建筑
var x = "y" in object;
之前,我不确定它在做什么。 公案期望 hasBomb 为真。
a in b
检查对象 b
是否有 属性 a
。
注:也是通过原型链来检查的
如果您不需要检查整个原型链,您可以使用此代码。
b.hasOwnProperty(a);
直接来自 documentation
// Custom objects
var mycar = {make: "Honda", model: "Accord", year: 1998};
"make" in mycar // returns true
"model" in mycar // returns true
所以在你的情况下,对象 megalomaniac
可能有也可能没有 属性 theBomb
并且这段代码(可能是某种单元测试)正在检查它。
声明由两部分组成:
"theBomb" in megalomaniac;
检查对象megalomaniac
(或其原型链)中是否存在名为theBomb
的 属性。参见 in Operator。var hasBomb = "theBomb" in megalomaniac;
将该检查的值(true
或false
)分配给变量hasBomb
.
示例:
var megalomaniac = {theBomb: 'boom'};
var hasBomb = "theBomb" in megalomaniac;
console.log(hasBomb); // true