我可能是錯的內部功能(不是箭頭)是不明確的,但我的理解是,這「這」在對象
var foo = {
'bar': x => x + 1,
'baz': function(y){ return this.bar(y); }
};
foo.baz(1);
應該可以正常工作,因爲我照顧不來定義foo.baz
如箭功能等this
內部baz
等於foo
。當然,當我在控制檯上測試它時,它會按我的預期工作。
現在,我有一個非常類似的設置與React,其中this
由於某種原因未定義。這是代碼:
const MapRoom = {
'getStyleFromCoords': coords => {
return { // account for borders
'left': coords[0] + 1,
'top': coords[1] + 1,
'width': (coords[2] - coords[0]) - 2,
'height':(props.coords[3] - props.coords[1]) - 2,
};
},
'Disabled': function(props){
console.log('this', this); // undefined
const style = this.getStyleFromCoords(props.coords); // error
return (
<div
className="room-selected"
style={style}
title={props.number}
></div>
);
}
}
然後
renderRooms(){
// stuff
return this.state.coords.map((coords, index) => {
// more stuff
if(disabled){
return (
<MapRoom.Disabled
key={roomNumber}
number={roomNumber}
coords={coords}
/>
);
} else if(...){}
});
}
render(){
return (
<stuff>
{this.renderRooms()}
</stuff>
);
}
我說,他們應該是analagous,但似乎並非如此。當然這不是什麼大問題,因爲我可以將該函數移動到對象之外,因此不需要this
來引用它,但是我很想知道實際發生了什麼,因爲我無法重現錯誤。
如果它的事項,我transpiling與巴貝爾代碼和輸出
var MapRoom = {
'getStyleFromCoords': function getStyleFromCoords(coords) {
return { // account for borders
'left': coords[0] + 1,
'top': coords[1] + 1,
'width': coords[2] - coords[0] - 2,
'height': coords[3] - coords[1] - 2
};
},
'Disabled': function Disabled(props) {
console.log('this', this);
var style = this.getStyleFromCoords(props.coords);
return __WEBPACK_IMPORTED_MODULE_0_react___default.a.createElement('div', {
className: 'room-selected',
style: style,
title: props.number
});
}
}
這是的WebPack編譯器創建一個匿名函數裏面。
你告訴我們你是如何調用'foo.baz',但是不知道是哪位'MapRoom .Disabled'。當使用一個普通的'function'時,函數是如何調用'this'的_called_,而不是它被定義的地方,所以沒有辦法回答這個問題。 – loganfsmyth
@loganfsmyth你是完全正確的,我的壞。讓我知道現在是否夠了。 – cronos2
編譯結果不再使用'this'。 – melpomene