Just for an example:
var animal = {"legs": 4}; var dog = {"barks" : true}; dog.__proto__ = animal;
now it means that:
console.log("legs" in dog); // TRUE - due to JS inheritance and since 'in' key looks into inherited prototypes console.log(dog.hasOwnProperty("legs")); // FALSE console.log(dog.hasOwnProperty("barks")); //TRUE
prints
true false true
Note one more thing: if the dog had the βlegsβ property, let's say 5 (hhmm spooky, which I know), so the βinβ operator (βlegsβ of the dog) will refer to the βlegsβ property that is declared in the dogβs subject rather than inherited βlegsβ that come from an animalβs object. eg:.
var animal = {"legs": 4}; var dog1 = {"barks": true}; var dog2 = {"barks": true, "legs": 5}; dog1.__proto__ = animal; dog2.__proto__ = animal; console.log(dog1.legs); console.log(dog2.legs); console.log("legs" in dog2)
prints
4 5 true
Mercury
source share