如何从JavaScript函数返回一个对象并访问该返回的对象?

时间:2013-03-04 02:33:35

标签: javascript

在下面的代码中,我希望访问getEnvironment函数返回的环境对象。我如何在代码中的其他位置访问此对象?

window.EXAMPLE = {

        config : {
            local: 'http://localhost:8888/example',
            staging_v2: 'http://example.com/staging',
            production: 'http://example.com',
            image_path: '/images/',

        },

        getEnvironment : function () {
            if (window.location.href.indexOf(EXAMPLE.config.local) > -1) {
                var environment = {
                    path : EXAMPLE.config.local + EXAMPLE.config.image_path,
                }
                return environment;
            }

            if (window.location.href.indexOf(EXAMPLE.config.staging_v2) > -1) {
                var environment = {
                    path : EXAMPLE.config.staging_v2 + EXAMPLE.config.image_path,
                }
                return environment;
            }

            if (window.location.href.indexOf(EXAMPLE.config.production) > -1) {

                var environment = {
                    path : EXAMPLE.config.production + EXAMPLE.config.image_path,
                }
                return environment;
            }
        },

    }

1 个答案:

答案 0 :(得分:3)

方法或函数返回的对象与任何其他对象的行为没有区别。以下是使用示例中方法返回的environment对象的示例:

var env = EXAMPLE.getEnvironment();
console.log(env.path);
相关问题