es6 箭头函数
箭头函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>箭头函数</title>
</head>
<body>
<script>
//声明
let fn = function(a,b) {
return a + b;
};
let fn2 = (a,b)=>{
return a + b;
};
//this是静态的,所在声明的作用域
console.log(fn(3,4));
console.log(fn2(3,4));
let getName = function(a,b) {
console.log(this.name);
};
let getName2 = ()=>{
console.log(this.name);
};
window.name = 'window name';
const school ={
name: '清华大学'
};
//直接调用
getName();
getName2();
//call方法调用
getName.call(school);
getName2.call(school);
//不能作为构造实例化对象 - 错误
// let Person = (name,age)=>{
// this.name = name;
// this.age = age;
// }
// let xiaoming = new Person('小明',23);
// console.log(xiaoming);
//不能使用arguments 变量
// let fn3 = ()=>{
// console.log(arguments);
// };
// fn3();
//4 箭头函数简写
//省略小括号
// let add = (a)=>{
// return a + a;
// }
// let add = a=>{
// return a + a;
// }
// 省略花括号
let pow = n => n * n;
console.log(pow(3));
</script>
</body>
</html>

浙公网安备 33010602011771号