Lesson NOTES:--Creating an object's singleton pattern

Source: Internet
Author: User

Individual instances, individual individual---Singleton mode

Long long ago, there was only a basic data type in JS.
I want to describe a person
var name = "Side East strategy";
var age = 28;
var height = "148cm";
var weight = "200kg";

I want to describe the second person.
var name = "Li Jianjie";
var age = 18;
var height = "188cm";
var weight = "60kg";

Now that we have a problem, the information we described later covers the first person's information. The conflict between variables and the pollution of global variables

In order to put together the characteristics that describe the same thing, and to avoid conflicts with other things, we introduce the object data type---> We call this writing pattern "singleton mode".
1) Implement grouping--and the same "namespace" (opening a space with a name) the singleton pattern is to put all the attributes and methods of describing this thing under the same namespace, realizing the function of grouping under the namespace.
2) Avoid conflicts--different namespaces are non-impact
var Person1 = {
Name: "Side East Strategy",
Age:28
};
var Person2 = {
Name: "Li Jianjie",
Age:18
};
Console.log (Person2.name);
Console.log (Person1.age);

We can implement the simplest modular development in a real project by applying a singleton pattern
"Modular Development": A site according to the function divided into a few chunks, and then by a team of multi-person collaboration between the development, everyone responsible for the corresponding parts, and finally in the code to merge, compress, upload ...

Lesson NOTES:--Creating an object's singleton pattern

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.