我有一个示例程序,我测试三个对象的原型链。当我在Mozilla中输入脚本时,它会给出预期的输出:
Object { constructor: John() }
Object { constructor: Employee() }
Object { run: Person.prototype.run(), … }
Object { … }
Chrome中的相同程序会提供此输出:
Employee {constructor: ƒ}
Person {constructor: ƒ}
{run: ƒ, constructor: ƒ}
{constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}
为什么会出现这种差异?究竟发生了什么?这是代码:
function Person() {
this.age = 15;
this.year_born = 1996;
}
Person.prototype.run = function () {
console.log("running");
}
function Employee() {
Person.call(this);
this.salary = 600;
}
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
function John() {
Employee.call(this);
this.wife = "Jenny";
}
John.prototype = Object.create(Employee.prototype);
John.prototype.constructor = John;
var per = new Person();
var emp = new Employee();
var john = new John();
for (var prot = Object.getPrototypeOf(john); prot != null; prot = Object.getPrototypeOf(prot)) {
console.log(prot);
}