A module pattern of Javascript _ YUI. Ext

Source: Internet
Author: User
In YUI, we only use two global variables: YAHOO and YAHOO_config. YUI uses YAHOO object-level members or variables in the member scope. We recommend that you use similar rules in your application. Douglas Crockford has taught you a useful singleton pattern to implement this rule. I think his pattern is useful for your YUI-based applications. Douglas calls it module pattern ). It works as follows:

Create a namespace object: If you use YUI, you can use YAHOO. namespace () method: YAHOO. namespace ("myProject"); this is assigned an empty myProject object, which is a member of YAHOO (if myProject already exists, it will not be overwritten ). Now we can add members of YAHOO. myProject.
Assign an anonymous function return value to your namespace object:
YAHOO. myProject. myModule = function (){
Return {
MyPublicProperty: "I was accessed as YAHOO. myProject. myModule. myPublicProperty. ";
MyPublicMethod: function (){
YAHOO. log ("I was accessed as YAHOO. myProject. myModule. myPublicMethod. ");
}
};
} (); // This bracket causes anonymous functions to be executed and returned
Note that there are closed braces and the last line of the followed parentheses ()-This symbol causes immediate execution of anonymous functions and returns objects containing myPublicProperty and myPublicMethod. As soon as this anonymous function returns, the returned object is accessed as YAHOO. myProject. myModule.

Add the "private" method and variable before the return statement to the anonymous function. So far, we have allocated myPublicProperty and myPublicMethod directly to YAHOO. myProject. myModule. In addition, when we place some code before the return statement, this mode also supports the added utility.
YAHOO. myProject. myModule = function (){
// "Private" variable:
Var myPrivateVar = "I can only be accessed in YAHOO. myProject. myModule .";
// Private method:
Var myPrivateMethod = function (){
YAHOO. log ("I can only be accessed in YAHOO. myProject. myModule .");
}

Return {
MyPublicProperty: "I can be accessed as YAHOO. myProject. myModule. myPublicProperty ."
MyPublicMethod: function (){
YAHOO. log ("I can be accessed as YAHOO. myProject. myModule. myPublicMethod .");
// In myProject, I can access private variables and methods.
YAHOO. log (myPrivateVar );
YAHOO. log (myPrivateMethod ());
// The native scope of myPublicMethod is myProject. We can use "this" to access public members.
YAHOO. log (this. myPublicProperty );
}
};
} (); In the above Code, we return an object with two members from an anonymous function. You can use this. myPublicProperty and this. myPublicMethod to access the YAHOO. myProject. myModule. Outside YAHOO. myProject. myModule, public members can access it using YAHOO. myProject. myModule. myPublicProperty and YAHOO. myProject. myModule. myPublicMethod.
Private variables myPrivateProperty and myPrivateMethod can only be accessed by anonymous functions or members of returned objects. Although anonymous functions are executed and terminated immediately, they are retained. With the power of closure, the Partial Variables of a function are retained after the function is returned. As long as YAHOO. myProject. myModule needs them, our two private variables will not be destroyed.

Practice this mode. Let's take a look at a common application case of this model. If you have a list, some items in the list can be dragged. The drag-and-drop CSS class is displayed on the items dragged by the application.

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.