JavaScript之this详解

1.函数预编译过程 this —> window

2.全局作用域里 this —> window

3.call/apply 可以改变函数运行时this指向

4.obj.func();   func()里面的this指向obj)

 

经典例题:
            var name = '222';
            var a = {
                name : "111",
                say : function() {
                    console.log(this.name);
                }
            }
            var fun = a.say;//把say : function() {console.log(this.name);}赋给fun
            fun();//222  fun在全局中执行  this -- >window
             a.say();//111 this-- >a
            var b = {
                name : "333",
                say : function(fun) {
                    console.log(this);
                    fun();
                }
            }
            b.say(a.say);//222
            b.say = a.say;
            b.say();//333

猜你喜欢

转载自blog.csdn.net/duyujian706709149/article/details/88131309
今日推荐