Answer the question
In order to leave comments, you need to log in
Prototype or method?
Hello! I wanted to know how prototype differs from a method that can be created in a constructor?
Answer the question
In order to leave comments, you need to log in
In the first case, the function will belong to the prototype. Will be created once. Will be common to all instances.
In the second, the function will be owned by the instance, will be created each time.
class MyClass {
constructor() {
this.constFunc = () => {}
}
methodFunc() {}
}
const e1 = new MyClass
const e2 = new MyClass
e1.constFunc === e2.constFunc // false
e1.methodFunc === e2.methodFunc // true
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question