Explore JavaScript: elegant encapsulation or execution efficiency? (1)

Source: Internet
Author: User

An elegantly encapsulated program looks so beautiful: after each property is hidden in an object, what you can see is what this object shows you, and how it operates, you don't need to worry about this.

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, whether it is the binary code after compilation or the memory usage during runtime, it is more than a simple C language.

This problem is more important in the scripting language, because JavaScript is simply an interpreted language, and the execution efficiency of the interpreted language is much lower than that of the compiled language.

1. Elegant Encapsulation

Let's take a look at variable encapsulation. The variables here are not only attributes, but also functions. As mentioned above, JavaScript does not have the class concept. It is an elegant implementation that uses the variable scope and closure to "cleverly simulate" it. Let's take a look at the previous Code:

 
 
  1. function Person() {     
  2.     var id;     
  3.     var showId = function() {     
  4.         alert("My id is " + id);     
  5.     }     
  6.     this.getId = function() {     
  7.         return id;     
  8.     }     
  9.     this.setId = function(newId) {     
  10.         id = newId;     
  11.     }     
  12. }     
  13. var p = new Person();     
  14. p.setId(1000);     
  15. alert(p.id); // undefined     
  16. // p.showId(); error: function not defined     
  17. var p2 = new Person();     
  18. alert(p.getId == p2.getId); // false    
  19.  
  20. function Person() {  
  21.     var id;  
  22.     var showId = function() {  
  23.         alert("My id is " + id);  
  24.     }  
  25.     this.getId = function() {  
  26.         return id;  
  27.     }  
  28.     this.setId = function(newId) {  
  29.         id = newId;  
  30.     }  
  31. }  
  32. var p = new Person();  
  33. p.setId(1000);  
  34. alert(p.id); // undefined  
  35. // p.showId(); error: function not defined  
  36. var p2 = new Person();  
  37. alert(p.getId == p2.getId); // false  

We elegantly implement private variables-although opportunistic. But what is the problem with this code? Why are the functions of two objects different?

Think about it. We use the scope of variables to simulate private variables and the closure to simulate public variables. That is to say, in fact, each created object will have a copy of the same code! Not only that id, but also those functions such as showId and getId will be created multiple times. Note that JavaScript Functions are just objects, so they won't be so strange.

But there is no doubt that this is a waste: each variable is different from its own data domain, and the function code is the same, because we perform the same operation. Other languages generally do not encounter this problem, because the functions and object concepts of those languages are different. For example, Java, the methods of each object actually point to copying the same code, instead, each object will have its own code copy.


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.