我是一个有用的助手,可以将文本翻译成中文。
我有一个对象,比如说
当然,我可以为父亲创建一个构造函数,例如:
然后使用
但这不完全是我想要的。由于“son”将拥有许多属性,因此将其声明为对象文字将更易读。但是我不知道如何设置它的原型。
做类似以下的事情:
无法工作,因为原型链似乎被隐藏起来,并且不关心构造函数.prototype的更改。
我认为我可以在Firefox中使用__proto__属性,例如:
但是据我所了解,这不是语言的标准特性,最好不要直接使用它。
有没有一种方法可以指定对象字面量的原型?
我有一个对象,比如说
儿子
,我想让它继承另一个对象父亲
。当然,我可以为父亲创建一个构造函数,例如:
Father = function() {
this.firstProperty = someValue;
this.secondProperty = someOtherValue;
}
然后使用
var son = new Father();
son.thirdProperty = yetAnotherValue;
但这不完全是我想要的。由于“son”将拥有许多属性,因此将其声明为对象文字将更易读。但是我不知道如何设置它的原型。
做类似以下的事情:
var father = {
firstProperty: someValue;
secondProperty: someOtherValue;
};
var son = {
thirdProperty: yetAnotherValue
};
son.constructor.prototype = father;
无法工作,因为原型链似乎被隐藏起来,并且不关心构造函数.prototype的更改。
我认为我可以在Firefox中使用__proto__属性,例如:
var father = {
firstProperty: someValue;
secondProperty: someOtherValue;
};
var son = {
thirdProperty: yetAnotherValue
__proto__: father
};
son.constructor.prototype = father;
但是据我所了解,这不是语言的标准特性,最好不要直接使用它。
有没有一种方法可以指定对象字面量的原型?