Adding custom properties to a function

后端 未结 10 786
独厮守ぢ
独厮守ぢ 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条回答
  •  萌比男神i
    2020-11-27 11:12

    "necromancing" here, but I think every great question needs simple answers:

    Yes and Yes*

    By attaching the properties to the function you clean up the scope, improve readability and add logical cohesion. An added benefit is that you document the relationship between the function and the variables. I think that's a superior design, much better than adding variables on the scope some examples of attaching properties to instances of functions

    Created some fun examples here and here. HERE AND HERE


    * I think it's worth noting that you probably won't see this very often. most developers probably don't realize it's possible. Some people are crazy about every drop of performance... "JavaScript engines optimize based on the 'shape' of an object'..." blah blah blah... ut I think you can follow the rule you have for Objects and you'll do fine.

提交回复
热议问题