简体   繁体   English

从另一个JavaScript文件调用javascript函数时,为什么会得到未定义的属性?

[英]Why I'am getting undefined property when calling a javascript function from another javascript file?

I'am using OOP javascript method (correct me if I'am wrong pls), to make use of js custom functions across the all website and javascript files. 我正在使用OOP javascript方法(如果我错了,请纠正我),以便在所有网站和javascript文件中使用js自定义函数。

There is a main template.js file were I store the all js functions I need: 我存储了我需要的所有js函数,这是一个主要的template.js文件:

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'); 因此,如果我需要调用__capitalize()函数,则可以通过如下所示的firebug / chrome控制台对其进行访问: template.capitalize('some Text');

It should return me Some text instead of undefined property ... What I'am doing wrong here ? 它应该返回我Some text而不是undefined属性...我在这里做错了什么? Any one noticed something I missed here pls ? 有人注意到我在这里想念的东西吗?

You are not returning anything from the capitalize method. 您没有从capitalize方法返回任何内容。

You need to return the capitalized value from capitalize (which was returned by __capitalize ) 您需要从capitalize (由__capitalize返回)中返回资本化值。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个 function javascript 调用异步 function 时变得未定义 - Getting undefined when calling an async function from another function javascript 从另一个文件调用Javascript函数“无法读取未定义的属性” - Calling Javascript function from another file 'Cannot read property of undefined' 为什么我的JavaScript对象中的属性未定义? - Why am I getting property undefined in my JavaScript object? 为什么我得到这个“无法读取未定义的属性”长度”? (JavaScript) - Why am I getting this 'cannot read property 'length' of undefined'? (JavaScript) 为什么我得到未定义的输出Javascript? - Why am I getting undefined output Javascript? JavaScript:为什么我在尝试从数组赋值时得到“未定义” - JavaScript: Why am I getting "undefined" when trying to assign value from an array 为什么在使用 split function of javascript 时出现错误? - Why am I getting an error when using split function of javascript? 谁能解释为什么我在Javascript中遇到此错误? [无法读取未定义的属性'length'] - Can anyone explain why I am getting this error in Javascript? [Cannot read property 'length' of undefined] 当我尝试在 JavaScript 中将变量从一个函数传递到另一个函数时,我收到 Reference Error: variable is not defined 错误(邮递员) - I am getting Reference Error: variable is not defined error when I am trying to pass variable from one function to another in JavaScript ( postman) 我在我的javascript对象属性中获得未定义的值 - i am getting undefined value in my javascript object property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM