E
E
Eugene4542020-06-15 09:05:35
JavaScript
Eugene454, 2020-06-15 09:05:35

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

1 answer(s)
A
Alex, 2020-06-15
@Eugene454

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 question

Ask a Question

731 491 924 answers to any question