admin管理员组文章数量:1335840
While trying to find a way to use nested classes in JS, I came up with this sort of thing:
class Character {
constructor() {
this.Info= class I {
constructor(name,value) {
this.name=name;
this.value=value;
}
};
}
bar () {
var trial = new this.Info("Goofy", 2);
alert(trial.name);
}
}
var test = new Character();
test.bar();
and it seems to work. However, I'm afraid this might be creating a new function object for each new
call, as I define the class in the constructor (which is executed at each new
call). Is there a more efficient way of doing this?
This question does not solve my issue as the author only wonders how to even have a nested class
; I'm already able to do that but I wonder if there's a more efficient way.
While trying to find a way to use nested classes in JS, I came up with this sort of thing:
class Character {
constructor() {
this.Info= class I {
constructor(name,value) {
this.name=name;
this.value=value;
}
};
}
bar () {
var trial = new this.Info("Goofy", 2);
alert(trial.name);
}
}
var test = new Character();
test.bar();
and it seems to work. However, I'm afraid this might be creating a new function object for each new
call, as I define the class in the constructor (which is executed at each new
call). Is there a more efficient way of doing this?
This question does not solve my issue as the author only wonders how to even have a nested class
; I'm already able to do that but I wonder if there's a more efficient way.
- Have you considered creating Info as a static method instead? – RobG Commented Jun 9, 2019 at 0:23
-
Nice one, but I guess it would mean falling back to the
function
syntax? I'd like to be able to useclass
– memememe Commented Jun 9, 2019 at 0:27 - Possible duplicate of Nested ES6 classes? – filipe Commented Jun 9, 2019 at 0:53
- @filipe I've edited to explain why my question is different – memememe Commented Jun 9, 2019 at 1:05
- So.. the most efficient way is to have it as static using any of the available nomenclature, or then just to not nest it ! – filipe Commented Jun 9, 2019 at 1:23
2 Answers
Reset to default 5Using a static property in react, angular or just using babel, because direct static class properties are not currently implemented on all browsers.
class Character {
static Info = class I {
constructor(name) { this.name=name; }
}
bar () {
return new Character.Info("Goofy");
}
}
const test = new Character();
console.log(test.bar());
Using a static property the old way -- currently working on all browsers.
class Character {
bar () { return new Character.Info("Goofy"); }
}
Character.Info = class I {
constructor(name) { this.name=name; }
}
const test = new Character();
console.log(test.bar());
Maybe the example you've given is too simple to demonstrate whatever problem you're trying to solve, but it seems to me you don't need to nest them at all.
class Info {
constructor(name, value) {
this.name = name;
this.value = value;
}
}
class Character {
bar() {
var trial = new Info("Goofy", 2);
console.log(trial.name);
}
}
const test = new Character();
test.bar();
本文标签: javascriptEfficient and elegant way to create nested ES6 classesStack Overflow
版权声明:本文标题:javascript - Efficient and elegant way to create nested ES6 classes? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742399631a2467624.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论