前端面试

前端面试遇到的算法

2020-08-03  本文已影响0人  百不理
var name = "Global"
    var object = {
     name: "local",
     fn: function() {
      return this.name;
     },
     getName: function() {
      console.log("1."+ this.name)
      return function() {
       return this.name;
      }
     },
     getFn:function() {
      return this.fn;
     }
    }
    var fun = object.getName()
    console.log("2."+fun());
    console.log("3."+fun.apply(object))
    console.log("4."+ fun.call(object))
    var fn = object.getFn()
    console.log("5."+fn())
答案解析:
1:local
2和5由于是return的一个function,this指向全局,所以是Global。
3、4是改变this指向,fun return 的this本来指向全局的,call和apply改变了this指向,this指向了object,所以是local
上一篇 下一篇

猜你喜欢

热点阅读