javascript高级课程-3

 函数表达式

arguments

this

构造方法

函数表达式

(function(window,u){//传递undefined
        alert("as");
    })(window);//调用一个函数表达式 匿名函数立即执行 传递全局变量的原因 在没有传递全局变量 在执行过程需要跳到外边寻找耗费时间

函数运行其内、关键的三个对象
AO 形成作用域链
argument 不形成链
this 形成原型链

arguments

    (function(window,u){
        var sum=0;
        for(var i=2;i<arguments.length;i++){
            sum+=arguments[i];//不定参数
        }
        console.log(sum);
    })(window,undefined,1,2,1,1,2,12,1,1,1,1,1);

arguments.callee为函数自身

console.log((function(window,u){
        if(u>1){
            return u+(arguments.callee)(window,u-1);
        }else{
            return 1;
        }
        
    })(window,100));

注意区分一下两者区别

//"use strict"//使用ES6语法要求
    var age=11;
    function  test(){
        this.age=12;
    }
    var s=new test();//此时this为null 当为null时则为window
    //函数内 带有this操作 应该 new 否则会污染全局变量
    console.log(age);

结果

//"use strict"//使用ES6语法要求
    var age=11;
    function  test(){
        this.age=12;
    }
    test();//此时this为null 当为null时则为window
    //函数内 带有this操作 应该 new 否则会污染全局变量
    console.log(age);

结果

从上面可以看到在不适用ES6语法要求时 直接调用默认为null 此时this为window 污染全局变量 如果用new 则this为创建的对象不会污染全局变量

var name="webcyh";
    function test(){
        console.log(this.name+"hehe");
    }
    var dog={name:"dog"};
    var cat={name:"cat"};
    dog.intro=test;
    dog.intro();
    cat.intro=dog.intro;
    cat.intro();
    (dog.intro=test)();

构造方法

function Cat(name){
        this.name=name;
    }
    var c =new Cat();
    /*
    new 时产生空对象{}
    方法的this指向{}
    {}.name=name;
    

    */
原文地址:https://www.cnblogs.com/webcyh/p/11391512.html