Mind Map
Introduction: I am always writing Javascript. How do you write your JS? Which method do you prefer? Today, I am nagging you about the common ones. -- If you are older, 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,CodeCompact.
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!
The object expression method is similar 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
In summary, when I write js code, I like to write it myself, and I do not like to use the frameworks on the Internet. -- First, you are familiar with your code (I know what is in it when someone asks me), and second, your 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. If Article This is helpful to you. Please recommend it to your friends and give them motivation for writing.