This article mainly helps you understand and learn javascript encapsulation, and forcibly implement information hiding through encapsulation. This article analyzes the advantages and disadvantages of encapsulation, interested partners can refer to the encapsulation that can be defined as hiding the internal data representation and implementation details of an object. Information Hiding can be enforced through encapsulation.
In JavaScript, keywords for declaring Private Members are not displayed. Therefore, to implement encapsulation/information hiding, we need to start with another idea. We can use the closure concept to create methods and attributes that can only be accessed from the object to meet the encapsulation requirements.
Basic Method
In general, we have learned three methods to achieve the purpose of encapsulation.
Use this. XXX to declare a variable, and then declare methods for value and value assignment, such as getXXX and setXXX.
Use this. _ XXX to declare a variable, and then declare methods for value and value assignment, such as getXXX and setXXX.
Use the "function scope" concept.
1. Wide-open portal
var Book = function(isbn,title,author){ this.setIsbn(isbn); this.setTitle(title); this.setAuthor(author);};Book.prototype = { setIsbn: function(isbn){ this.isbn = isbn; }, getIsbn: function(){ return this.isbn; }, setTitle: function(title){ this.title = title; }, getTitle: function(){ return this.title; }, setAuthor: function(author){ this.author = author; }, getAuthor: function(){ return this.author; }};
The encapsulation implemented using this method, although it implements the valuers and the value assignment tool to protect private attributes. However, in actual use, private attributes can still be accessed from outside, so basically, encapsulation is not implemented.
2. Use naming rules to differentiate
var Book = function(isbn,title,author){ this.setIsbn(isbn); this.setTitle(title); this.setAuthor(author);};Book.prototype = { setIsbn: function(isbn){ this._isbn = isbn; }, getIsbn: function(){ return this._isbn; }, setTitle: function(title){ this._title = title; }, getTitle: function(){ return this._title; }, setAuthor: function(author){ this._author = author; }, getAuthor: function(){ return this._author; }};
This method is similar to the first one. The difference is that different names are used to protect private attributes. However, in actual applications, it still does not implement encapsulation.
3. Use Function Scope
var Book = function(newIsbn,newTitle,newAuthor){ var isbn,title,author; this.setIsbn=function(newIsbn){ isbn = newIsbn; }; this.getIsbn=function(){ return isbn; }; this.setTitle=function(newTitle){ title = newTitle; }; this.getTitle=function(){ return title; }; this.setIsbn=function(newAuthor){ author = newAuthor; }; this.getIsbn=function(){ return author; };}
Because the variables declared in JavaScript Functions have a scope, using this method can avoid direct access to private attributes outside. Basically meet the packaging requirements.
Note that we can use this. XXX and var to declare variables within the function. The difference is that variables declared using this. XXX can be accessed externally. Variables declared using var are protected by function scopes and cannot be directly accessed outside the function.
4. Use Function scope Deformation
Var Book = (function (){//... other static methods return function (newIsbn, newTitle, newAuthor) {var isbn, title, author; this. setIsbn = function (newIsbn) {isbn = newIsbn;}; this. getIsbn = function () {return isbn;}; this. setTitle = function (newTitle) {title = newTitle;}; this. getTitle = function () {return title;}; this. setIsbn = function (newAuthor) {author = newAuthor ;}; this. getIsbn = function () {return author ;};};})();
This method directly returns the execution of a constructor. The constructor here is an embedded function.
The advantage of this method is that "only one copy exists in the memory. Because other static methods are declared outside the constructor, they are not privileged methods ."
The principle for determining whether a method should be designed as a static method is "whether this method will access private attributes ". If it is not needed, designing it as a static method will be more efficient because it will only be created.
Constant
We can use the "only accessors, no value assignment" method to implement constants.
// 1.var Book = function(){ var constants = ["key1": "1","key2": "2","key3": "3"]; this.getConstant = function(key){ return constants[key]; };};Book.getConstant("key1");// 2.var Book = (function(){ var constants = ["key1": "1","key2": "2","key3": "3"]; var con = function(){}; con.getConstant = function(name){ return constants[name]; }; return con;})();Book.getConstant("key1");
Advantages and disadvantages
1. Profit Center
Encapsulation protects the integrity of internal data;
Encapsulation makes object reconstruction easier;
Weaken the coupling between modules to improve the reusability of objects;
Helps avoid namespace conflicts;
......
2. Disadvantages
Private methods are difficult to test;
It must deal with complicated scope chains to make error scheduling more difficult;
It is easy to form over-encapsulation;
JavaScript does not support encapsulation in the native, so there is a problem of complexity in implementing encapsulation in JavaScript;
The above is all the content of this article, hoping to help you learn.