Javascript closure details

Source: Internet
Author: User

I have previously written an article about javascript closure, javascript closure, but it is not detailed enough to reflect the strength of the closure. This article. As we all know, javascript has no block-level scope and only function scope. It means that the parameters and variables defined in the function are invisible outside the function, while the variables defined at any position inside the function are visible anywhere inside the function. The advantage of this is that internal functions can access parameters and variables that define their external functions. First, we construct a simple object copy code 1 var testObj = {2 value: 10, 3 add: function (inc) {4 this. value + = (typeof inc = "number ")? Inc: 1; 5} 6}; 7 8 testObj. add (); 9 testObj. value; // 1110 11 testObj. add (2); 12 testObj. value; // 13 copy the code to write it in this way. The value cannot be modified illegally. You can modify it as follows. Copy code 1 var testObj = (function () {2 var value = 10; 3 return {4 add: function (inc) {5 value + = (typeof inc = "number ")? Inc: 1; 6}, 7 getValue: function () {8 return value; 9} 10}; 11}) (); 12 13 testObj. add (); 14 testObj. getValue (); // 1115 16 testObj. add (2); 17 testObj. getValue (); // 13 copy the code. We can call a function to initialize testObj. This function returns an object literal and defines a value variable in the function, this variable is always available for the add and getValue methods, but the function scope makes it invisible to other programs. At the same time, we can draw a conclusion that internal functions have a longer life cycle than their external functions. Let's continue to look at a constructor call example. Copy the code var MyObj = function (str) {this. status = str ;}; MyObj. prototype. getStatus = function () {return this. status ;}; var obj = new MyObj ("javascript"); obj. getStatus (); // there is nothing wrong with copying the code in javascript, but there will be a "one more click". Why use a getStatus method to access a property that can be directly accessed? It makes sense if status is a private property. Copy the code var obj = function (status) {return {getStatus: function () {return status ;};}; var myObj = obj ("javascript"); myObj. getStatus (); // "javascript" Copy code. When obj is called, it returns a new object containing the getStatus method. A reference of this object is saved in myObj, even if obj has been returned, the getStatus method still has the permission to access the status attribute of the obj object. The getStatus method does not access a copy of this parameter. It accesses the parameter itself. This is possible because the function can access the context where it was created, which is called a closure.

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.