ES6箭头函数总结

一、箭头函数

//1.定义函数的方式:function
function test1(){
console.log('1111')
}
const test2 = function (){
console.log('222')
}

test1();
test2();

//2.对象字面量中定义函数
const obj = {
aaa:function (){
console.log('3333')
},
bbb(){
console.log('444')
}
}

//3.ES6中的箭头函数
// const ccc = (参数列表) => {
//
// }
const ddd = () =>{

}

//3.1参数问题
//3.1.1 放入多个参数的写法
const fff = (num1,num2) => {
return num1 + num2;
}
//3.1.2 放入一个参数是,括号可以省略
const power = num => {
return num * num;
}

fff(10.20);
//3.1.3 函数代码块中只有一行代码时的写法,大括号{}也可以省略
const hh = (num1,num2) => num1 * num2

console.log('helloww');

//箭头函数中的this是如何查找的?
//答案:向外层作用域中,一层层的查找this,直到有this的定义
const obj1 = {
aaa(){
setTimeout(function (){
console.log(this) //window
})
setTimeout(() => console.log(this)) //obj

}

}

posted @ 2021-10-22 16:42  a龙  阅读(32)  评论(0编辑  收藏  举报