简体   繁体   English

if语句中的in表示什么或意味着什么?

[英]What does in do or mean in an if statement?

I am JavaScript learner. 我是JavaScript学习者。 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 ) . 最近,我正在使用jQuery,发现了类似条件: if ( "zoom" in div.style ) What is in and whats the condition being tested for here? 什么是in和什么条件正在这里进行测试?

It tests for the existence of a property in an object (including prototyped properties). 它测试对象中属性的存在(包括原型属性)。

Example: http://jsfiddle.net/6RVD2/1/ 示例: 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 . 参见in运算符

It checks whether the object div.style as the property zoom (ie div.style.zoom ). 它检查对象div.style是否作为属性 zoom (即div.style.zoom )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM