查了下此时arrow function中的this是global context,虽然知道这规则就是es6这样规定的,但是好奇心仍然得不到满足,今日偶然在知乎看见一个回答提到了更多信息。 原题是探讨关于this的缺陷的,其中 贺师俊 的回答提到: 1. JavaScript的this在直接调用时会是global,这是不是个错误? 答:是设计错误。所以ES5的strict模式改...
当OuterFunction被调用时,它创建了一个新的对象实例,并且this在innerFunction和arrowFunction中都指向这个新创建的对象。 当我们直接调用obj.innerFunction()时,this指向了全局对象(在浏览器中是window),因此this.value是undefined,导致抛出一个错误。 然而,当我们调用obj.arrowFunction()时,即使我们是在外部调用的,arrow...
Here,innerFunc()is defined using the arrow function. It takesthisfrom its parent scope. Hence,this.agegives25. When the arrow function is used withthis, it refers to the outer scope. 7. this Inside Function with Strict Mode Whenthisis used ina function with strict mode,thisisundefined. Fo...
function handleClick() { console.log(this); // 在事件处理函数中,this 可能不是你期望的对象 } document.addEventListener("click", handleClick); 理解this的不同绑定规则是编写高质量JavaScript代码的重要一步。通过掌握这些规则,可以更好地设计和组织代码,确保this的值符合预期。
arrow function 箭头函数中的this 箭头函数 箭头函数是对正规函数的语法简化,它没有this、arguments等属性,也不能当作构造函数使用,在使用中尤其要注意箭头函数没有自己的this,它的this是绑定的父作用域上下文,箭头函数主要用在匿名函数的地方,写起来更简单,比如...
A function's this keyword behaves a little differently in JavaScript compared to other languages. It also has some differences between strict mode and non-strict mode. JavaScript是一门比较奇特的语言,它的this与其他语言不一样,并且它的取值还取决于代码是否为严格模式("use strict")。
Most of the function calls in your code will have the default binding. The value of this is bound to the global object (the window object when working with JavaScript in the browser). This happens when you call a function with just the function name and it's arguments within brack...
arrow function with an inline onclickThis calls an arrow function with event listenerThis calls an regular function with an inline onclickThis calls an regular function with event listenerbuttonbutton[Log] i am arrow function[object Window] [Log] i am arrow function[object Window] [Log] i am...
functionNinja(){this.whoAmI=function(){returnthis;}.bind(this);} 在问题1中的结果ninja2.whoAmI() === ninja1也就得到了解释。 所以说,书中的话并不是那么的准确:arrow functions don’t get their own implicit this parameter when we call them; instead they remember the value of the this parame...
Until arrow functions, every new function defined its own this value based on how the function was called。This proved to be less than ideal with an object-oriented style of programming. 箭头函数 箭头函数的this取值,规则非常简单,因为this在箭头函数中,可以看做一个普通变量。