JavaScript constructors: JavaScript learning Notes (24) module Create construct letter

Source: Internet
Author: User
Tags array object constructor execution variables return variable

Module Creation Constructor (Modules that create constructors)
In the previous example, a MYAPP.utilities.array object was created, but sometimes it is more convenient to create your object using the constructor, and you can implement it using the module pattern.
The only difference is that the immediate execution function of the package module returns a function instead of an object.
The following example is a module pattern to create a constructor MYAPP.utilities.Array
Myapp.namespace (' MYAPP.utilities.Array ');
MYAPP.utilities.Array = (function () {
Dependencies
var uobj = MYAPP.utilities.object,
Ulang = MYAPP.utilities.lang,
Private properties and methods ...
Constr;
End Var
Optionally one-time init procedures
// ...
Public API--constructor
Constr = function (o) {
this.elements = This.toarray (o);
};
Public API--prototype
Constr.prototype = {
Constructor:MYAPP.utilities.Array,
Version: "2.0",
Toarray:function (obj) {
for (var i = 0, a = [], Len = Obj.length i < len; i = 1) {
A[i] = Obj[i];
}
return A;
}
};
Return the constructor
To being assigned to the new namespace
return constr;
} ()); How to use this construction method:
var arr = new MYAPP.utilities.Array (obj);
Import global variables into modules (importing Globals into a module)
In a variant of the generic modular model, you can pass parameters to the package module to execute the function immediately. You can pass any value, but usually a reference to a global variable or even a global variable itself.
Importing global variables accelerates the resolution of global symbols in an immediate execution function, because the imported global variable becomes a local variable of the function:
MYAPP.utilities.module = (function (app, global) {
References to the Global object
And to the Global app namespace object
are now localized
} (MYAPP, this));
This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20130710/39008.html

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.