Defining a Javascript prototype

后端 未结 5 1834
陌清茗
陌清茗 2020-11-22 10:05

What are the functional differences between the following two Javascript prototypes, and are there any benefits for choosing one over the other?

Option 1:

5条回答
  •  礼貌的吻别
    2020-11-22 10:41

    The first is good for one or two extra functions, but defining a totally new prototype with many functions would be very repetitive. On the other hand, doing the latter would destroy all the existing definitions for the prototype as you mentioned.

    In practice, I have used the first to define additional functions in Array and Math, etc., somewhat like categories in Objective-C. The latter I use as a "class definition."

提交回复
热议问题