Javascript object members that are prototyped as arrays become shared by all class instances

前端 未结 3 801
日久生厌
日久生厌 2020-11-22 08:40

Has anyone noticed this behavior before? This really threw me off... I would have expected prototyped arrays to be private to each class instance rather than shared between

3条回答
  •  误落风尘
    2020-11-22 09:17

    The behaviour is correct. [] is tranlated to new Array() in runtime, but only one such array is ever created.

    In other words, Obj.prototype = {...} is executed just like any other assigment.

提交回复
热议问题