foo in bar - 'in' operator javascript?

前端 未结 4 2221
庸人自扰
庸人自扰 2021-01-21 09:52

I recently read a tutorial on CSS browser feature detection... The end product was something like this...

var prefix = [\'Moz\', \'webkit\', \'O\', \'ms\', \'Kht         


        
4条回答
  •  醉酒成梦
    2021-01-21 10:47

    The statement if (foo in bar) tests whether the object bar has a property named foo. It doesn't test for a property with the value foo.

    That is:

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

    You can use this syntax on arrays because they are a type of object. If bar is an array then foo in bar will be true if foo is either a numeric index of the array that has a value or if foo is some other property or method name.

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

    test_style is an object, not an array.

提交回复
热议问题