javascriptjquery

Why am I getting undefined property when calling a javascript function from another javascript file?


I'm using OOP javascript method (correct me if I'm wrong please), to make use of js custom functions across the all website and javascript files.

There is a main template.js file were I store the all js functions I need:

var template = function(){

    /*** ******************** ***/
    /*** 1.1 MAIN INIT METHOD ***/
    function _init(){
        __initTooltip();
    }

    /*** ********************* ***/
    /*** 1.2 PRIVATE FUNCTIONS ***/
    
    // some functions before

    function __capitalize(string){
        return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
    }

    /*** ************************************************** ***/
    /*** 1.3 MAKE PRIVATE FUNCTIONS ACCESSIBLE FROM OUTSIDE ***/
    return {
        init:function(){
            _init();
        },
        capitalize:function(string){
            __capitalize(string);
        }
    };



}();

$(document).ready(function(){
    template.init();
});

so if I need to call a __capitalize() function, it will be accessible via the firebug/chrome console like this: template.capitalize('some Text');

It should return me Some text instead of undefined property ... What am I doing wrong here ? Any one noticed something I missed here pls ?


Solution

  • You are not returning anything from the capitalize method.

    You need to return the capitalized value from capitalize(which was returned by __capitalize)

        capitalize:function(string){
            return __capitalize(string);
        }