+ 3
Regarding javascript inheritance class.
I have a class "Commands" I want to create the method inside class "Commands" but I don't want to manually write method on Commands class , I want to import or inheritance like something to create method "run()" in class "Commands" . Later I want to access this by writing : var cmd = new Command ().run() R♡$€☆ I Am Rochie Vishnu Saini 💕₽ΔŘI💕[Inactive] Reetika Ria Sick!!Nikhat Shah (Dm Closed)
5 Réponses
+ 1
Ok i understand
this is what you should do:
class Parent {
run() {
return "something to return";
}
}
Parent.prototype.talk = function() {
return "something to say";
}
class Command extends Parent {
constructor() {
super();
}
}
var cmd = new Command().talk();
// how to do it
// write : ParentClass.prototype.methodName = function(){...}
// you should use prototypes
+ 2
NimaPoshtiban I want to add new method to parents class that I want to use later , how to add new method to the parent class without writing code inside parent class ? Please help me
+ 2
NimaPoshtiban thank you so much , you helped me lot
+ 1
this is how to do it:
class Parent {
run() {
return "something to return"
}
}
class Command extends Parent {
constructor() {
super();
}
}
var cmd = new Command().run();
// Desc:
// We define run function inside the parent class
// Then Command class should inherit from parent class
// in constructor we should call super() to inherit all parent class props
// side note: other OOP languages might have some different approaches
// There is no actual class in javascript , javascript uses prototypes concept
+ 1
I'm glad i could help , I really like programming concepts
you can try this online js playground for testing your codes
https://stephengrider.github.io/JSPlaygrounds/