This blog is reproduced in: http://devbean.javaeye.com/blog/412296
Elegant encapsulation or execution efficiency? This is a paradox.
The elegant encapsulated program looks so beautiful: after each property is hidden in an object, what you can see is that this object makes
As you can see, you don't need to worry about how it operates.
The execution efficiency is another thing. It is like the difference between C language and object-oriented C ++: C ++ is elegant, but the execution efficiency
The usage of both compiled binary code and runtime memory is more than that of a simple C language.
This problem is more important in the scripting language, because Javascript is simply an interpreted language, the execution efficiency of the interpreted language
It is much lower than the compilation language.
1. Elegant Encapsulation
Let's take a look at variable encapsulation. The variables here are not only attributes, but also functions.
As we have already said before, JavaScript does not have the concept of class. We use the variable scope and closure to "cleverly simulate" it.
This is an elegant implementation. Let's take a look at the previous Code:
JS Code
Function person () {<br/> var ID; <br/> var showid = function () {<br/> alert ("My ID is" + id ); <br/>}< br/> This. GETID = function () {<br/> return ID; <br/>}< br/> This. setid = function (newid) {<br/> id = newid; <br/>}< br/> var P = new person (); <br/> P. setid (1000); <br/> alert (P. ID); // undefined <br/> // P. showid (); error: function not defined <br/> var P2 = new person (); <br/> alert (P. GETID = p2.getid); // false
We elegantly implement private variables-although opportunistic. But what is the problem with this code? Is
What are the functions of two objects?
Think about it. We use the scope of the variable to simulate the private variable and the closure to simulate the public variable. That is to say, the actual
Each created object on will have a copy of the same code! Not just that ID, but also those showid, GETID, etc.
Number is also created multiple times. Note that JavaScript Functions are just objects, so they won't be so strange. But there is no doubt
This is a waste: The difference between each variable is its own data domain, and the function code is the same, because what we do is
The same operation. Other languages generally do not encounter this problem, because the functions and object concepts in those languages are different, such
Java, the method of each object actually points to a copy of the same Code, rather than each object will have its own code copy.
2. Viewing Efficiency
The encapsulation is elegant, but it is a waste. Fortunately, JavaScript is a flexible language, so we immediately thought
Can some function pointers point to another function?
JS Code
Function show () {<br/> alert ("I'm a person. "); <br/>}< br/> function person () {<br/> This. show = show; <br/>}< br/> var p1 = new person (); <br/> var P2 = new person (); <br/> alert (p1.show = p2.show); // true
This method is good and solves our previous problem: different objects share a piece of code. However
Efficiency, but it is not elegant-if I have many classes, isn't there many global functions?
Fortunately, there is another mechanism in javascript: prototype. Do you still remember this prototype? Each object maintains
Prototype attribute. The prototype attributes of these objects are shared. Then, we can put the function definition
In prototype, so different objects do not share a copy of code? This is true:
JS Code
Function person () {<br/>}< br/> person. prototype. show = function () {<br/> alert ("I'm a person. "); <br/>}< br/> var p1 = new person (); <br/> var P2 = new person (); <br/> alert (p1.show = p2.show); // true
However, this separation definition looks awkward, so well, why not write the function definition into the class definition?
JS Code
Function person () {<br/> person. prototype. show = function () {<br/> alert ("I'm a person. "); <br/>}< br/> var p1 = new person (); <br/> var P2 = new person (); <br/> alert (p1.show = p2.show); // true
In fact, there is no difference between this writing method and the above method: the only difference is that the Code location is different. This is just a syntactic sugar that looks sweet, and there is no substantial difference.
Initially, Microsoft's. Net Ajax framework used the previous mechanism to simulate private variables and functions. This write method is very similar to C # And very elegant. However, for the sake of efficiency, Microsoft later changed it to the prototype definition method. Although this method is not so elegant, It is very efficient.
In JavaScript, there is always a conflict between the elegance of such encapsulation and the efficiency of execution. Now our best solution is to define data in the class, and define functions in the prototype attribute of the class.