首页 > 文章列表 > js中this的使用场景

js中this的使用场景

js this
180 2022-08-06

使用场景

1、在构造函数中使用(构造函数本身)

2、作为对象属性时使用(调用属性的对象)

3、作为普通函数时使用(window)

4、call、apply、bind(执行的第一个参数)

实例

var a = {
  name: 'A',
  fun: function() {
    console.log(this.name);
  }
};
 
a.fun();  //this === a
a.fun.call({ name: 'B' });  //this === { name: 'B' }
var fun1 = a.fun;
fun1(); //this === window

推荐操作环境:windows7系统、jquery3.2.1版本,DELL G3电脑。