What are the differences between these three patterns of “class” definitions in JavaScript?

前端 未结 2 1209
花落未央
花落未央 2020-12-16 15:21

Are there any important/subtle/significant differences under the hood when choosing to use one of these four patterns over the others? And, are there any differences betwee

2条回答
  •  鱼传尺愫
    2020-12-16 15:50

    The first example puts the move function in the prototype which will be shared between all Animal instances.

    The second example creates a new move function for every the animal instance.

    The third example generates a Animal class with the move function in the prototype similar to the first example but with allot less code. (In your example the name is also shared between all instances, which you probably don't want)

    Putting the function in the prototype makes instantiating Animals faster, and because of the way JIT engines work even the execution of the function is faster.

提交回复
热议问题