admin管理员组文章数量:1314039
In my typescript I'm trying to create/clone an child-object via a method in the base-class. This is my (simplified) setup.
abstract class BaseClass<TCompositionProps> {
protected props: TCompositionProps;
protected cloneProps(): TCompositionProps { return $.extend(true, {}, this.props); } // can be overwriten by childs
constructor(props: TCompositionProps){
this.props = props;
}
clone(){
const props = this.cloneProps();
return this.constructor(props);
}
}
interface IProps {
someValues: string[];
}
class Child extends BaseClass<IProps>{
constructor(props: IProps){
super(props);
}
}
Now, I'm going to create a new object
const o1 = new Child({someValues: ["This","is","a","test"]};
// get the clone
const clone = o1.clone();
The constructor is hit (but it's just the call to the function), meaning there is no new object created.
When using return Child.prototype.constructor(props)
instead I get my new object.
So how can I call the constructor of Child
in it's base-class?
Also tried this
In my typescript I'm trying to create/clone an child-object via a method in the base-class. This is my (simplified) setup.
abstract class BaseClass<TCompositionProps> {
protected props: TCompositionProps;
protected cloneProps(): TCompositionProps { return $.extend(true, {}, this.props); } // can be overwriten by childs
constructor(props: TCompositionProps){
this.props = props;
}
clone(){
const props = this.cloneProps();
return this.constructor(props);
}
}
interface IProps {
someValues: string[];
}
class Child extends BaseClass<IProps>{
constructor(props: IProps){
super(props);
}
}
Now, I'm going to create a new object
const o1 = new Child({someValues: ["This","is","a","test"]};
// get the clone
const clone = o1.clone();
The constructor is hit (but it's just the call to the function), meaning there is no new object created.
When using return Child.prototype.constructor(props)
instead I get my new object.
So how can I call the constructor of Child
in it's base-class?
Also tried this
Share Improve this question asked Aug 24, 2017 at 20:28 KingKerosinKingKerosin 3,8515 gold badges44 silver badges86 bronze badges 01 Answer
Reset to default 11You can invoke the constructor with the new operator, that seems to work. Also I would use this
for the return type so that the clone method will return the derived type not the base type
abstract class BaseClass<TCompositionProps> {
protected props: TCompositionProps;
protected cloneProps(): TCompositionProps { return $.extend(true, {}, this.props); }
constructor(props: TCompositionProps){
this.props = props;
}
clone() : this{
const props = this.cloneProps();
return new (<any>this.constructor)(props);
}
}
本文标签: javascriptCall constructor from derived type via this in typescriptStack Overflow
版权声明:本文标题:javascript - Call constructor from derived type via this in typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741960042a2407222.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论