2017-02-20 74 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