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...
当OuterFunction被调用时,它创建了一个新的对象实例,并且this在innerFunction和arrowFunction中都指向这个新创建的对象。 当我们直接调用obj.innerFunction()时,this指向了全局对象(在浏览器中是window),因此this.value是undefined,导致抛出一个错误。 然而,当我们调用obj.arrowFunction()时,即使我们是在外部调用的,arrow...
functionmultiply(a,b){'use strict';// enable the strict modeconsole.log(this===undefined);// => truereturna*b;}// multiply() function invocation with strict mode enabled// this in multiply() is undefinedmultiply(2,5);// => 10 当使用严格模式调用multiply(2, 5)的时候,this就是undefined。
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. //严格模式下,函数中的this是undefined "use strict" function f(){ console.log(this); //undefined...
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")。
理解es6 中 arrow function的this 箭头函数相当于定义时候,普通函数.bind(this) 箭头函数根本没有自己的this,导致内部的this就是定义时候的外层代码块中的this。 外层代码块中的this,则取决于执行时候环境上下文context中的this 并不是所有的{}都可以代表是上下文环境或者代码块,例如 {x:1,y:2} ,就是简简单单...
log(this === undefined); // => true return a * b; } // multiply() function invocation with strict mode enabled // this in multiply() is undefined multiply(2, 5); // => 10 当使用严格模式调用 multiply(2, 5) 的时候,this 就是undefined。 严格模式不仅对当前作用域有效,它内部的作用...
代码语言:javascript 代码运行次数:0 复制 functionOrder(main,side,dessert){this.main=main;this.side=side;this.dessert=dessert;this.order=function(){return`I will have${this.main}with${this.side}and finish off with a${this.dessert}`;}}constnewOrder=newOrder("sushi","soup","yogurt");console...
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”)。
In JavaScript, the convention (and this is only a convention) is that any function that begins with a capital letter is to be used as a constructor. Then, one would call var foo = new Foo() and this would refer to the newly created object that is about to be referenced by foo. Of...