2017-02-20 75 views
0

嵌套功能不起作用ReactJs。但它在正常工作JavascriptReactJs嵌套功能不起作用

它表明这个错误

Uncaught TypeError: Cannot set property 'getWlc' of undefined

我怎么能写里面ReactJs功能

function ldViewLayer() { 
    this.getWlc = function() { 
     alert('Try Alerts on Babel'); 
    } 
    this.getWlc(); 
} 
ldViewLayer(); 
+1

'this'在非结合的非箭头的功能,除非你调用未定义的严格模式用'new'功能。 – SimpleJ

回答

0

这里嵌套函数是修正:

function ldViewLayer { 
    const getWlc =() => { 
    alert('Try Alerts on Babel'); 
    } 
    getWlc(); 
} 
ldViewLayer(); 

但是你在做什么应该和班级一起,如果你确实使用了babel使用es6。

在ES6 syntaxe你应该做的:

class LdViewLayer { 

    getWlc =() => { 
    alert('Try Alerts on Babel'); 
    } 
} 

LdViewLayer ldViewLayer = new LdViewLayer(); 
ldViewLayer.getWlc(); 

这里是一个很好的代码行:https://github.com/ryanmcdermott/clean-code-javascript