constobj={name:'Alice',sayHi:()=>console.log(`Hello,${this.name}`)// this指向全局对象或undefined(严格模式)};// 应该使用普通函数或显式绑定thissayHi:function(){console.log(`Hello,${this.name}`);} 没有自己的arguments:箭头函数没有自己的arguments对象,使用剩余参数(...args)替代。 代码语言:...
如果大家有需要,欢迎访问前辈的博客https://www.liaoxuefeng.com/学习。 ES6标准新增了一种新的函数:Arrow Function(箭头函数)。 更简洁的语法 我们先来按常规语法定义函数: function(x) {returnx *x; } 该函数使用箭头函数可以使用仅仅一行代码搞定! x => x * x 箭头函数相当于匿名函数,并且简化了函数定义。
2. Contruction functiona(){}a()// run the function, instruction sequencenewa()// construction In javascript, when you see a function, you cannot make sure how to call that function Number()newNumber()Date()newDate() After ES6, introducearrow functionandclass class: you can only use it ...
因此匿名函数表达式的规则也适用于ArrowFunction,不过两者还是有区别的,ArrowFunction中没有规定不能直接出现super,也就是说在ArrowFunction中可以用super方法,其次ArrowFunction内部没有[[Construct]]方法,因此不能作为构造器调用,所以在创建Function对象时不执行makeConstructor方法。最重要一点就是ArrowFunction没有本地的this...
// Arrow Function: hello = () => { document.getElementById("demo").innerHTML+=this; } // The window object calls the function: window.addEventListener("load", hello); // A button object calls the function: document.getElementById("btn").addEventListener("click", hello); ...
就像上面讲的arrow function没有自身的this,当用call()或apply() 调用箭头函数时无法改变函数主体内的this。 示例: 代码语言:javascript 复制 // 普通函数varsayHello=function(userName){console.log('hi '+userName);console.log(this);};sayHello.call({x:1},'polk');// => this == { x: 1 }// 箭...
ES6标准新增了一种新的函数:Arrow Function(箭头函数)。 为什么叫Arrow Function?因为它的定义用的就是一个箭头: 代码语言:javascript 复制 x=>x*x 上面的箭头函数相当于: 代码语言:javascript 复制 function(x){returnx*x;} 箭头函数相当于匿名函数,并且简化了函数定义。箭头函数有两种格式,一种像上面的,只包含...
function (a, b){ return a + b + 100; } // Arrow Function (a, b) => a + b + 100; // Traditional Function (no arguments) let a = 4; let b = 2; function (){ return a + b + 100; } // Arrow Function (no arguments) ...
在JavaScript中,箭头函数(Arrow Function)是ES6引入的新特性,它提供了一种简洁的语法来定义函数。 箭头函数的基本结构如下: 例如: 箭头函数还有几个...
functionfoo(n){consttheArguments = [n];varf= () => theArguments[0] + n And since the argument passed tofoois 5, that plusn(the same argument) is 10. In the third function, the same logic applies. Inside an arrow function,argumentsrefers to the closest identifier namedarguments, or th...