gpt4 book ai didi

javascript - foo in bar - 'in' 运算符 javascript?

转载 作者:行者123 更新时间:2023-12-02 07:39:08 25 4
gpt4 key购买 nike

我最近看了一个关于CSS浏览器特征检测的教程...最终产品是这样的...

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');

我不明白的部分是...

if (prop in test_style)if (foo in bar)

根据我的阅读,if (foo in bar) 用于检查一个值是否在数组中,但我在这里可能是错的,我没有找到太多关于此的文档。另外,如果这用于检查数组中的值 test_style = document.createElement('div').style 是一个数组吗?没有意义...

我很困惑。如有任何澄清,我们将不胜感激。

最佳答案

语句if (foo in bar) 测试对象bar 是否具有named foo 属性。它不会测试值为 foo 的属性。

即:

var bar = {"a" : "x", "b" : "y"};
alert("a" in bar); // true
alert("x" in bar); // false

您可以在数组上使用此语法,因为它们是一种对象。如果 bar 是一个数组,那么 foo in bar 将为真,如果 foo 是数组的具有值的数字索引,或者如果 foo 是其他一些属性或方法名称。

Also, if this is used to check values in an array HOW is test_style = document.createElement('div').style an array?

test_style 是对象,不是数组。

关于javascript - foo in bar - 'in' 运算符 javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13149450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com