Say I have an object person
like so
var person = {
firstname: 'Default',
lastname: 'Default',
getFullName: function() {
return this.firstname + ' ' + this.lastname;
}
}
I make a new object john
and set its prototype to that of person
var john = {
firstname: 'John',
lastname: 'Doe'
}
john.__proto__ = person;
if I console.log
john
, I see a tree structure like so
where we see getFullName
embedded under __proto__
. Now here comes the surprise
for (var prop in john) {
console.log(prop)
}
returns
Even though getFullName
was one level deep, somehow, the loop was able to find it.
Now, compare that to
var obj = {a: 1, b: {c:2, d: 3 }}
for (var prop in obj) {console.log(prop)}
which behaves as I expect, which is that c
and d
were not automagically found by the loop
So how is it that in the former case the loop traversed deeper in to the tree to dig up embedded properties while in the latter it did not?