当时使用nodejs和jsbin调试均是如此, 查了下此时arrow function中的this是global context,虽然知道这规则就是es6这样规定的,但是好奇心仍然得不到满足,今日偶然在知乎看见一个回答提到了更多信息。 原题是探讨关于this的缺陷的,其中 贺师俊 的回答提到: 1. JavaScript的this在直接调用时会是global,这是不是个错误?
因为书中对于arrow function中this的解释,我也不是很理解。原文是这样的:arrow functions don’t get their own implicit this parameter when we call them; instead they remember the value of the this parameter at the time they were created. 当我们调用arrow functions时,它们没有自己的隐式的this参数;它...
In JavaScript,thiskeyword refers to theobjectwhere it is called. 1. this Inside Global Scope Whenthisis used alone,thisrefers to the global object (windowobject in browsers). For example, leta =this;console.log(a);// Window {}this.name ='Sarah';console.log(window.name);// Sarah Run C...
Arrow functions were created to simplify function scope and make using the ‘this’ keyword much more straightforward. They utilize the => syntax, which looks like an arrow. Even though I don’t think it needs to go on a diet, people call it “the fat arrow”(and Ruby enthusiasts may k...
也许你在其他面向对象的编程语言曾经看过this,也知道它会指向某个构造器(constructor)所建立的对象。但事实上在JavaScript里面,this所代表的不仅仅是那个被建立的对象。先来看看ECMAScript 标准规范对this 的定义:「The this keyword evaluates to the value of the ThisBinding of the current execution context.」...
Before we dive deeper into ES6 arrow functions, it’s important to first have a clear picture of what ‘this’ binds to in ES5 code. If the ‘this’ keyword were inside an object’smethod (a function that belongs to an object), what would it refer to?
JavaScript中的this 在实际应用中,了解this的行为是非常重要的,特别是在编写库或框架时,或者当你需要在回调函数中访问特定的上下文时,通常推荐使用箭头函数或者其他方法来确保this的正确指向。 在ES6中,this的值取决于它是如何被调用的。 this不是一个函数或对象,而是一个特殊的关键字,其值在函数被调用时确定。
在JavaScript中,this是一个关键字,表示当前执行代码的上下文。在前两次回答中,我们提到了this的不同绑定规则,包括默认绑定、隐式绑定、显式绑定、new 绑定以及箭头函数中的this行为。让我们更全面地总结和完善这些内容。 1. 默认绑定 在全局作用域中或函数内部,当没有明确指定this指向时,this默认指向全局对象(在浏览...
JavaScript has always been an essential part of web development, and with the advent of Node.js, it has become even more popular as a server-side technology. One of the most critical and sometimes confusing aspects of JavaScript is the this keyword, w
In constructor functions When you are usingconstructor functions with a new keyword,thisbehaves a bit differently than usual. In short, whatnewoperator does is that: It creates a new blank object. It makesthisto point to this newly created object inside the constructor function ...