What is difference between define function by prototype and class property?

后端 未结 2 2047
醉话见心
醉话见心 2020-12-12 16:21

Follow my code,
Apple is define function by prototype.
Banana is define function by class property.

var Apple = function(){}
Apple.prototype.say = fu         


        
相关标签:
2条回答
  • 2020-12-12 16:49

    When you create more than one instance of Apple, you will still only have only one instance of say() in memory. However, when you create more than one instance of Banana, you will end up creating lots of instances of the say() function.

    That's why prototypes save memory. You also avoid the processing cost of creating and assigning the say() function.

    Also, if you change the parent object's properties, if the child does not replace that property, changes are visible from the child.

    0 讨论(0)
  • 2020-12-12 16:55

    prototype members are like class membeprototype members are like class member, while when u define it other way its not a class member. So if you are creating lot of object of Apple all will be sharing same function, while in case of banana, every object will have their own copy of function. Think prototype in javascript as static in C#.

    0 讨论(0)
提交回复
热议问题