admin管理员组文章数量:1026446
I would like to know if I'm doing it properly...
Having this code:
class Room {
constructor(type, size, hasWindows, equipment) {
this.type = type;
this.size = size;
this.hasWindows = hasWindows;
this.equipment = ['esterillas', ...equipment];
};
};
class PilatesRoom extends Room {
};
const room1 = new PilatesRoom('pilates', 20, true, ['balón medicinal'])
console.log(room1);
//returns: PilatesRoom {type: "pilates", size: 20, hasWindows: true, equipment: Array(2)}
I mean... I don't really need to use "constructor" and "super" to make it works perfectly, but when I check it on the internet, everybody uses it. Should I? For example:
class PilatesRoom extends Room {
constructor(type, size, hasWindows, equipment) {
super(type, size, hasWindows, equipment)
};
};
This returns the same.
I'm trying to understand! Thank you guys for your time.
I would like to know if I'm doing it properly...
Having this code:
class Room {
constructor(type, size, hasWindows, equipment) {
this.type = type;
this.size = size;
this.hasWindows = hasWindows;
this.equipment = ['esterillas', ...equipment];
};
};
class PilatesRoom extends Room {
};
const room1 = new PilatesRoom('pilates', 20, true, ['balón medicinal'])
console.log(room1);
//returns: PilatesRoom {type: "pilates", size: 20, hasWindows: true, equipment: Array(2)}
I mean... I don't really need to use "constructor" and "super" to make it works perfectly, but when I check it on the internet, everybody uses it. Should I? For example:
class PilatesRoom extends Room {
constructor(type, size, hasWindows, equipment) {
super(type, size, hasWindows, equipment)
};
};
This returns the same.
I'm trying to understand! Thank you guys for your time.
Share Improve this question asked Dec 6, 2018 at 13:13 SazLamasSazLamas 451 silver badge8 bronze badges 3- "when I check it on the internet, everybody uses it." - where and how many samples did you check? I could certainly find you counterexamples. – Bergi Commented Dec 6, 2018 at 14:43
- stackoverflow./q/48657481/1048572 – Bergi Commented Dec 6, 2018 at 14:48
- @SazLamas please mark one answer as accepted. every so-question should have one accepted answer. Thanks! – wuarmin Commented Sep 14, 2021 at 10:35
4 Answers
Reset to default 4You don't have to add a child class constructor if it doesn't add any logic. (In fact, static analysis and code quality tools sometimes flag it as a "useless constructor" and give a warning.)
Some programmers prefer the explicit definition of the constructor, some may carry over habits from other languages which may require it, etc. But unless the child constructor is actually doing something for the child class other than just passing the same values to the parent constructor, it's not necessary.
You have to use the super()
expession when you want to use a constructor of your child class. Otherwise you don't have to. As simple as that.
class PilatesRoom extends Room {
// the constructor should be removed (there is no point to keep it):
constructor(type, size, hasWindows, equipment) {
super(type, size, hasWindows, equipment)
};
};
In the above code, there is no reason to define a constructor. However in the below code you have to call foo()
and thus, you also have to use super()
:
class PilatesRoom extends Room {
constructor(type, size, hasWindows, equipment) {
super(type, size, hasWindows, equipment)
foo()
};
};
More info here: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/super#Description
As far as I know class constructor is just a syntactic sugar in javascript so you don't have to use it.
Also as MDN explain:
"The constructor method is a special method for creating and initializing an object created with a class. There can only be one special method with the name "constructor" in a class. A SyntaxError will be thrown if the class contains more than one occurrence of a constructor method.
A constructor can use the super keyword to call the constructor of the super class."
as it mentioned it can be there and or not.
MDN for further understanding
Less code is more. So if your child class doesn't have additional attributes and needs no special initialization logic, skip it. If you skip it you get the default constructor, which is totally sufficient.
You need to maintain every line of code. If there's no line there's no maintenance.
I would like to know if I'm doing it properly...
Having this code:
class Room {
constructor(type, size, hasWindows, equipment) {
this.type = type;
this.size = size;
this.hasWindows = hasWindows;
this.equipment = ['esterillas', ...equipment];
};
};
class PilatesRoom extends Room {
};
const room1 = new PilatesRoom('pilates', 20, true, ['balón medicinal'])
console.log(room1);
//returns: PilatesRoom {type: "pilates", size: 20, hasWindows: true, equipment: Array(2)}
I mean... I don't really need to use "constructor" and "super" to make it works perfectly, but when I check it on the internet, everybody uses it. Should I? For example:
class PilatesRoom extends Room {
constructor(type, size, hasWindows, equipment) {
super(type, size, hasWindows, equipment)
};
};
This returns the same.
I'm trying to understand! Thank you guys for your time.
I would like to know if I'm doing it properly...
Having this code:
class Room {
constructor(type, size, hasWindows, equipment) {
this.type = type;
this.size = size;
this.hasWindows = hasWindows;
this.equipment = ['esterillas', ...equipment];
};
};
class PilatesRoom extends Room {
};
const room1 = new PilatesRoom('pilates', 20, true, ['balón medicinal'])
console.log(room1);
//returns: PilatesRoom {type: "pilates", size: 20, hasWindows: true, equipment: Array(2)}
I mean... I don't really need to use "constructor" and "super" to make it works perfectly, but when I check it on the internet, everybody uses it. Should I? For example:
class PilatesRoom extends Room {
constructor(type, size, hasWindows, equipment) {
super(type, size, hasWindows, equipment)
};
};
This returns the same.
I'm trying to understand! Thank you guys for your time.
Share Improve this question asked Dec 6, 2018 at 13:13 SazLamasSazLamas 451 silver badge8 bronze badges 3- "when I check it on the internet, everybody uses it." - where and how many samples did you check? I could certainly find you counterexamples. – Bergi Commented Dec 6, 2018 at 14:43
- stackoverflow./q/48657481/1048572 – Bergi Commented Dec 6, 2018 at 14:48
- @SazLamas please mark one answer as accepted. every so-question should have one accepted answer. Thanks! – wuarmin Commented Sep 14, 2021 at 10:35
4 Answers
Reset to default 4You don't have to add a child class constructor if it doesn't add any logic. (In fact, static analysis and code quality tools sometimes flag it as a "useless constructor" and give a warning.)
Some programmers prefer the explicit definition of the constructor, some may carry over habits from other languages which may require it, etc. But unless the child constructor is actually doing something for the child class other than just passing the same values to the parent constructor, it's not necessary.
You have to use the super()
expession when you want to use a constructor of your child class. Otherwise you don't have to. As simple as that.
class PilatesRoom extends Room {
// the constructor should be removed (there is no point to keep it):
constructor(type, size, hasWindows, equipment) {
super(type, size, hasWindows, equipment)
};
};
In the above code, there is no reason to define a constructor. However in the below code you have to call foo()
and thus, you also have to use super()
:
class PilatesRoom extends Room {
constructor(type, size, hasWindows, equipment) {
super(type, size, hasWindows, equipment)
foo()
};
};
More info here: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/super#Description
As far as I know class constructor is just a syntactic sugar in javascript so you don't have to use it.
Also as MDN explain:
"The constructor method is a special method for creating and initializing an object created with a class. There can only be one special method with the name "constructor" in a class. A SyntaxError will be thrown if the class contains more than one occurrence of a constructor method.
A constructor can use the super keyword to call the constructor of the super class."
as it mentioned it can be there and or not.
MDN for further understanding
Less code is more. So if your child class doesn't have additional attributes and needs no special initialization logic, skip it. If you skip it you get the default constructor, which is totally sufficient.
You need to maintain every line of code. If there's no line there's no maintenance.
本文标签: classJavascript When do I have to use constructor in child classes declarationStack Overflow
版权声明:本文标题:class - Javascript. When do I have to use constructor in child classes declaration? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745646337a2161049.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论