In other words, arrow functions treat this like any other lexical variable. 翻译:换句话说,箭头函数对待this就像对待任何其他词法变量一样 If you use a this inside an arrow function, it behaves exactly as any other variable reference, which is that the scope chain is consulted to find a function...
原文:https://dev.to/bhagatparwinder/arrow-function-basics-34cm 简介 箭头函数是在 ES6 引入的,相对于函数表达式来说是一种更简洁的方式。 箭头函数名称的来源是因为使用了=>。 语法 代码语言:javascript 代码运行次数:0 运行 AI代码解释 constfunctionName=(arg1,arg2,...argN)=>{returnvalue;} ...
箭头函数(Arrow Function)是ECMAScript 6中的新特性。 问题 从代码中分析箭头函数的指向问题: function foo() { return ()=>{ console.log(this.a) } } const obj1 = {a:1} const obj2 = {a:2} const bar = foo.call(obj1) bar.call(obj2) 我们将会看到哪一个 obj 的a会被打印出呢? 假如fo...
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")。 this的值是什么?
function Person( name){ this.name = name; this.x=function(){ return this; } } let p = new Person("mcgrady"); console.log(p===p.x()) //true 1. 2. 3. 4. 5. 6. 7. 8. 9. AI检测代码解析 //严格模式下,函数中的this是undefined ...
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 ...
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’s method (a function that belongs to an object), what would it refer to?
也许你在其他面向对象的编程语言曾经看过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 In the example above, when we passobj.greetingas a callback to the functionsayHello(), itsthiskeyword loses itsthisbinding toobjand points to the global object.this.greetis not referring toobj—instead it is referring to the global object. This can be solved by using thebind()me...