Javascript 如何自定义设置原型链接(构造函数)?

Javascript 如何自定义设置原型链接(构造函数)?,javascript,Javascript,我正在学习javascript链接,但我不能像我想的那样设置构造函数的深度 function a() {} function b() {} function c() {} a.prototype.text1 = 'text1'; b.prototype.text2 = 'text2'; c.prototype.text3 = 'text3'; a.prototype.constructor = b; b.prototype.constructor = c; var foo = new a(

我正在学习javascript链接,但我不能像我想的那样设置构造函数的深度

function a() {}
function b() {}
function c() {}

a.prototype.text1 = 'text1';
b.prototype.text2 = 'text2';
c.prototype.text3 = 'text3';

a.prototype.constructor = b;
b.prototype.constructor = c;

var foo = new a();

console.log(foo.text1) // it`s output 'text1'. yea~ it`s right!
// But...
console.log(foo.text3) // it`s output undefined. My intention was output 'test3'.
为什么会这样? 我能做什么

请帮忙。谢谢。

您可以使用

函数a(){}
函数b(){}
函数c(){}
c、 prototype.text3='text3';
b、 prototype=Object.create(c.prototype);
b、 prototype.text2='text2';
a、 prototype=Object.create(b.prototype);
a、 prototype.text1='text1';
var foo=新的a();
console.log(foo.text1);
console.log(foo.text2);
console.log(foo.text3)您可以尝试探索,抛弃所有丑陋的原型样板。实现目标的示例如下:

让obj1={
Text 1:“Text 1”
}
让obj2=Object.create(obj1)
obj2.text2='Text 2';
让obj3=Object.create(obj2)
obj3.text3='Text 3';
设foo=obj3;
console.log(foo.text1)//Text 1
console.log(foo.text2)//Text 2

console.log(foo.text3)//Text 3
@msm082919查看我刚刚更新的ES6语法。如果你知道ES6,你会更容易理解。我认为最后一种方法是错误的。当
var foo=new b()
@msm082919时,我可以读取属性text3,这不是您想要做的吗?这是一个关于OLOO设计的建议:这是一个非常好的建议,适用于只需要使用单个对象的场景。但是,
class
es确实有为任意数量的实例构建模板的好处,因此请记住,这在所有情况下都不是理想的+1.