箭头函数

90 阅读1分钟
| -------- | ------------------------------------------------------------------------ |
|          | /* 普通函数 */                                                               |
|          | // function fn(str1,str2){                                               |
|          | // let msg = str1+str2;                                                  |
|          | // return msg                                                            |
|          | // }                                                                     |
|          | // let a = fn('张三丰','会打太极');                                             |
|          | // document.write(a);                                                    |
|          |                                                                          |
|          | /* 普通函数可以作为构造函数 */                                                       |
|          | // let f = new fn();                                                     |
|          | // console.log(f);                                                       |
|          |                                                                          |
|          | /* 箭头函数 */                                                               |
|          | /* => 后面只有一段表达式 代表return 这段表达式 */                                        |
|          | /* => 后面有多段表达式 需要使用{} 并且手动return */                                      |
|          | /* 箭头函数只有一个参数 ()可以省略不写 多个参数需要加()*/                                       |
|          | // let fn = (str1,str2) =>{                                              |
|          | // let msg = str1+str2;                                                  |
|          | // return msg                                                            |
|          | // }                                                                     |
|          | // let a =fn('张无忌','会九阳神功');                                             |
|          | // document.write(a);                                                    |
|          |                                                                          |
|          | /* 箭头函数不可以作为构造函数 不然会报错 => Uncaught TypeError: fn is not a constructor */ |
|          | // let f = new fn();                                                     |
|          | // console.log(f);                                                       |
|          |                                                                          |
|          | /* 普通函数的 this 是谁调用指向谁 都没有调用 默认指向 window*/                                |
|          | /* 箭头函数没有自己的this 箭头函数的this 是上下文环境的this */                                |
|          | let obj1 = {                                                             |
|          | name:'张三',                                                               |
|          | // fn:function (){                                                       |
|          | // console.log(this);                                                    |
|          | // }                                                                     |
|          | fn:()=>{                                                                 |
|          | console.log(this);                                                       |
|          | }                                                                        |
|          | }                                                                        |
|          | let obj2 = {                                                             |
|          | name:'李四'                                                                |
|          | }                                                                        |
|          | // obj1.fn(); /* => obj1 */                                              |
|          | obj1.fn(); /* => window */                                               |
|          | /* 普通函数的this可以被call 或 apply 修改 */                                        |
|          | /* 箭头函数的this不会被call 或 apply 修改 */                                        |
|          | // obj1.fn.call(obj2); /* => obj2 */                                     |
|          | obj1.fn.call(obj2); /* => window */                                      |
|          | </script>