console.log(a)
var a=12;
funcyion fn(){
    console.log(a)
    var a=13;
}
fn()
console.log(a)

//undefined undefined 12
Copy the code
console.log(a)
var a=12;
function fn(){
    console.log(a)
    a=13;
}
fn()
console.log(a)
//undefined 12 13
Copy the code
console.log(a)      // I pay therefore I am not
a=12;
function fn(){
    console.log(a)
    a=13;
}
fn()
console.log(a)
Copy the code
// Global scope variable promotes var foo; var bar=AAAFFF111
//foo=1
// The bar() parameter is assigned to none
Var foo is private
// if(! Foo) foo is private private foo=undefined! Foo =true condition is true
//foo=10

//=>foo:10

var foo=1;
function bar(){
    if(! foo){var foo=10;
    }
    console.log(foo)  / / 10
}
bar()
Copy the code
// Global scope variable promotes var foo;
var  foo='hello';       //foo='hello'
(function(foo){
    //=> Private scope
    // The parameter foo = 'hello' foo is private
    console.log(foo);   //'hello'
    var foo=foo || 'world';
        Var is no longer promoted because there is already a private variable foo
        //'hello' || 'world' => 'hello'
    console.log(foo)            // 'hello'
})(foo)  //=> Pass the global value of foo as an argument to the parameter in the private scope
console.log(foo)        //'hello'
Copy the code
// The global scope variable is promoted var a; var fn=AAAFFF111;
//a=9
//fn() is formed without destroying the scope A parameter assignment variable without promoting d
    / / a = 0 global a = 0
    / / return BBBFFF111 function
//f=fn() = BBBFFF111 ;
// set F(5) to b=5; Variable promotion None
    //5+a++ a is global = "log: 5 global A =1 destroy

//fn()(5)
    //fn() forms the do not destroy scope B(temporarily do not destroy when BBBFFF222(5) is done) parameter assignment variable promotion d does not exist
        / / a = 0 global a = 0
        / / return BBBFFF222 function
    //BBBFFF222(5) set b=5; Variable promotion None
       //5+a++ = "log: 5 global a=1 destruction


//f(5) forms a private scope
    // set b=5; Variable promotion None
    //5+a++ //
    //=> log:6 ; Global A =2 destruction

/ / global a = 2


var a=9;
function fn(){
    a=0;
    return function(b){
        returnb+a++; }}var f=fn();
console.log(f(5))           / / 5
console.log(fn()(5))        / / 5
console.log(f(5))           / / 6
console.log(a)              / / 2
Copy the code

The reference data type is passed into the function as an argument

What is passed in is the heap memory space address, and if the parameter does not redirect to the new heap memory space address, the original data is manipulated

Private variables are not directly related to global variables, but they may be indirect

Both private and global variables refer to values of data types and point to the same heap memory space

// Global scope
Var ary; var res; Var fn=AAAFFF111(); 3, assign value)
//ary(an array is an object)= AAAFFF222(1, open space; 3, assign value)

//fn(ary) => pass the global ary as an argument to fn's parameter: fn(AAAFFF222)

    // Assign parameter ary=AAAFFF222 The private variable ary and the global variable ary both refer to the same memory space AAAFFF222.
    // Variable promotion none
    //ary[0]=0 AAAFFF222
    //ary=[0] BBBFFF111 // create a new memory space to store things and assign values
        // The private variable ary (BBBFFF111) is no longer related to the global ary (AAAFFF222)
    //ary[0]=100; BBBFFF111 space index 1 becomes 0 [100]
    //return ary => return BBBFFF111
//res=fn(AAAFFF222) => res:BBBFFF111
var ary=[1.2.3.4];
function fn(ary){
    ary[0] =0;
    ary=[0];
    ary[0] =100;
    return ary;
}
var res=fn(ary)
console.log(ary)  [0.2.3.4]
console.log(res)    [100]

Copy the code
// Global scope
Var f; Var fn=AAAFFF111 (); 3, assign value)
// fn(2) opens private scope A, A is not destroyed
    // The parameter is assigned to I =2 A private // the variable is not promoted
    //return opens up new memory space BBBFFF111
//f=BBBFFF111


//f(3) BBBFFF111(3) open private scope B
    // assign B private n=3; Variable promotion None
    //n + (--i) 3+(--A.i) =>3+1 =4


//fn(4) open private scope C temporarily do not destroy BBBFFF222(5) destroy after execution
    // Assign the parameter I =4 C private
    // the return function opens up new memory space BBBFFF222
    //BBBFFF222(5) open private scope D
    // assign B private n=5; Variable promotion None
    //n + (--i) 5+(--A.i) =>5+3 =8

//f(8) =>BBBFFF111(8) open private scope E
        // Assign E private n=8; Variable promotion None
        //n + (--i) 3+(--A.i) =>8+0 =8
function fn(i){
    return function(n){
        console.log(n + (--i))
    }
}

var f=fn(2);
f(3);               / / 4
fn(4) (5);           / / 8
fn(6) (7);           / / 12
f(8);               / / 8
Copy the code
// Global scope
Var num; var obj; var fn;
//num=10 obj=AAAFFF111 3, assign value)
//obj. Fn = XXX obj = AAAFFF111
    // The self-executing function executes
    // open private scope A is not destroyed
    // The parameter is assigned a.num = 20
    // Code execution
    //this.num=num*3;自执行函数执行 this为window num为A.num   全局num=20*3=60
    //num++ A.num=21
    //return opens up new memory space BBBFFF111
//fn=obj.fn; fn=BBBFFF111;
//fn(5)
    // open private scope B destruction
    // Assign n=5; No variable promotion
    // this.num+=n; This = window global num=65
    //num++ num is not private
    / / output 22

//obj.fn(10);
    // open private scope C destruction
    // Assign n=10; No variable promotion
    // this.num+=n; This is obj obj. Num = 30
    //num++ num is not private
    / / output 23
//console.log(num,obj.num) 65 30


var num =10;
var obj={
    num:20
}
obj.fn=(function(num){
    this.num=num*3;  //this.num find this's num*3
    num++;
    return function(n){
        this.num+=n;
        num++;
        console.log(num)
    }
})(obj.num)
var fn=obj.fn;
fn(5);                      / / 22
obj.fn(10);                 / / 23
console.log(num,obj.num)    / / 30 65
Copy the code
//Fn AAAFFF111 has prototype by default;
    Prototype AAAFFF222 Prototype default with constructor:Fn
    //prototype is an Object datatype with \__proto__ pointing to object. prototype
// prototype= AAAFFF333 (1, 1; 2, store things 3, assign) store public attributes
     // This is also the default Object with \__proto__ pointing to object.prototype
     // fn. prototype no longer points to AAAFFF222 but to AAAFFF333
//var f1=new Fn;
    // open a private scope
    //f1.x=100; f1.y=200; F1.getx =AAAFFF444 are private attributes
    //f1 is an instance of Fn which is an object with \__proto__ pointing to Fn. Prototype

//var f2=new Fn;
    // open a private scope
    //f2.x=100; f2.y=200; F2.getx =AAAFFF444 are private attributes
    //f2 is an instance of Fn that is an object with \__proto__ pointing to Fn. Prototype

function Fn(){
    this.x=100;
    this.y=200;
    this.getX=function(){
        console.log(this.x)
    }
}
Fn.prototype={
    y:400.getX : function(){
        console.log(this.x);
    },
    getY : function(){
        console.log(this.y);
    },
    sum:function(){
        console.log(this.x + this.y); }}var f1=new Fn;
var f2=new Fn;
console.log(f1.getX === f2.getX)                        //false
console.log(f1.getY === f2.getY)                        //true
console.log(f1.__proto__.getY === Fn.prototype.getY)    //true
console.log(f1.__proto__.getX === f2.getX)              //false
console.log(f1.getX == Fn.prototype.getX)               //false
console.log(f1.constructor)                             //Object
console.log(Fn.prototype.__proto__.constructor)         //Object (Fn. Prototype ={} is to change the public method pointing to, 'if you don't use constructor=Fn instance. Constructor = Object ')
f1.getX()            //this:f1 100
f1.__proto__.getX() //this:f1.__proto__(Fn.prototype) undefined
f2.getY() //this:f2 private not found public getY but this is F2 200
Fn.prototype.getY()    //this:(Fn.prototype 400
f1.sum(); //this:f1 f1.X+f1.Y=300
Fn.prototype.sum();  //this:Fn.prototype Fn.prototype.X+Fn.prototype.y=undefined+400=NaN
Copy the code