如何在 Array.map 中获得正确的“this”?

IT技术 javascript
2021-03-14 20:55:40

我假设有一些应用callapplyhere 但我不确定如何实现它。

http://codepen.io/anon/pen/oXmmzo

a = {
  foo: 'bar',
  things: [1, 2, 3],
  showFooForEach: function() {
    this.things.map(function(thing) {
      console.log(this.foo, thing);
    });
  }
}

a.showFooForEach();

假设我想要map一个数组,但在函数中,我需要访问属于this哪个foofunctionmap创建一个新的this背景,所以我显然需要coerse在某种程度上这种情况下回来了,但我怎么做,同时还具有访问thing

5个回答

刚刚意识到我应该Array.map()更仔细地阅读文档一个人只需要将this值作为第二个参数传入map()

http://codepen.io/anon/pen/VLggpX

a = {
  foo: 'bar',
  things: [1, 2, 3],
  showFooForEach: function() {
    this.things.map(function(thing) {
      console.log(this.foo, thing);
    }, this);
  }
}
a.showFooForEach();

此外,如何理解bind()call()apply()工作是严肃的JavaScript开发人员必须的。这些允许我们跳过愚蠢的任务,比如

var self = this;
myItems.map(function(item) {
  self.itemArray.push(item);
});

myItems.map(function(item) {
  this.itemArray.push(item);
}.bind(this));

截至 2018 年,您可以使用箭头函数:

a = {
  foo: 'bar',
  things: [1, 2, 3],
  showFooForEach: function() {
    this.things.map((thing) => {
      console.log(this.foo, thing);
    });
  }
}

a.showFooForEach();

你可以bind()在你的上下文中使用它。

a = {
  foo: 'bar',
  things: [1, 2, 3],
  showFooForEach: function() {
    this.things.map(function(thing) {
      console.log(this.foo, thing);
    }.bind(this));
  }
}

a.showFooForEach();

那是因为 JS 词法范围

来自 MDN:

bind() 方法创建一个新函数,当调用该函数时,将其 this 关键字设置为提供的值,并在调用新函数时在任何提供的参数之前提供给定的参数序列。

在此处阅读更多信息:https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

在这里:http : //javascriptissexy.com/javascript-apply-call-and-bind-methods-are-essential-for-javascript-professionals/

此外,map()确实接受第二个参数作为“this”

a = {
  foo: 'bar',
  things: [1, 2, 3],
  showFooForEach: function() {
    this.things.map(function(thing) {
      console.log(this.foo, thing);
    }, this);
  }
}

a.showFooForEach();

来自 MDNmap()文档:

参数

生成新数组元素的回调函数

thisArg 可选执行回调时用作 this 的值。

进一步阅读:https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

简短的建议

PS: Array.map通常被称为当你想要做的事与你的阵列,例如添加10到每一个项目,或者类似的东西。既然Array.map返回一个新的数组。如果您只使用 console.log 或不会影响数组本身的东西,您可以只使用 Array.forEach 调用

有以下三种方式:

一个普通的变量

一个没有特别奇怪的this

var self = this;
this.things.map(function(thing) {
  console.log(self.foo, thing);
});

函数.prototype.bind

this.things.map(function(thing) {
  console.log(this.foo, thing);
}.bind(this));

使用 Array.prototype.map 的第二个参数

(可选)第二个参数是调用内部函数的上下文。

this.things.map(function(thing) {
  console.log(this.foo, thing);
}, this);

前两种方式是通用的处理方式this第三个是特定于map, filter, forEach

感谢您为每个解决方案添加有用的上下文。
2021-05-15 20:55:40

不需要什么复杂的!map需要第二个参数thisArg,因此您只需将它与要在每个项目上调用的函数一起传入:

a = {
  foo: 'bar',
  things: [1, 2, 3],
  showFooForEach: function() {
    this.things.map(function(thing) {
      console.log(this.foo, thing);
    }, this);
  }
}

a.showFooForEach();

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

map 允许使用名为“thisArg”的第二个参数,因此您只需像这样使用它:

showFooForEach: function() {
    this.things.map(function(thing) {
      console.log(this.foo, thing);
    },this);