Javascript ReactJs嵌套函数不工作
嵌套函数在ReactJs中不起作用。但是它在正常的Javascript中工作 它显示了这个错误 未捕获的TypeError:无法设置未定义的属性“getWlc” 如何在ReactJs函数中编写嵌套函数Javascript ReactJs嵌套函数不工作,javascript,function,reactjs,Javascript,Function,Reactjs,嵌套函数在ReactJs中不起作用。但是它在正常的Javascript中工作 它显示了这个错误 未捕获的TypeError:无法设置未定义的属性“getWlc” 如何在ReactJs函数中编写嵌套函数 function ldViewLayer() { this.getWlc = function() { alert('Try Alerts on Babel'); } this.getWlc(); } ldViewLayer(); 更正如下: func
function ldViewLayer() {
this.getWlc = function() {
alert('Try Alerts on Babel');
}
this.getWlc();
}
ldViewLayer();
更正如下:
function ldViewLayer {
const getWlc = () => {
alert('Try Alerts on Babel');
}
getWlc();
}
ldViewLayer();
但是你所做的应该是在课堂上,如果你使用的是babel,那么你肯定是在使用es6
在es6 Syntax中,您应该执行以下操作:
class LdViewLayer {
getWlc = () => {
alert('Try Alerts on Babel');
}
}
LdViewLayer ldViewLayer = new LdViewLayer();
ldViewLayer.getWlc();
这是一个非常好的代码行:
在严格模式下的非绑定非箭头函数中,此
未定义,除非您使用new
调用函数。