发表于: 2020-07-19 19:36:03

1 1962


今天完成的事情:

今天准备小课堂内容与讲小课堂

知识要点:

全局环境中的this

 console.log(this);

总结:在全局作用域中它的 this 执行当前的全局对象(浏览器端是 Window,node 中是 global)

严格模式 ‘use strict’下的this

当函数被多个对象包裹时this的指向,如

                   'use strict';

                  function test() {

                  console.log(this);

                  };

                  test();

                  // undefined

原因:this 并不会指向全局,而是 undefined,这样的做法是为了消除 js 中一些不

严谨的行为在javascritp中,不一定只有对象方法的上下文中才有this, 全局函数调

用和其他的几种不同的上下文中也有this指代。 它可以是全局对象、当前对象或者任意对象,

这完全取决于函数的调用方式。JavaScript 中函数的调用有以下几种方式:

作为对象方法调用,作为函数调用,作为构造函数调用,和使用 apply 或 call 调用。

1.作为对象方法调用:this 被自然绑定到该对象

  var point = {

                x : 0,

                y : 0,

                moveTo : function(x, y) {

                    this.x = this.x + x;

                    this.y = this.y + y;

                    }

                };

                point.moveTo(1, 1)//this 绑定到当前对象,即 point 对象

2.作为函数调用:this被绑定到全局对象

function makeNoSense(x) {

                  this.x = x;

                  }

                  makeNoSense(5);

                  x;// x 已经成为一个值为 5 的全局变量

3.作为构造函数调用:this 绑定到新创建的对象上

 function Point(x, y){

                   this.x = x;

                   this.y = y;

                }

注:构造函数不使用new调用,则和普通函数一样。一般地,构造函数首字母大写

4.使用 apply 或 call 调用:在 JavaScript 中函数也是对象,对象则有方法,

apply 和 call 就是函数对象的方法。

  function Point(x, y){

                      this.x = x;

                      this.y = y;

                      this.moveTo = function(x, y){

                          this.x = x;

                          this.y = y;

                      }

                   }

                   var p1 = new Point(0, 0);

                   var p2 = {x: 0, y: 0};

                   p1.moveTo(1, 1);

                   p1.moveTo.apply(p2, [10, 10]);

常见问题与解决

问题一

    var obj = {

                   name: 'qiutc',

                   foo: function() {

                     console.log(this);

                   },

                   foo2: function() {

                     console.log(this);

                     setTimeout(this.foo, 1000);

                   }

                 }

                 obj.foo2();

现象:两次打印的this不一样

问题一可以这么这么解决:利用 闭包 的特性来处理

var obj = {

                      name: 'qiutc',

                      foo: function() {

                        console.log(this);

                      },

                      foo2: function() {

                        console.log(this);

                        var _this = this;

                        setTimeout(function() {

                          console.log(this);  // Window

                          console.log(_this);  // Object {name: "qiutc"}

                        }, 1000);

                      }

                    }

                    obj.foo2();

可以看到直接用 this 仍然是 Window;因为 foo2 中的 this 是指向 obj,

我们可以先用一个变量 _this 来储存,然后在回调函数中使用 _this,

就可以指向当前的这个对象了

问题二

'use strict';

                 function foo() {

                   console.log(this);

                 }

                 setTimeout(foo, 1);

                 // window

现象:加了严格模式,foo 调用也没有指定 this,应该是出来undefined,

但是这里仍然出现了全局对象问题二可以这么这么解决:利用 闭包 的特性来处理

var obj = {

                      name: 'qiutc',

                      foo: function() {

                        console.log(this);

                      },

                      foo2: function() {

                        console.log(this);

                        var _this = this;

                        setTimeout(function() {

                          console.log(this);  // Window

                          console.log(_this);  // Object {name: "qiutc"}

                        }, 1000);

                      }

                    }

                    obj.foo2();

可以看到直接用 this 仍然是 Window;因为 foo2 中的 this 是指向 obj,我们可以先用一

个变量 _this 来储存,然后在回调函数中使用 _this,就可以指向当前的这个对象了

如何理解this?

当一个函数被调用时,拥有它的object会作为this传入。在全局下,就是window or global,

其他时候就是相应的 object。也可以看到,call和apply就是利用这一点实现更改this 值的


二,明天计划的事情:整理知识点
三,遇到的问题:无



返回列表 返回列表
评论

    分享到