Meaning of prototype in javascript

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

I wrote short code of inheritance of reader from Person:



        
6条回答
  •  被撕碎了的回忆
    2021-02-02 12:49

    When you add any function with this.functionname to any object constructor every object created by that constructor makes their own copy of that function which also takes memory. Imagine if you have several objects created by same constructor and how much memory it will take. on other hand when you creates it with cunstructorName.prototype.functionName function loads only once in memory and every object shares same prototype function constructor. this approach makes your code faster in loading and operation and also saves a big chunk of memory.

提交回复
热议问题