Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

typescript - 类变量在超级调用后的默认值?(Class variables default value after super call?)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...