JS中的this JS中this的指向一般可以直接归成一条规律 ====》 函数中的this function f1(){ return this; } f1() === window; // true 网上分析较多,因而将其汇总:函数中的this 一般都指向调用这个函数的对象 直接写在 ...
普通函数:this指向分为 种情况, . obj.getName 指向obj .getName 非严格模式下,指向window,严格模式下为undefined . var a new A a 指向A本身 .getName .apply obj 指向obj 箭头函数:箭头函数本身是没有this和arguments的,在箭头函数中引用this实际上是调用的是定义时的上一层作用域的this。这里强调的是 ...
2019-03-25 17:52 0 1851 推荐指数:
JS中的this JS中this的指向一般可以直接归成一条规律 ====》 函数中的this function f1(){ return this; } f1() === window; // true 网上分析较多,因而将其汇总:函数中的this 一般都指向调用这个函数的对象 直接写在 ...
最近做的项目中遇到了在箭头函数里使用this时报错的问题,把箭头函数的写法改成function()后,this的指向才达到预期。关于这个问题值得研究一下。 在箭头函数出现之前的ES5时代,this指向它的调用者。是哪个对象调用了这个属性或方法,this就指向这个对象。这有 ...
Vue: 不要在选项 property 或回调上使用箭头函数,比如 created: () => console.log(this.a) 或 vm.$watch('a', newValue => this.myMethod())。因为箭头函数并没有 this,this 会作为变量一直 ...
箭头函数和普通函数的区别如下。 普通函数:根据调用我的人(谁调用我,我的this就指向谁) 箭头函数:根据所在的环境(我再哪个环境中,this就指向谁) 一针见血式总结: 普通函数中的this: 1. this总是代表它的直接调用者(js的this是执行上下文), 例如 obj.func ...
一.箭头函数 二.箭头函数的this指向 三.改变this指向 四.注意事项特别说明 转载: https://blog.csdn.net/DcTbnk/article/details/105419682 ...
1.箭头函数的定时器this指向当前对象 var obj ={ name:"Tina", popup:function(){ setTimeout(()=>{ console.log("打印名字",this.name) },3000) }} obj.popup(); //打印名字 ...
this 在面试中,js指向也常常被问到,在开发过程中也是一个需要注意的问题,严格模式下的this指向undefined,这里就不讨论。 普通函数 记住一句话哪个对象调用函数,该函数的this就指向该对象。总指向它的调用者。 obj.getName() 无疑会打 ...
全局环境下,指向windows console.log(this.document === document); // true 函数上下文调用 function f1(){ return this; } f1() === window; // true 对象中 ...