JavaScript Library Development Rule _ javascript skills

Source: Internet
Author: User
1. Keep it non-invasive and mark the JavaScript code You Don't Want To Know; 2. prohibit modification and extension of Object. prototype !; 3. the fewer extensions of JavaScript built-in objects, the better. 4. follow the standard; 5. or follow the lead; 6. flexible; 7. memory Management; 8. eliminate browser sniffing; 9. smaller and better ...... 1. Keep non-invasiveMy HTML tag does not want to know your JavaScript code.

2. It is strictly prohibited to modify and extend Object. prototype!This is very important, so we need a rule for it. Objects are the basic building blocks of JavaScript Functions. Do not confuse them.

3. Do not over-ScaleThe fewer extensions of JavaScript built-in objects, the better. Do not misunderstand what I mean. There are a few useful methods in JavaScript native objects. Sometimes you have to add one or two methods of your own. However, it is not enough for creative (Library) programmers to add one or two methods. However, please stop! Add the required information. The less extensions you have on JavaScript built-in objects, the less likely your code conflicts with other frameworks.

4. Follow the standardsAs a library DevelopmentYou define the JavaScript code mode. The design mode is a manifestation of weak programming language capabilities. Remember, JavaScript and DOM are still being normalized. If you want to "modify" something, you 'd better first check whether it has been corrected and consider the existing solution. If you follow the standard, follow the standard closely (for example, do not omit a parameter of the forEach method ).

5. Or follow the leadMozilla leads Brendan Eich, the creator of the JavaScript. JavaScript language, and continues Development. Compared with other browsers, these new language features are first available in Mozilla browsers. If you want to add new language features to JavaScript, You can first look at the Mozilla standard. For example, if you want to extend an enumeration method to an Array object, you 'd better name this method forEach instead of each. if you really want to provide language features that do not exist, follow the existing standards closely (refer to the example above ).

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

7. Manage memoryEveryone is worried about memory leakage. You have to do your best.

8. Eliminate browser sniffingIt seems that browser vendors will always compete by adding new features;-) as the Library DevelopmentYou must keep up with the latest trends. Occasionally browsing Ajaxian is not enough. You must read every blog article as a slave to find the next hack. browser sniffing will be addictive.

9. Smaller and betterVarious JavaScript libraries have matured. Some libraries have been applied to mainstream websites. However, not all users have 2 Mbit DSL bandwidth, so please keep the database small. A better way is to provide a packaging page that allows you to effectively build your own library as needed.

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

11. Add Rules
Documentation, annoying but absolutely required.
The more namespaces you use, the more difficult it is to remember, just like your phone number.
Remember: millions of people may execute your code.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.