Does some JavaScript library use dynamic aspects of the prototype system?

前端 未结 3 1788
醉梦人生
醉梦人生 2020-12-12 03:32

JavaScript object created with a prototype maintains \"live\" connection to its prototype, so that changing the prototype also affects the objects created from it.

T

3条回答
  •  猫巷女王i
    2020-12-12 04:06

    Surely. I think the most prominent use of this feature is the dynamical adding of methods to the jQuery prototype (called "plugins"), which is exposed as jQuery.fn.

    I can't understand why you think "the semantics of the language would be much simpler if newly created object just copied the prototype properties". Why would that be simpler? You can create objects with that method, using a extend() function, but that knocks the concept of prototypical inheritance on the head.

    It is one of the core features to have that non-static inheritance in Javascript. It is useful to modify the prototype object of already generated instances for:

    • adding features, like described above. You can load whole plugins only if you need them.
    • enhance features (sometimes). This is often used to overwrite non-standard behaviour
    • really change them (seldom). This can be used with configuration objects, which inherit from an object with the default settings. Then you can change the default settings, and they will apply to all instances (at least to them which didn't overwrite the property in question).

提交回复
热议问题