Say I have some basic class that gets an object of properties and sets them in the class:
(假设我有一些基本类,可以获取属性的对象并将其设置在类中:)
export interface BaseNodeProps {
id? : string;
}
export class BaseNode {
id : string;
constructor(props: BaseNodeProps) {
for (let prop in props)
this[prop] = props[prop];
}
}
And I extend this class to have more properties
(我扩展了该类,使其具有更多属性)
export interface ExtendedProps extends BaseNodeProps {
name? : string;
list? : string[];
}
export class ExtendedNode extends BaseNode {
name : string = 'Default name';
list : string[] = [];
constructor(props: BaseNodeProps) {
super(props);
}
}
Now, say I want to call for new ExtendedNode with some properties:
(现在,假设我要调用具有某些属性的新ExtendedNode:)
someProps = {
name : 'My name',
list : ['thing to do', 'another thing to do']
}
When I call for new ExtendedNode(someProps)
, the object always get the default values (ie 'Default name'
and []
), although I sent different values to the construcor in someProps
, that the super()
method should set.
(当我调用new ExtendedNode(someProps)
,对象总是获得默认值(即'Default name'
和[]
),尽管我向someProps
的construcor发送了不同的值,但应该设置super()
方法。)
Does the super() command is being called before the variables declaration? (在变量声明之前是否调用了super()命令?)
What is the reason for this? (这是什么原因呢?)
ask by Moshe Estroti translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…