Which one do you prefer to write JavaScript?

Source: Internet
Author: User

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

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.