箭头函数面试总结

1.箭头函数和普通函数的区别?

 箭头函数不能用于构造函数
 箭头函数没有 prototype 属性
 箭头函数不绑定 arguments
 箭头函数不绑定this
 箭头函数无法使用 call()或 apply()来改变其运行的作用域

2.箭头函数笔试题

// 箭头函数的this是继承父级作用域的this,而不是指向调用者
// 箭头函数没有自己的this也并不能使用call,apply以及bind去绑定this
function fun () {
    return () => {
        return () => {
            return () => {
                console.log(this.name)
                    }
                }
        }
}
var f = fun.call({name: 'foo'})
var t1 = f.call({name: 'bar'})()()   // foo
var t2 = f().call({name: 'baz'})()   // foo
var t3 = f()().call({name: 'qux'})   // foo

3.以及箭头函数的用法

//1.基本用法
let fn = v=>v+v;
//等价于
function fn(v){
    return v+v;
}
console.log(fn(9));  //9+9=18,输出18


//2.箭头函数不能用于构造函数
var Box=age=>{
    this.myAge=age;
}
var obj=new Box(20);//Box is not a constructor
console.log(obj.myAge);

//3.箭头函数没有prototype属性
var Foo = () => {};
console.log(Foo.prototype); // undefined

//4.箭头函数不绑定arguments
var arguments = 42;
var fn = () => arguments;
console.log(fn()); // 42

//5.箭头函数不绑定this
window.color = "red";
//let 声明的全局变量不具有全局属性,即不能用window.访问
let color = "green";
let obj = {
    color: "blue",
    getColor: () => {
        return this.color;//this指向window
    }
};
let sayColor = () => {
    return this.color;//this指向window
};
obj.getColor();//red
sayColor();//red

//6.箭头函数无法使用 call() 或 apply() 来改变其运行的作用域
window.color = "red";
let color = "green";
let obj = {
  color: "blue"
};
let sayColor = () => {
  return this.color;
};
sayColor.apply(obj); //red
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值