简体   繁体   English

“ in”语句中使用的document.documentElement

[英]document.documentElement used in “in” statement

I saw this piece of code in jQuery's source. 我在jQuery的源代码中看到了这段代码。 I am a novice in javascript and I would like to know how this works. 我是javascript的新手,我想知道这是如何工作的。

if ( "getBoundingClientRect" in document.documentElement ) {
// do something...
}

How is it different from 有什么不同

if(document.documentElement.getBoundingClientRect) {
// do something...
}

...? ...?

That's an expression using the in operator . 那是一个使用in运算符的表达式。 The in operator checks to see if the target object has a property with the given name (directly, or via the object's prototype). in运算符检查目标对象是否具有具有给定名称的属性(直接或通过对象的原型)。

What it's doing is seeing if document.documentElement has a property with the name getBoundingClientRect (which is a handy function jQuery would like to use if the browser provides it). 它的作用是查看document.documentElement是否具有名称为getBoundingClientRect的属性(如果浏览器提供的话,这是jQuery希望使用的便捷函数)。

How is it different from if(document.documentElement.getBoundingClientRect) { // do something... } if(document.documentElement.getBoundingClientRect) { // do something... }有什么区别if(document.documentElement.getBoundingClientRect) { // do something... }

in doesn't require fetching the value of the property, just checking that it exists. in不需要获取属性的 ,只需检查它是否存在。 Also note that the second form you list would test the truthiness of the value of the property. 还要注意,您列出的第二种形式将测试属性的真实性。 In the case of a function like getBoundingClientRect that would be fine, but if you wanted to test something whose value might come back falsey ( 0 , "" , etc.), it would fail even though the property exists. 对于像getBoundingClientRect这样的函数来说,这很好,但是如果您要测试其值可能返回假值的东西( 0""等),即使该属性存在,它也会失败。

You'll see this sort of thing a lot when doing feature-detection. 在进行特征检测时,您会经常看到这种事情。 For instance, if you want to know if a browser supports the HTML5 placeholder attribute: 例如,如果您想知道浏览器是否支持HTML5 placeholder属性:

if ('placeholder' in document.createElement('input')) {
    // Yes, it does
}

This is an example where we couldn't use the form if (document.createElement('input').placeholder) because if the condition were false, we wouldn't know whether it was false because the property didn't exist, or false because the property existed but had a falsey value (and the default value for placeholder is "" , which is indeed falsey). 这是一个示例,其中我们不能使用if (document.createElement('input').placeholder)因为如果条件为假,则由于该属性不存在,我们将不知道它是否为假,或者因为该属性存在但具有假值(并且placeholder的默认值为"" ,实际上为假),所以为false。

Gratuitous link to list of feature detections. 指向功能检测列表的免费链接。

from MDN The in operator returns true if the specified property is in the specified object. from MDN如果指定的属性在指定的对象中,则in运算符将返回true。

syntax : 句法 :

prop in objectName 

prop A string or numeric expression representing a property name or array index. prop表示属性名称或数组索引的字符串或数字表达式。

objectName : Name of an object. objectName:对象的名称。

so document.documentElement is the Object and "getBoundingClientRect" is the property. 因此document.documentElement是Object,而"getBoundingClientRect"是属性。

The 'a' in obj is different from obj.a if a == false . 如果a == false 'a' in obj'a' in objobj.a不同。

obj = {a: false};
'a' in obj;
> true
obj.a
> false

The in operator checks to see if a given property is or present within a specified object or not. in运算符检查指定对象中是否存在给定属性。 SO, 所以,

if ( "getBoundingClientRect" in document.documentElement ) {
// do something...
}

will check if getBoudingClientRect property is available in document.documentElement Object. 将检查document.documentElement对象中的getBoudingClientRect属性是否可用。

Moreover, you might want to read the article form John Resig himself, who has well explained, Why it is used. 此外,您可能想阅读John Resig自己的文章 ,他很好地解释了“为什么使用它”。

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

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