当前位置: 移动技术网 > IT编程>网页制作>CSS > JavaScript之函数中的this和严格模式

JavaScript之函数中的this和严格模式

2018年12月07日  | 移动技术网IT编程  | 我要评论

摩洛客中文论坛,叶象花,青春期2演员表

函数中的this的指向
普通函数中的this是谁-----window

//普通函数
    //    function f1() {
    //      console.log(this);
    //    }
    //   f1();

定时器方法中的this是谁----window

//定时器中的this
    //    setinterval(function () {
    //      console.log(this);
    //    },1000);

对象.方法中的this是谁----当前的实例对象

原型对象方法中的this是谁---实例对象

构造函数中的this是谁-----实例对象

    //构造函数
    //    function person() {
    //      console.log(this);
    //对象的方法
    //      this.sayhi=function () {
    //        console.log(this);
    //      };
    //    }
    //原型中的方法
    //    person.prototype.eat=function () {
    //      console.log(this);
    //    };
    //    var per=new person();
    //    console.log(per);
    //    per.sayhi();
    //    per.eat();

严格模式:

//严格模式:
    "use strict";//严格模式
    function f1() {
      console.log(this);//window
    }
    f1();

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网