This article presents an example of the advanced usage method for JavaScript closures, with the need for friends to refer to
Extended
Code:
Copy Code code as follows:
var blogmodule = (function (my) {
My. Addphoto = function () {
//Add internal code
};
return to my;
} (Blogmodule));
Say:
It passes itself into the method, and then implements the extension of the method, sort of like part assembly.
Code:
Copy Code code as follows:
var blogmodule = (function (me) {var oldaddphotomethod = My. Addphoto;
My. Addphoto = function () {//overloaded method, still able to invoke old side} via Oldaddphotomethod; return to my;} (Blogmodule));
Say:
The advantage is that you can call the previous method.
Cloning and inheritance
Code:
Copy Code code as follows:
var Blogmodule = (function (old) {var i = {}, key; for (key) {if (Old.hasownproperty (key)) {My[key] = Old[key]; } var Oldaddphotomethod = old. Addphoto; My. Addphoto = function () {//After cloning, the rewrite, of course, you can continue to invoke Oldaddphotomethod}; return to my; } (Blogmodule));
Say:
A simple cloning implementation
Share private objects across files
Code:
Copy Code code as follows:
var blogmodule = (function (my) {var _private = My._private = My._private | | {}, _seal = My._seal = My._seal | | function () {delete my._private; delete my._seal; delete my._unseal;}, _unseal = My._unseal = My._unseal | | function () {my._private = _private; my._seal = _seal; my._unseal = _unseal;}; return to my; } (Blogmodule | | {}));
Say:
Blogmodule._seal () lock, _unseal () unlock, to achieve the privatization of internal variables. I don't think it's the best way to do this, but we can learn the lock-locked function.