const obj = { name: 'Alice', regularFunction: function() { console.log(this.name); }, arrowFunction: () => { console.log(this.name); }};obj.regularFunction(); // 'Alice',this指向obj对象obj.arrowFunction(); // undefined,箭头函数的this指向在定义时确定,这里指向全局对...
AI代码解释 constobj={name:'Alice',sayHi:()=>console.log(`Hello,${this.name}`)// this指向全局对象或undefined(严格模式)};// 应该使用普通函数或显式绑定thissayHi:function(){console.log(`Hello,${this.name}`);} 没有自己的arguments:箭头函数没有自己的arguments对象,使用剩余参数(...args)替代。
因此匿名函数表达式的规则也适用于ArrowFunction,不过两者还是有区别的,ArrowFunction中没有规定不能直接出现super,也就是说在ArrowFunction中可以用super方法,其次ArrowFunction内部没有[[Construct]]方法,因此不能作为构造器调用,所以在创建Function对象时不执行makeConstructor方法。最重要一点就是ArrowFunction没有本地的thi...
箭头函数的this取值,规则非常简单,因为this在箭头函数中,可以看做一个普通变量。 An arrow function does not have its own this. The this value of the enclosing lexical scope is used; arrow functions follow the normal variable lookup rules. 箭头函数没有自己的this值,箭头函数中所使用的this都是来自函数...
箭头函数arrowfunction ◼ 箭头函数是ES6之后增加的一种编写函数的方法,并且它比函数表达式要更加简洁: 箭头函数不会绑定this、arguments属性; 箭头函数不能作为构造函数来使用(不能和new一起来使用,会抛出错误); ◼ 箭头函数如何编写呢? (): 函数的参数 ...
3.2 call()、apply() 调用无法改变this 就像上面讲的arrow function没有自身的this,当用call()或apply() 调用箭头函数时无法改变函数主体内的this。 示例: 代码语言:javascript 代码运行次数:0 3.3 没有arguments 使用arrow function创建的函数,自身是没有arguments成员。
arrowFunction(); } var obj = { outerFunction: outerFunction }; obj.outerFunction(); // 输出:obj对象 在这个例子中,尽管箭头函数在outerFunction内部定义,但它的this值继承自包围它的函数,即outerFunction。因此,箭头函数中的this指向了obj对象。这表明箭头函数不会创建自己的this上下文。此外,值得注意的是,...
function func() { console.log(this) } func.bind(1).bind(2)() // 1 1. 2. 3. 4. 5. 箭头函数中 this 不会被修改 func = () => { // 这里 this 指向取决于外层 this,参考口诀 7 「不在函数里」 console.log(this) } func.bind(1)() // Window,口诀 1 优先 ...
this 在Java 等面向对象的语言中,this 关键字的含义是明确且具体的,即指代当前对象。一般在编译期确定下来,或称为编译期绑定。而在 JavaScript 中,this 是动态绑定,或称为运行期绑定的,这就导致 JavaScript 中的 this 关键字有能力具备多重含义,变得有点随意。而在ES6中又引入了Arrow Function以及Class,它们对于...
箭头函数继承自最近的外围非箭头函数作用域的 this 值。 function outerFunction() { return () => { console.log(this === window); // 输出 true }; } var arrowFunction = outerFunction(); arrowFunction(); 6. 注意事项 丢失的 this:在一些情况下,this 的值可能会丢失或产生意外的结果。 var obj...