我想优化项目,但遇到问题。我不知道如何解决这个问题。我想使用初始化IS_LOCALHOST属性和CONTEXT_PATH的立即调用函数,但我无法访问isLocalhost()函数和常量属性(如端口号)。我尝试将this
作为参数立即调用函数,但它在文档上也引用,我尝试保存像self: this
这样的区分,并使用this.self
,例如peremeter,甚至util
。我不明白我如何解决这个问题。请帮助我了解工作解决方案。Javascript。从立即调用函数获取外部对象的引用
var util = {
WAR_FILE_NAME : 'app-name/',
DEFAULT_TOMCAT_PORT : 8080,
DEFAULT_SECURE_TOMCAT_PORT : 8443,
/*** Pre construct block ***/
IS_LOCALHOST : (function() {
var isLocalhost = false, hostWithPort = location.host;
if (hostWithPort.indexOf('localhost') !== -1 || hostWithPort.indexOf('127.0.0.1') !== -1) {
isLocalhost = true;
}
return isLocalhost;
}()),
isLocalhost : function(){
return this.IS_LOCALHOST;
},
CONTEXT_PATH : (function (utilModule) {
return location.hostname + (location.port ? ':' + utilModule.DEFAULT_TOMCAT_PORT : '') + '/' + (utilModule.isLocalhost() ? utilModule.WAR_FILE_NAME : '');
}(util)),
SECURE_CONTEXT_PATH : (function (utilModule) {
return location.hostname + (location.port ? ':' + utilModule.DEFAULT_SECURE_TOMCAT_PORT : '') + '/' + (utilModule.isLocalhost() ? utilModule.WAR_FILE_NAME : '');
}(util))
}
[在对象文本声明自引用]的可能的复制(HTTP://计算器.com/q/4616202/1048572) – Bergi 2014-09-01 15:45:19