During Modular programming, many projects adopt the module mode because it provides structured ideas and helps organize the growing code. Javascript, C ++, Java, and other object-oriented languages are different. It does not have the syntax for importing packages ,, however, the module mode provides a powerful tool for creating self-contained uncoupled code snippets. This is why many projects adopt this model.
Now, let's get started. The module mode is actually a combination of the following modes:
I. namespace Mode
Ii. Real-Time Functions
3. Declare dependency
4. Private and privileged members
1. Create a namespace Function
/*** Namespace (STR) * @ Param {string} string * @ returns {object} object */var ZJ ={}; ZJ. namespace = function (STR) {var o = ZJ; var arr = Str. split (". "); For (VAR I = (ARR [0] = 'zj '? 1:0), Len = arr. length; I <Len; I ++) {o = O [arr [I] = O [arr [I] |{};} return o ;} ZJ. namespace ("ZJ. util. dom ");
II. The following uses real-time functions to provide private scopes. The real-time functions return an object, that is, an actual module with a public interface. Therefore, these modules can be used through this interface.
ZJ. util. DOM = (function () {return {GETID: function (ID) {return document. getelementbyid (ID) ;}, getclass: function (classname) {var arr = document. getelementsbytagname ('*'); For (VAR I = 0, Len = arr. length; I <Len; I ++) {If (ARR [I]. getattribute ('class') = classname) {return arr [I] ;}else {return 'do not have this object ';}}}}})();
3. Add some private attributes and privileged methods:
ZJ. util. DOM = (function () {// Private Property VaR _ name = 'zhansan', _ id = 'lisi'; // Private method function _ XXX () {}// defines some var variables return {GETID: function (ID) {return document. getelementbyid (ID) ;}, getclass: function (classname) {var arr = document. getelementsbytagname ('*'); For (VAR I = 0, Len = arr. length; I <Len; I ++) {If (ARR [I]. getattribute ('class') = classname) {return arr [I] ;}else {return 'do not have this object ';}}}};})();
4. Add declarative Dependencies
ZJ. namespace ("ZJ. util. array "); var module = ZJ. namespace ("ZJ. util. dom "); module = (function () {// declare dependent var arr = ZJ. util. array; // Private Property VaR _ name = 'zhansan', _ id = 'lisi'; // Private method function _ XXX () {}// defines some var variables return {GETID: function (ID) {return document. getelementbyid (ID) ;}, getclass: function (classname) {var arr = document. getelementsbytagname ('*'); For (VAR I = 0, Len = arr. length; I <Len; I ++) {If (ARR [I]. getattribute ('class') = classname) {return arr [I] ;}else {return 'do not have this object ';}}}};})();
5. Import global variables into the module
ZJ. namespace ("ZJ. util. array "); var module = ZJ. namespace ("ZJ. util. dom "); module = (function ($, APP) {// declare dependency var arr = ZJ. util. array; // Private Property var name = 'zhansan', id = 'lisi'; // Private method function XXX () {}// defines some var variables return {GETID: function (ID) {return document. getelementbyid (ID) ;}, getclass: function (classname) {var arr = document. getelementsbytagname ('*'); For (VAR I = 0, Len = arr. length; I <Len; I ++) {If (ARR [I]. getattribute ('class') = classname) {return arr [I] ;}else {return 'do not have this object' ;}}}) (jquery, MyApp );
This article is my own summary of the past few days. If you see this article in your peers, if you have different opinions, please leave a message.
Reprinted please indicate the source: always believe that sharing can improve yourself and achieve others, I'm Ben, has been on the road, never dare to neglect.