var object1 = {
name: "abc",
age: 21
}
var Object2 = function() {}
Object2.prototype = object1;
Object2.prototype.hello = function() {
console.log("Hello");
}
var obj = Object.create(object1);
for (var prop in obj) {
console.log(prop + ": " + obj[prop]);
}
The output of this code is:
name: abc
age: 21
hello: function () {
console.log("Hello");
}
Obj is created by setting its prototype as object1, which doesn't have the 'hello' function, so why is it listed in the output? If I comment out 'Object2.prototype = object1;', the 'hello' function no longer shows in the output. I don't see how obj and Object2 are connected. Can anyone please explain what's happening here?