富在酒吧 - “在'操作的JavaScript? [英] foo in bar - 'in' operator javascript?
问题描述
我最近看了关于CSS的浏览器功能检测的教程......最终产品是这样的事情...
I recently read a tutorial on CSS browser feature detection... The end product was something like this...
var prefix = ['Moz', 'webkit', 'O', 'ms', 'Khtml'],
test_style = document.createElement('div').style;
var css_check = function(prop) {
if (prop in test_style) {
return true;
}
for (var i = 0; i < prefix.length; i++) {
if (prefix[i] + prop in test_style) {
return true;
}
}
return false;
};
css_check('whatev_css_property');
我不明白的部分是这个...
The part I dont understand is this...
如果(在test_style道具)
或如果(巴富)
。
从我读过如果(巴富)
用来检查一个值是一个数组,但我可能是错在这里,我没有发现这么多的文档。此外,如果这是在使用一个数组来检查值是如何为 test_style =使用document.createElement('DIV')。风格
数组?无厘头...
From what I've read if (foo in bar)
is used to check if a value is in an array but I might be wrong here, I haven't found much documentation on this. Also, if this is used to check values in an array HOW is test_style = document.createElement('div').style
an array? Doesn't make sense...
我是可怕的困惑。任何澄清将大大AP preciated。
I am terrible confused. Any clarification would be greatly appreciated.
推荐答案
的在
运算符用于检查一个的键的presence 的在数组或对象,如:
The in
operator is used to check for the presence of a key in an array or object, e.g.
3 in [1, 2, 3] // false, since the array indices only go up to 2
2 in [1, 2, 3] // true
'x' in { x: 5 } // true
'toString' in Object.prototype // true
的风格
属性存在的的CSSStyleDeclaration ,其中包含在活动的浏览器支持的每个样式属性的属性。
The style
property there is an instance of CSSStyleDeclaration, which contains properties for each supported style attribute in the active browser.
在code片段在您检查后给观看浏览器是否支持这种风格的一些版本(无论是官方的一个或与多家供应商共同prefixes之一)。
The code snippet you gave in your post checks whether the viewing browser supports some version of that style (either the official one or with one of a number of common vendor prefixes).
这篇关于富在酒吧 - “在'操作的JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!