Why assign `this` to `self` and run `self.method()`?

后端 未结 5 2110
我在风中等你
我在风中等你 2021-02-19 07:30

I\'m reading the source from mongoose

Collection.prototype.onOpen = function () {
  var self = this;
  this.buffer = false;
  self.doQueue();
};
<
5条回答
  •  梦毁少年i
    2021-02-19 07:57

    The only reason you would usually do that is if the call to doQueue() is inside a block that will change the value of this such as another function.

    In this case however it doesn't serve any purpose and was probably a remnant of older code that was not changed back.

提交回复
热议问题