美文网首页
JS高级-函数中this的指向

JS高级-函数中this的指向

作者: 哎呦呦胖子斌 | 来源:发表于2018-11-07 16:45 被阅读0次

    普通模式下:
    普通函数中的this?
    调用的时候是window.f1(),window可省略 window

    对象.方法中的this?
    function Person(){
    this.sayHi = function(){
    console.log(this);
    }
    }
    var per = new Person();
    per.sayHi();
    实例对象

    定时器中的this?
    调用的时候是window.setInterval(function(){},1000),window可省略 window

    构造函数中的this?
    实例对象

    原型对象方法中的this?
    function Person(){}
    Person.prototype.satHi = function(){
    console.log(this);
    }
    var per = new Person();
    per.sayHi();
    实例对象
    严格模式下”use strict”;
    方法或者函数的调用必须由对象引起,如果没有写对象对其进行调用的话,那么输出的结果就是undefined

    相关文章

      网友评论

          本文标题:JS高级-函数中this的指向

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