An operator incan tell you if a key exists in an object or any of the objects in the prototype chain. A function hasOwnPropertycan tell you if this key exists in the object itself (not in any of the objects in the prototype chain).
if ("foo" in obj) {
// `obj` or an ancestor has a key called "foo"
}
if (obj.hasOwnProperty("foo")) {
// `obj` has a key called "foo"
}
, , Object ({} = > new Object()), . , , , , :
var a = [1, 2, 3, 4];
a.foo = "testing";
alert("push" in a); // 1. alerts "true"
alert(a.hasOwnProperty("push")); // 2. alerts "false"
alert("foo" in a); // 3. alerts "true"
alert(a.hasOwnProperty("foo")); // 4. alerts "true"
# 1 true, push Array.prototype ( , ). # 2 , push , . # 3 # 4 true, foo .
JSON, Javascript Object Literal Notation, JSON . ( " " ) . , ( ) . {} JSON, , , , "undefined" .: -)