Q:

How do fix :"Cannot read property 'init' of undefined TypeError"?

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!

A:

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.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

