admin管理员组

文章数量:1314832

I using Jquery-3.2.1, Jquery-Ui 1.12.1.In my JavaScript file:

window.TruyenOnlineScript = (function () {
    var _this = {};

    _this.init = function () {
        _this.initSearchMobile();
        _this.initSidebar();
    };   

    _this.initSearchMobile = function () {
        //Open Input Search Mobile
        $('.js-open-search-box-mobile').on('click', function (event) {
            event.preventDefault();
            $('body').addClass('open-search-box');
            setTimeout(function () {
                $('#js-search-input-mobile').focus()
            }, 500);
        });
    };

    _this.initSidebar = function () {
        //Open Navbar Moblie
        $('.js-open-sidebar').on('click', function (event) {
            event.preventDefault();
            event.stopPropagation();
            $('body').addClass('open-sidebar');
        });
    };

})();

$('document').ready(function () {
    window.TruyenOnlineScript.init();
});

But I got the error "init of undefined":

Can anybody show me how to fix it? Thank you!

I using Jquery-3.2.1, Jquery-Ui 1.12.1.In my JavaScript file:

window.TruyenOnlineScript = (function () {
    var _this = {};

    _this.init = function () {
        _this.initSearchMobile();
        _this.initSidebar();
    };   

    _this.initSearchMobile = function () {
        //Open Input Search Mobile
        $('.js-open-search-box-mobile').on('click', function (event) {
            event.preventDefault();
            $('body').addClass('open-search-box');
            setTimeout(function () {
                $('#js-search-input-mobile').focus()
            }, 500);
        });
    };

    _this.initSidebar = function () {
        //Open Navbar Moblie
        $('.js-open-sidebar').on('click', function (event) {
            event.preventDefault();
            event.stopPropagation();
            $('body').addClass('open-sidebar');
        });
    };

})();

$('document').ready(function () {
    window.TruyenOnlineScript.init();
});

But I got the error "init of undefined":

Can anybody show me how to fix it? Thank you!

Share Improve this question asked Oct 26, 2018 at 13:53 PhuongPhuong 3393 gold badges7 silver badges16 bronze badges 1
  • 2 Your initialization function does not have a return statement. – Pointy Commented Oct 26, 2018 at 13:54
Add a ment  | 

2 Answers 2

Reset to default 4

You are setting window.TruyenOnlineScript to the return value of an Immediately Invoked Function Expression:

window.TruyenOnlineScript = (function () {
   . . .
})();

but that expression doesn't return any value and so window.TruyenOnlineScript winds up being undefined (and that's why you can't call init() on undefined).

You need to have the IIFE return an object for TruyenOnlineScript to reference.

window.TruyenOnlineScript = (function () {
    var _this = {};

    _this.init = function () {
        _this.initSearchMobile();
        _this.initSidebar();
    };   

    _this.initSearchMobile = function () {
        //Open Input Search Mobile
        $('.js-open-search-box-mobile').on('click', function (event) {
            event.preventDefault();
            $('body').addClass('open-search-box');
            setTimeout(function () {
                $('#js-search-input-mobile').focus()
            }, 500);
        });
    };

    _this.initSidebar = function () {
        //Open Navbar Moblie
        $('.js-open-sidebar').on('click', function (event) {
            event.preventDefault();
            event.stopPropagation();
            $('body').addClass('open-sidebar');
        });
    };
    
    return _this; // <-- Now this will be returned

})();

$('document').ready(function () {
    window.TruyenOnlineScript.init();
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Some users have already given you the solution, but I want to show you another way to create the same object. I can't say it's "better", but it's clearer and a little easier to understand:

window.TruyenOnlineScript = {
    init: function () {
        this.initSearchMobile();
        this.initSidebar();
    },

    initSearchMobile: function () {
        //Open Input Search Mobile
        $('.js-open-search-box-mobile').on('click', function (event) {
            event.preventDefault();
            $('body').addClass('open-search-box');
            setTimeout(function () {
                $('#js-search-input-mobile').focus()
            }, 500);
        });
    },

    initSidebar: function () {
        //Open Navbar Moblie
        $('.js-open-sidebar').on('click', function (event) {
            event.preventDefault();
            event.stopPropagation();
            $('body').addClass('open-sidebar');
        });
    }   
};

本文标签: javascriptHow do fix quotCannot read property 39init39 of undefined TypeErrorquotStack Overflow