es6常用语法:箭头函数

// function foo() {

// console.log('hello');

// }

// foo(); hello

// let foo = () => console.log('hello');

// foo(); hello

// function foo(v){

// return v;

// }

// let foo = v => v;

// let ret = foo(111);

// console.log(ret); 111

// 多个参数必须用括号包住

// let foo = (a,b) => {let c = 1; console.log(a + b + c);}

// foo(1,2); 4

// 匿名

// let arr = [123,456,789];

// arr.forEach(function(element,index){

// console.log(element,index);

// })

// arr.forEach((element,index) => {

// console.log(element,index);

// })

// 箭头函数的注意事项

// 1、箭头函数中this取决于函数的定义, 而不是调用

// function foo(){

// // 使用call调用foo时, 这里的this其实就是call的第一个参数

// // console.log(this);

// setTimeout(()=>{

// console.log(this.num); 1

// },100)

// }

// foo.call({num:1});

// ---------------------------------

// 2、箭头函数不可以new

// let foo = () => {this.num = 123;};

// new foo();报错

// ------------------------------------

// 3、箭头函数不可以使用arguments获取参数列表, 可以使用rest参数代表

// let foo = (a,b) => {

// // console.log(a,b);

// console.log(arguments);这种方式获取不到实参列表

// }

// foo(123,456);

let foo = (...param) => {

console.log(param);

}

foo(123,456);

发布了13 篇原创文章 · 获赞 20 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/gufudhn/article/details/100856572