Encapsulation is the basis of object-oriented, and the anonymous functions and closures to be learned today are designed to implement JavaScript object-oriented encapsulation. Encapsulation implementation and encapsulation of variables improve data and system security. encapsulation is the basis of object-oriented design.
1. Anonymous Functions
That is, a function without a name is created as follows:
Function (){...}
An anonymous function exists independently and cannot be run. You can call a variable by assigning a value or execute it by using an expression.
1. assign a value to the variable as a general function usage
Var run = function (){
Return 'method running ';
};
Alert (run ());
2. self-execution through Expressions
(Function (optional)
{
Alert ('method running ');
}) (Arguments );
Ii. Closure
Create an anonymous function within a common function. The anonymous function can access the variables in the common function to implement features similar to those encapsulated in the object-oriented model, this anonymous function is called a closure corresponding to a common function. A closure is an interface created externally to access internal variables of a function.
Functionrun (){
Varusername = 'forrest ';
Return function () {// return the box () local variable through the anonymous function
Returnusername;
};
}
// Alert (username); // error username is not defined
Alert (run ()());
Features: The local variables used in the closure will reside in the memory, avoiding naming conflicts caused by the use of global variables. It is worth noting that the this keyword is used in the closure to point to the window object.
3. Encapsulation
For better security and data protection, data needs to be private and encapsulated. JS does not have the block-level scope concept (that is, out of the scope, can still access declared variables), but with the Foundation above, we can achieve encapsulation effects like other formal object-oriented languages.
Function user () {(function () {for (I = 0; I <5; I ++) {alert (I );}})(); alert (I); // The variable I} cannot be accessed here // Private scope replaces the global variable (function () {var username = 'forrest'; alert (username );}) (); // After the anonymous function is executed, the variable is destroyed immediately. // access the private variable function User () {var username = Forrest; // Private Variable function learn () {// Private function return 'learn';}; // provides external interfaces to indirectly access internal members this. userlearn = function () {return username + learn () ;}}var user = new User () alert (user. userlearn (); // implement get (), set () function User (value) {var user = value; this. getUser = function () {return user;} this. setUser = function (value) {user = value;} var user = new User ('forrest'); alert (user. getUser (); user. setUser ('lil'); alert (user. getUser (); // static private variable (function () {var user = ''; User = function (value) {user = value ;}; Box. prototype. getUser = function () {return user ;}; Box. prototype. setUser = function (value) {user = valu ;};}) (); // literally privatized function NormalUser () {}var user = function () {var user = 'forrest'; function learn () {return user + 'learning... ';}; var nu = new NormalUser (); nu. userlearn = function () {return learn () ;}; return nu ;}(); alert (user. userlearn ());
Conclusion:
Although Javascript is not an orthodox object-oriented language, it can also achieve the effects of object-oriented encapsulation, inheritance, polymorphism, etc. It still needs to be well understood in learning, especially for not many people who use JavaScript, they can only wait for the next step to solve this series of problems and further understand its application in Object-Oriented aspects.