Why use chained prototype inheritance in javascript?

后端 未结 3 1158
礼貌的吻别
礼貌的吻别 2021-02-08 10:05

perf

Why do we build a prototype inheritance chain rather then using object composition. Looking up through the prototype for each step in the chain get\'s expensive.

3条回答
  •  天命终不由人
    2021-02-08 10:49

    The main reason would have to be changes to the prototype object. A change to an ancestor object will be reflected across the entire chain. This could, conceivably, be a benefit. Though I can't immediately think of any real-world instances, I think embracing this dynamic nature could provide a dynamic that other (read: class-based) languages simply don't provide.

    Objects further up the prototype chain could evolve as needed across the lifetime of an application, and those changes would be reflected across all descendant objects. This could be easily combined with JavaScript's functions as first-class objects to dynamically modify functionality as needed.

    That said, if this functionality is not necessary, there is no reason to use the prototype chain over composition.

提交回复
热议问题