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
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.