JavaScript design mode: Singleton mode and javascript Design Mode
1. What is the js Singleton mode?
2. Functions and precautions of the singleton mode:
3. Sample Code:
<Script type = "application/javascript"> $ (document ). ready (function () {// 1. create two independent objects, jason and jerry; // 2. ask jerry to communicate with jason through the doorbell; // 3. check whether there is a door in jason's house. If there is a door that communicates with didi directly through the doorbell, if not, create a door first. // 4. communication between two Singleton instances/* immediate function object jason */var jason = (function () {var jason_home = function (message) {this. menling = message;} var men; var info = {sendMessage: function (message) {if (! Men) {men = new jason_home (message);} return men;} return info;}) ();/* object jerry */var jerry = {callJason: function (msg) {var _ jason = jason. sendMessage (msg); alert (_ jason. menling); _ jason = null; // wait for garbage collection}; jerry. callJason ("dingdong") ;}) </script>
In actual js development, Singleton is often used as the namespace:
<! DOCTYPE html>
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.