Mind Map
IntroductionI am always writing js. How do you write your js? Which method do you prefer? Today, I am nagging you about the common ones. -- If you are older, you also like to talk nonsense about this hobby.) Don't bother me!
Direct Initialization
Advantages:
1. userData is an object instance. -- A class is instantiated when it comes up, and the browser cannot stand it.
2. Compact code.
3. High programming efficiency.
Disadvantages:
1. code reusability is relatively poor.
2. It does not conform to the object-oriented programming idea. -- The design pattern is often used in the class. This method can be used directly on objects!
Conclusion: Do not use this method to create custom objects.
Use Case: QQ mini pop-up window
Constructor Method
Usage: You need to use "new and constructor" to create an instance.
Advantages:
Memory is allocated only when new is used. If it is not new, the browser will never give you memory resources for the rest of your life. -- Ignore her if you want!
Object expression methodSimilar to the first "direct initialization" method. -- It is actually a variant.
The red part above makes people look dizzy. -- This Code allows anyone to read it. It is estimated that all of them will vomit blood!
Anonymous Function Method
This is often used by me. Look at this code and use two words to describe it. It's "pretty" and I'll give it two more words, "Pretty pretty"
The one above is often used by me. I have written a bunch of extensions in the project. With this stuff, I feel a lot of excitement.
Case: jQuery
Summary
When I write js code, I like writing my own code, and I do not like to use the current online framework. -- First, I am familiar with my own code. Others ask me, and I know what is in it.) Second, my framework is more suitable for existing projects.
However, I usually pay more attention to these frameworks. Generally, I want to see how these frameworks are written, and then I will put them into my own projects. Which of the above methods do you prefer when writing js Code? Or do you have a better method? Let us know one or two.
Address: http://www.cnblogs.com/baochuan/archive/2012/04/30/2473771.html