Discover learning javascript design patterns, include the articles, news, trends, analysis and practical advice about learning javascript design patterns on alibabacloud.com
Learning JavaScript design patterns: singleton and javascript Design Patterns
I. Definition
Ensure that a class has only one instance and provides a global access point to it.When you c
Learning JavaScript design patterns: class inheritance and javascript Design Patterns
Before doing one thing, you must first understand the benefits of doing this thing, and believe tha
system, and that this pattern can help them complete the reorganization for parts of the system that are not suitable for a particular purpose.Patterns that fall into this category include:
Decorator (decorator)
Facade (appearance)
Flyweight (enjoy Yuan)
Adapter (Adapter)
Proxy (agent)
Iii. Patterns of behaviorBehavioral patterns focus on improving or simplifying communication between d
) Function.prototype.call or Function.prototype.apply callis used to specify the pointer to the this object within the function.The difference between call and apply:A) The first parameter of apply specifies the point of the This object, and the second parameter is an indexed collection, which can be an array or an array of classes.var func=function(a,b,c) { alert ([A,b,c]); output [};func.applay](null//null points to the default host object, window in the browser. b) One of the parameters of
are also known as GoF23 design patterns.? Because the design pattern: the basis of reusable object-oriented software , the book determines the position of the design pattern, and the design pattern that people usually say implies "object-oriented
This article mainly introduces the "class inheritance" of JavaScript design patterns. This article uses code examples to explain the implementation of class inheritance, if you need a friend, you can refer to the following. Before doing one thing, you must first understand the benefits of doing this thing. I believe that no one is willing to do things for no reas
This article mainly guides you through the JavaScript design mode. It focuses on the singleton mode, illustrates the technology and disadvantages of Singleton mode, and analyzes the singleton mode in detail, if you are interested, you can refer to the singleton mode definition to ensure that a class has only one instance and provides a global access point to it.
The Singleton mode is a common mode. We usua
pattern is also known as the subscriber pattern.7. The command pattern is the way the method calls are encapsulated, and the method invocation can be parameterized and passed by the named schema, and then executed when needed.8. The responsibility chain pattern is used to eliminate the coupling between the sender and receiver of the request.Monomer (Singleton) modeThe way to use monomers is to use a single namespace to contain all of your own code global objectsFactory (Factory) modeProvides an
");
}
};
Function.prototype.after = function (fn) {var self = this;
return function () {var ret = self.apply (this, arguments);
if (ret = = "Nextsuccessor") {return fn.apply (this, arguments);
return ret;
};
var order = Order500.after (order200). After (Ordernormal);
Order (1, true, 10);
Advantage: Decoupling the complex relationship between the requesting sender and the n recipient.Disadvantage: There is no guarantee that a request will be handled by the nodes in the chain.
I
-UML class diagrams are often used to describe the static structure between classes when describing the design mode. The purpose of this article is to allow the readers to understand the UML class diagrams and to figure out several relationships in the UML class diagrams.
-In the subsequent design model learning process, only these two points are adequate. More a
, the Observer (or object) wishing to receive topic notifications must subscribe this int Erest to the object firing the event (the subject).The Publish/subscribe pattern however uses a topic/event channel which sits between the objects wishing to receive notific Ations (Subscribers) and the object firing the event (the publisher). This event system allows code to define application specific events which can pass custom arguments containing values need Ed by the Subscriber. The idea here's to av
1, the core of the singleton mode is to ensure that there is only one instance and provide global access.2. An inert singleton refers to creating an object instance when needed.such as creating a unique div in the page general practicevar Creatediv = (function () {var Div;return function () {if (!div) {div = document.createelement ("div");Div.style.width= "100px";div.innerhtml = "FDSAFDSAFDSAFDSA";Div.style.display = ' None ';Document.body.appendChild (DIV);return div;}return div;}})();document.
example as follows:functionCar (model, year, miles) { This. Model =model; This. Year =Year ; This. Miles =miles;} //Note Here, we are using Object.prototype.newMethod rather than//Object.prototype so as to avoid redefining the prototype ObjectCar.prototype.toString =function () { return This. Model + "have done" + This. Miles + "Miles";}; //Usage: varCIVIC =NewCar ("Honda Civic", 2009, 20000 );varMondeo =NewCar ("Ford Mondeo", 2010, 5000 ); Console.log (Civic.tostring ()); Console.log (mond
instance. It returns //a singleton instance of a singleton objectGetInstance:function(options) {if(Instance = = =undefined) {Instance=NewSingleton (options); } returninstance; } }; return_static;}) (); varSingletontest =singletontester.getinstance ({pointx:5}); //Log the output of pointx just to verify it is correct//Outputs:5Console.log (SINGLETONTEST.POINTX);Whilst the Singleton have valid uses, often when we find ourselves needing it in JavaS
In order to avoid contamination of global variables, it can be privatized with the addition of namespaces and closure packages.One, using namespacesChange a variable to a private variable under a namespacevarmyapp={}; Myapp.namespace=function(name) {varParts=name.split ('. ')); varCurrent=MyApp; for(varIinchparts) { if(!Current[parts[i]]) {Current[parts[i]]={}; } Current=Current[parts[i]]; } }; Myapp.namespace (' Event '); Myapp.namespace (' Dom.style '); Console.dir (MYAPP);Th
) {console. log ("general purchase, no coupons");} else {console. log ("inventory insufficient") ;}}; Function. prototype. after = function (fn) {var self = this; return function () {var ret = self. apply (this, arguments); if (ret = "nextSuccessor") {return fn. apply (this, arguments);} return ret;} var order = order500.after (order200 ). after (orderNormal); order (1, true, 10 );
Advantage: the complex relationship between request senders and N recipients is decoupled.Disadvantage: a request c
experience reach a certain level, and it also means to summarize a lot of good design experience, however, it may not be the case that "no teacher can do anything", or you can say that, on the basis of level and experience, it is time for the system to learn the "mode, it is helpful to learn the experts' summary and verify your own shortcomings.
This series of design p
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.