Chr*_*ris 3 javascript jquery global-variables
考虑以下javascript:
$(function(){
var private_function = function(){
alert("private_function!");
}
setTimeout("private_function();", 1000);
});
Run Code Online (Sandbox Code Playgroud)
这会产生错误"private_function not defined".
有没有办法在javascript中推迟执行私有/匿名函数而不污染全局命名空间/通过全局模块公开它?
谢谢你的任何建议.
$(function(){
var private_function = function(){
alert("private_function!");
}
setTimeout(private_function, 1000);
});
Run Code Online (Sandbox Code Playgroud)
要么
$(function(){
var private_function = function(){
alert("private_function!");
}
setTimeout(function(){
private_function(); // with this method you can also pass some arguments
}, 1000);
});
Run Code Online (Sandbox Code Playgroud)