这周学习了一些ps,然后复习了之前学过的es6,就简述一下看了什么吧
1.箭头函数的声明与调用
ES6允许使用箭头(=>)定义函数
let fn=function(){}
let fn=(a,b)=>{
return a+b;
}
//调用函数
let result=fn(1,2);
console.log(result);
2.箭头函数的特点
1.this是静态的,this始终指向函数声明时所在的作用域下的this这个值
function getName(){
console.log(this.name);
}
let getName2 = () => {
console.log(this.name);
}
//设置window对象的name属性
window.name='尚硅谷';
const school={
name:"ATGUIGU"
}
//call
getName.call(school); //ATGUIGU
getName2.call(school); //尚硅谷
箭头函数的this永远指向其父作用域,任何方法都改变不了,包括call,apply,bind。
普通函数的this指向调用它的那个对象。
2.不能作为构造实例化对象
let Person=(name,age)=>{
this.name=name;
this.age=age;
}
let me=new Person('xiao',30);
console.log(me);// Uncaught TypeError: Person is not a constructor
3.不能使用arguements变量
let fn= () =>{
console.log(arguements);
}
fn(1,2,3);//Uncaught ReferenceError: arguements is not defined
4.箭头函数的简写
1)省略小括号,当形参有且只有一个的时候
let add = n =>{
return n+n;
}
console.log(add(9));
2)省略花括号,当代码只有一条语句的时候,此时return必须省略,而且语句的执行结果就是函数的返回值
let pow=(n)=> n*n;
console.log(pow(9))
完毕!