[英]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.