Which of the following statements about JavaScript do you prefer?

Source: Internet
Author: User
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.

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.