123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <!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>箭头函数</title>
- </head>
- <body>
- <script>
- let fn = function(){
- }
- let fn = (a,b) =>{
- return a+b;
- }
- //调用
- let r = fn(3,4)
- console.log(r)
- //this是静态的,始终指向函数声明时所在的作用域下的this值
- function getName(){
- console.log(this.name)
- }
- let getName2 = () =>{
- console.log(this.name)
- }
- window.name = 'zhangsan'
- const school = {
- name = 'lisi'
- }
- getName() //zhangsan
- getName2() //zhangsan
- //call
- getName.call(school) //lisi
- getName2.call(school) //zhangsan 保持不变
- //不能作为构造函数实例化对象
- let Person = (name,age) =>{
- this.name = name;
- this.age = age;
- }
- let me = new Person('zhang',18)
- console.log(me) //报错
-
- //不能使用arguments变量
- let fn = () =>{
- console.log(arguments) // not defined
- }
- fn()
- //简写
- //省略小括号 当形参有且只有一个时
- let add = n =>{
- return n*n
- }
- //省略花括号 当代码只有一条语句时,return也必须省略
- let pow = (n) => n*n
-
- </script>
- </body>
- </html>
|