Adding custom properties to a function

后端 未结 10 779
独厮守ぢ
独厮守ぢ 2020-11-27 10:34

Searching for appropriate answer proved difficult because of the existence of many other problems related to my keywords, so I\'ll ask this here.

As we know, functio

10条回答
  •  执笔经年
    2020-11-27 10:57

    It's perfectly acceptable to add properties or methods to a function object. It's done quite often. The jQuery/$ object is an example of this. It's a function with quite a few methods attached.

    When properties are added to a constructor they are called 'static' properties and can be invoked without an an instance of the class. e.g. Object.create.

    I don't have enough rep to write a comment so I will say here: It generally considered bad practice to extend the prototypes of built in objects, especially if your code has to play with other people's code. It can have unpredictable consequences that are hard to to track.

提交回复
热议问题