调用函数调用jsx
问题描述:
我想从外部调用一个函数,并且我希望这个函数可以访问相同的this
变量(这意味着道具,参考等)。我一直想:调用函数调用jsx
const something = (() => {
console.log(this); // undefined
});
class FooBar extends React.Component {
render() {
return (
<div>
{something.call(this)}
</div>
);
};
};
然而,使用Function.prototype.call
,this
仍然是不确定的。与apply
发生同样的事情,并且绑定不会立即执行函数。怎么了?
答
更改为,先生
const something = function() {
console.log(this); // FooBar
};
+0
是的,以这种方式工作 - 但为什么它不作为一个箭头功能? – user99999
+0
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_functions “通过调用或应用调用”部分:) – MariuszJasinski
你能试试这个:'const的东西=函数(){执行console.log(本); ''? –