JavaScript design mode--constructor

Source: Internet
Author: User

The module mode is a block of code that provides public and private methods, facilitates encapsulation of organization code, and reduces the conflict between variables and function names against other modules.

Recommended reading:

Http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html

http://toddmotto.com/mastering-the-module-pattern/

The module model is varied and I only list the module patterns that are commonly used, or are relatively perfect or have special uses.

1. Revealing module mode with private variable plus "_"

var Module = (function  () {  varfunction  () {    //  Private Stuff  };   var function () {    _privatemethod ();  };     return {    publicmethod:publicmethod  };}) ();

All functions are placed inside the function, and the private function or variable is preceded by "_", and the public method uses the same name as the return object to directly reference the function inside. This is a very clear structure.

2. Mode of Extension Module

var module = (function  (Module) {        function  () {        //  Another method !     };         return Module;    | | {});

The module can be extended by introducing the previous module.

JavaScript design mode--constructor

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.