开发者

Use of "in" while declaring JS variables

Thanks in advance for your patience... Really trying to fill in weird gaps in 开发者_如何学Gomy JS knowledge...

Analyzing other people's scripts and came across:

Var isTouch = "ontouchstart" in window;

What is the "in window" part doing? I've googled several basic variable declaration tutorials and it's not mentioned, and "in" is such a basic word, it's been hard to find answers.

Thanks,

JK


The expression someString in someObject returns a boolean indicating whether the object has a property by that name.
Spec

Your code, other than having a miscapitalized Var, sets isTouch to true if window has an ontouchstart property.


isTouch is now simply a boolean (true or false), as the in operator returns a boolean:

var foo = {
   bar : 42
},

hasBar  = 'bar'  in foo,  // true
hasFoob = 'foob' in foo;  // false
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜