The singleton mode, which is a kind of creation design pattern, is also called the monomer pattern, which is only allowed to instantiate the object class once. Sometimes it is also used to plan a namespace.
1 varUtil = {2GetName:function () {3Console.log (' WP ')4 },5Getage:function () {6Console.log (25)7 },8GETALL:function () {9 This. GetName ();Ten This. Getage (); One } A } -Wang.getall ()
In addition to defining namespaces, you can manage the individual modules of the code base.
Cases:
1 varUtil = {2 Tool: {3Tool1:function () {4 ...5 },6Too2:function () {7 ...8 }9 },Ten Ajax: { OneGetfunction () { A ... - }, -Postfunction () { the ... - } - }, - Animate: { +Movefunction () { - ... + }, ARotatefunction () { at ... - } - } - } -Util.Tool.tool1 ();
exceptions, using singleton mode, you can also set static variables that cannot be modified
var Conf = (function () { var conf = {conf_ A: 100 200 300} return {get: function return conf[name]? Conf[name]: null }}) (); var conf_a = conf.get (' conf_a ' //
Description: Because static variables are defined in the function object, and only the Get method is provided inside the function, these static variables can only be accessed externally through privileged methods, but these static variables cannot be modified
JavaScript design mode-Singleton mode