@reference_-1_overall
//test start- 2
//what if using 'Employee.prototype = Person.prototype' instead?
//jane.sayHello(); //output: Hello, my name is Jane, I'm the Accountant.
//jane.walk(); // output: Jane is walking.
//jane.run(); // output: Jane is running.
//alert(Employee.prototype.isPrototypeOf(jane)); // output: true
//txt = jane.constructor.toString();
//output: function Employee(name, title) { Person.call(this, name); this.title = title; }
//p.sayHello(); //output: Hello, my name is Jane, I'm the undefined.
//Because 'Employee.prototype.sayHello = function () {...}' statement also changed the 'Person.prototype'.
//test end - 2
//test start- 3
//what if using 'Employee.prototype = new Person()' instead?
//jane.sayHello(); //output: Hello, my name is Jane, I'm the Accountant.
//jane.walk(); // output: Jane is walking.
//jane.run(); // output: Jane is running.
//alert(Employee.prototype.isPrototypeOf(jane)); // output: true
try {
let e = Object.create(Employee.prototype);
//e.sayHello(); // output: Hello, my name is undefined, I'm the undefined
//e.walk(); // output: undefined is walking.
//e.run(); // output: undefined is running.
let x = Object.create(Person.prototype);
//x.sayHello(); // output: Hello, my name is undefined.
//x.walk(); //throw an error: Object doesn't support property or method 'walk'
//x.run(); // output: undefined is running.
} catch (err) {
alert(err.message);
}
//test end - 3
Very simply said, new X is Object.create(X.prototype) with additionally running the constructor function. (And giving the constructor the chance to return the actual object that should be the result of the expression instead of this.)
@reference_1_stackoverflow
//test start - 4
//using default: 'Employee.prototype = Object.create(Person.prototype)'
try {
let e = Object.create(Employee.prototype);
//alert(typeof e.prototype); // output: undefined
//e.sayHello(); // output: Hello, my name is undefined, I'm the undefined
//e.walk(); // throw an error: Object doesn't support property or method 'walk'
//e.run(); // output: undefined is running.
let x = Object.create(Person.prototype);
//x.sayHello(); // output: Hello, my name is undefined.
//x.walk(); //throw an error: Object doesn't support property or method 'walk'
//x.run(); // output: undefined is running.
} catch (err) {
alert(err.message);
}
//test end - 4
No comments:
Post a Comment