鍍金池/ 問答/HTML/ es6 map方法中的this怎么綁定

es6 map方法中的this怎么綁定

new Array(10).fill(0).map(_ => console.log(this), [])

this要指定為后面的空數(shù)組,es6里怎么寫

回答
編輯回答
尕筱澄

不用箭頭函數(shù)就行了唄 要不就new Array(10).fill(0).map(_ => console.log(this).bind([])

2017年4月11日 18:23
編輯回答
骨殘心

不用箭頭函數(shù)就行了,你不能以任何形式給箭頭函數(shù)綁定this,箭頭函數(shù)的this始終指向函數(shù)定義時所在的對象

new Array(10).fill(0).map(function(_){ console.log(this)}, [])
2017年3月14日 09:47
編輯回答
淺時光
new Array(10).fill(0).map(function(){
        console.log(this)
      },[])
2017年5月12日 20:52
編輯回答
選擇
var d = {a:1}
new Array(10).fill(0).map(function() {console.log(this)}, [d])

箭頭函數(shù)不是所有的時候都適用的,

2018年8月3日 07:01