美文网首页
函数中的this

函数中的this

作者: Anglie | 来源:发表于2019-01-25 21:16 被阅读0次

    一,普通函数中的this指向,
    二,定时器方法中的this指向,
    三,构造函数中的this指向,
    四,对象方法中的this指向,
    五,原型中的this指向

    function obj(){
    console.log(this);
    }
    obj();
    window

    <!定时器中的this指向>
    setInterval(function (){
    console.log(this);
    },300)
    window

    function Person(){
    console.log(this);
    }
    var person1=new Person();
    Person

    // 对象方法中的this指向实例对象
    function Person(){
    console.log(this);
    this.say=function(){
    console.log(this);
    }
    }
    var person1=new Person();
    person1.say();
    Person {}
    Person {say: ƒ}

    <!--原型方法中的this指向实例对象-->
    function Person() {
        Person.prototype.sayHi=function(){
            console.log(this);
        }
    }
    var person1=new Person();
    person1.sayHi();
    Person

    相关文章

      网友评论

          本文标题:函数中的this

          本文链接:https://www.haomeiwen.com/subject/tybvjqtx.html