A good article on the concept of js-closures

Source: Internet
Author: User
Tags closure vars

Reading notes:

1. The closure uses the motive:

1): The function of the internal variables exposed;

2): The function internal variable resides in memory.


2, closure of the use of the way:

1 Use global variables and anonymous functions within the function body

    function Obj () {
        var vars = [];
        Addvar is a global variable in the body of the function, and the anonymous method is the closure (which references the temporary variable VARs in the obj () method)
        Addvar = function (Newvar) {
            vars.push (Newvar);
        }
        Readvars is a global variable in the body of the function, and the anonymous method is the closure (which references the temporary variable VARs in the obj () method)
        Readvars = function () {
            alert (VARs);
        }
    After the OBJ () call, the global variables Addvar and Readvars Obj () are initialized in the body of the function
    .
    Addvar ("1");
    Addvar ("ESDSDG");
    Readvars ()//Show 1,ESDSDG
    addvar ("FDSAFADSF");
    Readvars ();//Display 1,ESDSDG,FDSAFADSF/
    *
     Summary:
     after global variables Addvar and readvars initialization, because Addvar and Readvars refer to closures (anonymous methods), The
     anonymous method also references a temporary variable in the body of the obj () method. Therefore, OBJ () will not be released and the
     temporary variable VARs will reside in memory until the global variable Addvar and Readvars are not released.
     */

2 The internal function as the return value, the internal function (closure) exposed


    function newObj () {
        var vars = [];
        Closure Addvar, referencing the VARs
        function Addvar (newvar) {
            vars.push (newvar);
            alert (VARs);
        }
        Exposes the closure function to return
        Addvar
    by returning a value ObjRef refers to the closure function Addvar ()
    var objRef = NEWOBJ ();
    ObjRef ("T1");//t1
    objRef ("T2");//t1,t2/
    *
     Summary:
     1, function exposes closure function by return value
     2, Bjref references the closure function Addvar (), the closure function uses the temporary variable in the NEWOBJ () function VARs
     3, Bjref () is not released when NEWOBJ is not released
     * *


3, the reference relationship is 3 +, the object will not be released, or will be recycled recycling mechanism.



Good Article recommended:

Http://www.jb51.net/article/24101.htm

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.