How do I get the right “this” in an Array.map?

前端 未结 5 1286
悲哀的现实
悲哀的现实 2020-11-27 22:33

I assume there is some application of call or apply here but I\'m not sure how to implement it.

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



        
5条回答
  •  孤街浪徒
    2020-11-27 22:41

    map allows a second argument called "thisArg" so you just use that like so:

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

提交回复
热议问题