简体   繁体   中英

Uncaught TypeError: controller.init is not a function

I am trying to create some JavaScript code using ES6 syntax, but it is giving me some errors. When I code it with ES5, it runs fine.

index.js


let UIController = () =>  {
    return {
        getInput: () => {

        },
        clearFields: () => {

        },
    }
}

let dataController = () => {

}

let controller = (dataCtrl, UICtrl) =>  {
    let setupEventListeners = () => {                     
    };

    return {
        init: () => {
            console.log('Application has started.');            
            setupEventListeners();
        }
    };
}

controller(dataController, UIController);
controller.init();

I expect it to log The application has started , but instead, it gives me an error.

You defined controller as a function. So controller.init() doesn't work, because it has not returned its value yet. You can store and access its properties of the returned object of the controller function like so:

let someVar = controller(dataController, UIController);
someVar.init();

Or without any additional variable assignment

controller(dataController, UIController).init();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM