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...
「The this keyword evaluates to the value of the ThisBinding of the current execution context.」「this 这个关键字代表的值为当前执行上下文的ThisBinding。」然后再来看看MDN 对this 的定义:「In most cases, the value of this is determined by how a function is called.」「在大多数的情况下,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")。
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 ...
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?
ES6 新增了一种新的函数: 箭头函数 Arrow Function 箭头函数相当于匿名函数,简化了函数定义,将原函数的 function 关键字和函数名都删掉,并使用 => 连接参数和函数体 1. ES6 前定义函数 AI检测代码解析 1. // function 关键字 ...
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 ...
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...