Module mode in Javascript Mode

Source: Internet
Author: User

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.

 

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.