The understanding of JavaScript active Object--pseudo-Singleton mode

Source: Internet
Author: User

In their own study of JavaScript design patterns in the process, accidentally write a piece of code to understand the deeper, the reason is called pseudo-singleton mode, because the result of this code is very much like a singleton mode, but in fact, the active object caused by the misunderstanding.

The code is simple:

 function   person () { var  money = 0; Person.prototype.getMoney  = function   ()    { return   money; } Person.prototype.addMoney  = function   (    m) {Money  += m; }}  var  a = new   person ();  var  b = new   person (); A.addmoney ( 20 

Here are some students may feel very strange, A and B is completely two different objects, each new time will be the private variable money initialized to 0, why a object using the Addmoney () method, and the B object's money also becomes 20?

This time creates the illusion that the result of instantiating multiple objects through new is producing the same object, as if it were a singleton pattern.

If you're wondering about the result, then look at it, and if you see it at first glance, you don't have to waste your time here.

We start with this constructor, and here I am using VAR to create a private variable money, and in this constructor we provide two anonymous functions to the constructor of the prototype object Getmoney and Addmoney, that is, through the closure of the way to provide access to money variables, If you write the prototype object outside, you cannot access the private variable of money.

In fact, precisely because my behavior led to the occurrence of pseudo-singleton, we then use the graph to analyze what happens to the active object of the function when we new object.

If the function of the active object, the execution of the environment is not very understanding, give a portal http://www.docin.com/p-509501990.html, explained very clearly.

When we var a = new person (), this is the case:

Instance object A can invoke the two properties in the prototype object through the prototype chain to point to the two anonymous functions (the Circle of fans), who access the Money property of the active object one with the help of the closure. This time the Money property is initialized to 0.

When we var B = new Person (), the situation becomes this:

It is now clear that what really changed is the anonymous function that the attributes in the prototype object point to, which means that the money variables accessed by instance A and instance B through the access in the prototype are now in the active Object 2, and the active object 1 has been discarded after the bad play.

The next sentence of the A.addmoney (20) is actually the Activity object 2 in the money into 20, it is clear that the last two Getmoney access to the Activity object 2 inside the money, will naturally return 20. The result is a pseudo-singleton effect.

Summarize the points to note:

1, in order to achieve the effect of pseudo-singleton, the variables in the constructor must be set to a private variable rather than through this to the strength attribute, because that way access is not through the closure.

2, to add the prototype object inside the constructor to access the private variables, do not understand? I'm not going to take a close look at the closures!

3, the last object to access all is the last new, the constructor formed by the activity of the object, the students have doubts about this can exchange the order of code execution, such as change to this:

var New Person (); A.addmoney;//write in front var New Person (); Console.log (A.getmoney ());//Return to 0console.log (B.getmoney ());//Return 0

This is natural, because you added 20 of the money in the activity object one, when you create the Activity Object 2 o'clock, the instance object A has changed, A and B now point to the active Object 2, where money is initialized to 0, do not know more students look at the picture yo.

The understanding of JavaScript active Object--pseudo-Singleton mode

Related Article

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.