SoFunction
Updated on 2025-04-07

JavaScript library development rules

1. Stay non-invasiveMy HTML tags don't want to know your JavaScript code.

2. Modification and extension are strictly prohibited!This is important, so a rule is needed that is entirely directed to it. Objects are the basic building blocks of JavaScript functions, don't mess with them.

3. Don't over-extendThe fewer extensions to JavaScript built-in objects, the better. Don't get me wrong. There are a little useful methods in JavaScript's native objects, and sometimes you have to add one or two of your own. However, for creative (library) programmers, adding this "one or two" method is not enough. However, please stop! Just add what you need. The less extensions you have to JavaScript built-in objects, the less likely your code will conflict with other frameworks.

4. Follow the standardsAs a libraryDevelopment, you define the pattern of JavaScript code. Design patterns are a manifestation of weak programming language capabilities. Remember, JavaScript and DOM are still being standardized. If you want to "correct" something, it's better to first see if it has been corrected and consider existing solutions. If you follow the standard, follow the standard closely (for example: don't miss a parameter of the forEach method).

5. Or follow the leadMozilla leads JavaScript. Brendan Eich, the creator of JavaScript language, continuesDevelopment. Compared to other browsers, these new language features are first available in Mozilla browser. If you want to add new language features to JavaScript, you can take a look at the Mozilla standard first. For example, when you want to extend an enumeration method to an Array object, it is best to name this method forEach instead of each. If you do want to provide language features that do not exist yet, follow the existing standards (see the example above).

6. Stay flexibleIf I want to modify the behavior without changing your source code, is it easy? If not easy enough, make it easier.

7. Manage memoryEveryone is worried about memory leaks, so you have to do your best.

8. Eliminate browser sniffingIt seems that browser manufacturers will always compete by adding new features;-) as a libraryDevelopment, you must keep up with the latest trends. Browsing Ajaxian once a while is not enough, you have to read every blog post like a slave to find the next hack. Browser sniffing will be addictive.

9. SmallerVarious JavaScript libraries are already mature. Some libraries are already applied to mainstream websites. But not everyone has 2MBit DSL bandwidth installed, so please keep the library small. A better approach is to provide a packaged page that allows you to effectively build your own library as I need.

10. Rule 10 Good ol' tenth rule (it seems to be a spoken usage, I don't know how to translate it well).You can always rely on the tenth rule. This is: Stay predictable. I should be able to guess what your method is for. If I don't know the name of a method, I should also be able to guess it.

11. Additional Rules
Documentation, annoying but definitely needed to do it.
The more namespace you use, the harder it will be for me to remember, just like your phone number.
Remember: there may be millions of people who will execute your code.