Closed scope of the problem package

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<script type="text/javascript">
    //测试1
    var obj = {name:'haha',data:{value:1}};
    function outer(){
        var objback = obj.data;
        function inner(){
            objback = {}
        }
        return inner;
        
    }
    where f1 = outs ();
    f1 ();
    the console.log ( " Test. 1 " , obj) // {name: 'haha', Data: {value:}}. 1; not changed

    // Test 2

    var obj2 = {name:'haha',data:{value:1}};
    function outer2(){
        var objback = obj2;
        function inner(){
            objback.data = null
            console.log("测试21",JSON.stringify(obj2));   //{name:'haha',data:null}
            objback.name = "asdf";
            console.log("测试22",JSON.stringify(obj2));   //{name:'asdf',data:null}
            objback.data = {};
            
            console.log("测试23",JSON.stringify(obj2)); //{name:'asdf',data:{}}
        }
        return inner;
        
    }
    where f 2 = outer2 ();
    f2();
    console.log("测试2",JSON.stringify(obj2)) //{"name":"asdf","data":{}}
    
    // 1,2 closure described an object attribute of the object referenced in the closed package can not be modified after the reaction to an object, the object reference in their entirety into the subject reaction can be

    //测试3
    var obj3 = {name:'haha',data:{value:1}};
    function classA(){
        this.obj = {name:'haha',data:{value:1}};
        this.outer = function(){
            
            var data  =  this.obj.data;
            var data3 = obj3.data;
            function inner(){
                data.value = 2;
                Data3 = {};
            }
            return inner;
        }
        this.test=function(){
            
            console.log("测试3",this.obj.data.value)
        }
    }

    var a =  new ClassA ();
    a.outer()();
    a.test();
    the console.log ( " test. 3 " , the this .obj3.data)
     // 2,3 closure properties of an object described by reference to the target object does not reflect the modified closure, but the object is to modify the object a value may be subject to the reaction, the reaction may be an object reference in its entirety into the subject 
    // test. 4 
     function classB () {
         the this .OBJ = {name: ' haha ' , Data: {value: . 1 }};
         the this .outer =  function () {
            
            var data  = this.obj;
            function inner(){
        
                data.data.value = 2;
                console.log("测试4",data.data.value)
                
            }
            return inner;
        }
        this.test=function(){
    
            console.log("测试4",this.obj.data.value)
        }
    }
    //
    var b = new classB();
    b.outer()();
    b.test();

    var data = {value:10,data:{value:1}};
    console.log(JSON.stringify(data));
    data.data = {};
    console.log(data)
    // overall description of the object to change the variable to point to the object properties, change the point of this property will not react to the object itself, but because of a number of variables only. I was stupid 
</ Script > 
</ body > 
</ HTML >

 

Guess you like

Origin www.cnblogs.com/sz-toosimple/p/11769227.html