0

i am having some issues learning class methods syntaxes... there are a few and I dont know if they have different behaviours or if they are just equivalent/updated versions of one another.

is my thoughts real?

class Person { 
    constructor(name) { 
        this.name = name
    }

    instanceMethod() { // this.
        console.log('hello there')
    }
}

Person.prototype.instanceMethod = () => { //equivalent to this?
}

const person = new Person()
person.instanceMethod // so we call the "person.prototype.instanceMethod'' in the instance of the class 

// ... //
class Person {
    constructor(name) {
        this.name = name
    }

    static staticMethod() { // this.
        console.log('hello there')
    }
}

class OtherPerson extends Person {
}

Person.staticMethod = () => { // is equivalent to this?
}

const person = new Person()
Person.staticMethod() // so we call the static method in the Parent Class
OtherPerson.staticMethod //or a child class of that Parent Class.

or to call in the child class of the class you have to provide their own static methods?

class OtherPerson extends Person {
(... constructor and stuff...)

  staticMethod() { console.log('hello there') } // defining their own static methods.
  staticMethod() { super.staticMethod() } //or make this as well... both with same results
}
nishi
  • 377
  • 1
  • 13
  • 1
    [You cannot use arrow functions as methods](https://stackoverflow.com/q/31095710/1048572), so it would be `… = function(){ … }`, but yes instance methods are still placed on the prototype and static methods are still placed on the class constructor itself. – Bergi Aug 23 '20 at 17:46
  • 1
    And yes, unlike in ES5, `class`es do inherit static methods as well, you don't have to redefine it on `OtherPerson`. – Bergi Aug 23 '20 at 17:48
  • oh yeah. i forgot about the arrow function xD So my thoughts are right! Thanks! You could answer my questions instead of comment so i would evaluete as elegible. – nishi Aug 23 '20 at 18:07

0 Answers0