The disadvantage of JS closure package

Source: Internet
Author: User

Closures have many interesting uses, and the two features of JavaScript make it so interesting: 1. function is an object, which is equal to the array, object, and status. 2. JavaScript variable scope range. The JavaScript authoritative guide has an in-depth explanation of these two points.

A well-known use of closures is to implement object-oriented access control. That is, C + +, C #, Java and other object-oriented private, public access control. Look at the sample code first

 

<script type= "Text/javascript" >
function ClassA () {
var a = 4;
This.get_a = function () {
return A;
};
This.set_a = function (value) {
A = value;
};
}

var v = new ClassA ();
document.write (v.get_a () + "<br/>"); Showing 4
V.set_a (1);
document.write (v.get_a () + "<br/>"); Showing 1
Alert (V.A); Show undefined
</script>


The code above is very simple, just put the data member A's accessor (setter/getter), in the constructor. A using var declaration, the outside cannot directly access a. Both Get_a and set_a use this to associate the two functions with the class object and are visible outside.

The method above is very technical, but I have never done so, Microsoft's ajax.net also did not use closures to implement access control. The reason is that it takes up more memory, and here's why it takes up more memory.

First, we use prototypes (prototype) to define a class

<script type= "Text/javascript" >
function ClassA () {
}
Classa.prototype = {
Fun1:function () {
document.write ("prototype fun1 <br/><br/>");
}
}

var a = new ClassA ();
var B = new ClassA ();
A.fun1 (); Prototype FUN1
B.fun1 (); Prototype FUN1
CLASSA.PROTOTYPE.FUN1 = function () {
document.write ("Modify function<br/><br/>");
}
A.fun1 (); Modify function
B.fun1 (); Modify function
A.FUN1 = function () {
document.write ("Modify a ' s method<br/><br/>");
}
A.fun1 ();
B.fun1 ();
</script>


As can be seen from the running results above, the instantiated objects share the same method using the class defined by prototype, and once the prototype has changed, it affects all objects. When an object modifies its own method, the system executes a copy on write, pointing the object to the new method without affecting the other objects.

Let's look at the following paragraph:

<script type= "Text/javascript" >
function ClassA () {
THIS.FUN1 = function () {
document.write ("Object function <br/>");
};
}
var a = new ClassA ();
var B = new ClassA ();
A.fun1 ();
B.fun1 ();
CLASSA.PROTOTYPE.FUN1 = function () {
document.write ("Modify object function <br/>");
}
A.fun1 ();
B.fun1 ();
A.FUN1 = function () {
document.write ("Modify a ' s function <br/>");
}
A.fun1 ();
B.fun1 ();
</script>


As you can see from the running results, the result of the closure is that each object has a separate method, even if the method is implemented exactly the same way. This can result in unnecessary memory waste. Imagine how much memory would be wasted if a class instance that uses closures has many objects.

The disadvantage of JS closure package

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.