简体   繁体   English

Javascript。 从即时调用函数获取外部对象的引用

[英]Javascript. Get reference on outer object from immediate call function

I've wanted to optimize project but faced with problem. 我想优化项目,但遇到了问题。 I don't know how resolve this problem. 我不知道如何解决这个问题。 I want to use immediate call functoins which initialize IS_LOCALHOST property and CONTEXT_PATH, but I can't get access to isLocalhost() function and constant properties (like port number). 我想使用立即调用函数来初始化IS_LOCALHOST属性和CONTEXT_PATH,但是我无法访问isLocalhost()函数和常量属性(例如端口号)。 I try to put this as a parameter immediate call function but it references on document also I try to save refence like self: this and use this.self like peremeter and even util . 我尝试将this作为参数立即调用函数,但它引用了文档,我也尝试保存诸如self: this引用self: this并使用this.self甚至utilthis.self I don't understand how I can resolve this problem. 我不明白如何解决此问题。 Please, help me to understand working solution. 请帮我了解有效的解决方案。

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) )
    }

I'm not sure why you need to make these as IIFEs . 我不确定为什么您需要将它们作为IIFE

Why not make them normal functions as in the first example below, or simply set the properties at the appropriate time as in the second example? 为什么不像下面的第一个示例那样使它们成为普通函数,或者不像第二个示例那样在适当的时间简单地设置属性?

Example 1 -- normal functions 示例1-正常功能

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 util.IS_LOCALHOST;
    },

    CONTEXT_PATH: function() {
        return location.hostname + (location.port ? ':' + util.DEFAULT_TOMCAT_PORT : '') + '/' + (util.isLocalhost() ? util.WAR_FILE_NAME : '');
    },

    SECURE_CONTEXT_PATH: function() {
        return location.hostname + (location.port ? ':' + util.DEFAULT_SECURE_TOMCAT_PORT : '') + '/' + (util.isLocalhost() ? util.WAR_FILE_NAME : '');
    }
};

Example 2 -- set properties afterwards using the IIFEs 示例2-之后使用IIFE设置属性

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 util.IS_LOCALHOST;
    }
};

util.CONTEXT_PATH = (function() {
    return location.hostname + (location.port ? ':' + util.DEFAULT_TOMCAT_PORT : '') + '/' + (util.isLocalhost() ? util.WAR_FILE_NAME : '');
})();

util.SECURE_CONTEXT_PATH = (function() {
    return location.hostname + (location.port ? ':' + util.DEFAULT_SECURE_TOMCAT_PORT : '') + '/' + (util.isLocalhost() ? util.WAR_FILE_NAME : '');
})();

Don't create your object like this: 不要像这样创建对象:

var util = {
    foo: bar,
    blah: stuff

etc. This is verbose and cumbersome. 等等。这很繁琐。 Instead, wrap it in a IIFE and put all initialization logic in this function: 而是将其包装在IIFE中,并将所有初始化逻辑放入此函数中:

var util = (function() {
   var t = {};
   t.foo = bar;
   t.blah = stuff;
   return t;
})();

For example: 例如:

var util = (function() {

    var t = {};

    t.WAR_FILE_NAME =  'app-name/';
    t.DEFAULT_TOMCAT_PORT = 8080;
    t.DEFAULT_SECURE_TOMCAT_PORT = 8443;

    t.IS_LOCALHOST = false;
    var hostWithPort = location.host;
    if ( hostWithPort.indexOf('localhost') !== -1 || hostWithPort.indexOf('127.0.0.1') !== -1 ) {
        t.IS_LOCALHOST = true;
    }

    t.CONTEXT_PATH = location.hostname
        + (location.port ? '=' + t.DEFAULT_TOMCAT_PORT : '')
        + '/'
        + ( t.IS_LOCALHOST ? t.WAR_FILE_NAME : '' );

    t.SECURE_CONTEXT_PATH = location.hostname
        + (location.port ? '=' + t.DEFAULT_SECURE_TOMCAT_PORT : '' )
        + '/'
        + ( t.IS_LOCALHOST ? t.WAR_FILE_NAME : '');

    return t;

})();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM