Association between singleton mode and new Mode
I read a very good book called "javascript design mode and development practice" two days ago, which was also recently published. The design mode here allows us to learn how to write highly readable data, code with high reusability.
In the first chapter, we can see the singleton mode. Although we have encountered this mode before, we usually put the creation object together with the logic of controlling the Singleton, so the code looks like this:
var getDiv=function(){var divCache;getDiv=function(){if(divCache){return divCache;}return divCache=document.createElement("div");;}return getDiv();}
The logic for creating a div and controlling a singleton is completely intertwined. If we do not need a Singleton, but need a new div each time, then you must re-write a function to create a div.
In fact, there is something that can be optimized here. Although we use closures to implement Singleton, every time we create a div, we determine whether divCache exists. In fact, we can change it to this:
var getDiv=function(){var divCache;getDiv=function(){return divCache;}return divCache=document.createElement("div");;}
In this way, the point of the getDiv is changed during the first call. When this function is called every time, the speed is optimized.
However, next time we don't want to get a div, I want to get a span, an object, or next time I don't want to use a singleton. What should I do? Therefore, we must separate the logic of the created object from the Singleton, so that the singleton can be used on any object, instead of directly calling the function of creating the object when using the Singleton.
The example of creating a singleton function looks like this:
function getSingle=function(fn){var cache;return function(){return cache||fn.apply(this,arguments);}}
We changed the div creation function to the following:
var getDiv=function(){return document.createElement("div");;}
We changed the div created in singleton mode to the following:
var getSingleDiv=getSingle(getDiv);
In this way, the singleton mode is perfectly realized. Here, the singleton mode is basically understood, but the problem arises, how can we use the singleton mode to create objects using Constructors?
For example, there is a class:
function People(name){this.name=name;}We must use new to get an object. How can we use the singleton mode?
The answer is that there is no way. Only the new method can be abandoned. At least I think so. There are several ways to circumvent new:
Method 1:
function People(name){if(!(this instanceof People)){return new People;}this.name=name;}
Method 2:
getObject=function(cla){return function(){function _(){};_.prototype=cla.prototype;var obj=new _();cla.apply(obj,arguments);return obj;}}var getPeople=getObject(People);
Of course, I recommend the second method. The advantage is self-evident. The first method needs to be determined every time it is created. The second method does not know whether someone has tried this method before, I figured it out myself. Hey, I personally think the advantage is quite obvious.
The following explains why the second method is like this:
Let's take a look at a negative textbook. Most people may think this is the case:
getObject=function(cla){return function(){return new cla();}}But the problem arises. What should we do with the parameters? In this way, the parameters cannot be passed in. We all know that the apply method is used to pass parameters, but the apply method cannot be used by classes. Only functions can use the apply method. The following method is incorrect:
var a=new People.apply(obj,"liyong");
The error here means that the apply function is not a constructor, And it is positive. The above is equivalent to: new apply (), and apply is a function, not a constructor, so how can we use apply?
It must have been my first method, but it is also a factory method. First, use a useless class, and then change the class prototype to the class, in this way, the prototype method of the given class is obtained, and the apply method is used for private attributes. Here we need some js Object-Oriented Knowledge, if you don't understand it, you can flip through the js object-oriented book. I will not explain it one by one.
Now, let's talk about it. Thank you!