开发者

What does in do or mean in an if statement?

I am JavaScript learner. I like to skim code to learn new things. Recently i was looking in jQuery and found a condition like : if ( "zoom" in div.style ). Wh开发者_运维百科at is in and whats the condition being tested for here?


It tests for the existence of a property in an object (including prototyped properties).

Example: http://jsfiddle.net/6RVD2/1/

var obj = {someProp: 'someValue',
           anotherProp: 'anotherValue'  
           };

var empty_obj = {};

function F() {};

F.prototype.someProp = 'someValue';

var proto_obj = new F;

if( 'someProp' in obj ) { 
   alert('yep'); // alert fires
}


if( 'someProp' in empty_obj ) { 
   alert('yep');  // alert doesn't fire
}


if( 'someProp' in proto_obj ) { 
   alert('yep');  // alert fires
}


See the in operator.

It checks whether the object div.style as the property zoom (i.e. div.style.zoom).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜