在下面的代码中,我希望访问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;
}
},
}
答案 0 :(得分:3)
方法或函数返回的对象与任何其他对象的行为没有区别。以下是使用示例中方法返回的environment
对象的示例:
var env = EXAMPLE.getEnvironment();
console.log(env.path);