6.箭头函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
// ES6允许使用[箭头(=>)]来定义函数
//let fn = ()=>{}; //相当于let fn = function() {}
// let fn = (a, b) => {
// return a + b;
// };
// // 调用函数
// let result = fn(3, 5);
// console.log(result);
// 1.this是静态的,this始终指向函数声明时所在作用域下的this的值
// function getName() {
// console.log(this.name);
// }
// let getName2 = () => {
// console.log(this.name);
// };
// window.name = "张三";
// let person = {
// name: "zhangsan",
// };
// 直接调用
// getName();
// getName2();
// call方法调用,改变this指向
// getName.call(person);
// getName2.call(person);
// 2.不能作为构造函数实例化对象
// let Person = (name, age) => {
// this.name = name;
// this.age = age;
// };
// let me = new Person("xiao", 30);
// console.log(me);
// 3.不能使用arguments变量
// let fn = () => {
// console.log(arguments);
// };
// fn(1, 2, 3);
// 4.箭头函数简写
// 1)省略小括号:当新参有且只有一个的时候,可以省略小括号
let add = n=>{
return n+n
}
console.log(add(9))
// 2)省略大括号:当代码体只有一条语句时可以省略大括号,同时必须省略return,而且函数的执行结果就是函数的返回值
let pow = n=> n*n
console.log(pow(5))
</script>
</body>
</html>
浙公网安备 33010602011771号