function xyz() {}
function abc() {}
var p = new xyz();
abc.prototype = p;
var o = new abc();
o.__proto__ === p // true
o.__proto__.__proto__ === xyz.prototype // true
o.__proto__.__proto__.__proto__ === Object.prototype // true
o.__proto__.__proto__.__proto__.__proto__ === null // true
或者:
function xyz() {}
function abc() {}
var p = Object.create(xyz.prototype);
abc.prototype = p;
var o = new abc();
o.__proto__ === p // true
o.__proto__.__proto__ === xyz.prototype // true
o.__proto__.__proto__.__proto__ === Object.prototype // true
o.__proto__.__proto__.__proto__.__proto__ === null // true
或者:
class xyz {}
class abc extends xyz {}
var o = new abc();
o.__proto__.__proto__ === xyz.prototype // true
o.__proto__.__proto__.__proto__ === Object.prototype // true
o.__proto__.__proto__.__proto__.__proto__ === null // true
或者:
function xyz() {}
const abc = {
__proto__: Object.create(xyz.prototype)
}
abc.__proto__.__proto__ === xyz.prototype // true
abc.__proto__.__proto__.__proto__ === Object.prototype // true
abc.__proto__.__proto__.__proto__.__proto__ === null // true
或者:
function xyz() {}
function abc() {}
Object.setPrototypeOf(abc.prototype, xyz.prototype);
var o = new abc();
o.__proto__.__proto__ === xyz.prototype // true
o.__proto__.__proto__.__proto__ === Object.prototype // true
o.__proto__.__proto__.__proto__.__proto__ === null // true
來源
2017-06-16 17:14:46
Ben
'Object.setPrototypeOf(abc.prototype,xyz.prototype);' – 4castle
請使用反引號來正確格式化代碼。 – 2017-06-16 16:47:56
你想達到什麼目的?或者這是一種學術學習練習? – 2017-06-16 16:49:05