Meaning of prototype in javascript

前端 未结 6 976
别跟我提以往
别跟我提以往 2021-02-02 12:10

I wrote short code of inheritance of reader from Person:



        
6条回答
  •  礼貌的吻别
    2021-02-02 12:57

    The difference is that is you further extend the Person class the sub classes will not inherit the getName() method

    Edit: I was not correct in above statement. Just tested on the jsfiddle. Regardless of if we define a method on the prototype or on the function instance itself, it is available for the subclasses in the chain.

    Here is the proof: http://jsfiddle.net/u8qrd/

    I understand that there is a performance/memory benefit of attaching the methods to prototype. Appart from that isn't there any behavioral difference when it comes to inheritance?

    (hopefully I'm not violating SO rules by asking a question here)

提交回复
热议问题