对不起,我不能再具体与标题。Javascript范围问题,在一个匿名函数
我正在构建一个网站(个人),它根据url中使用的查询字符串向用户显示不同的内容。
例如page=home.html
将显示home.html
该网站的JavaScript被包装对象内,包含不同的数据每个值,一些伪代码:
(function(){
var wrapper = {
init: function(){
//Runs on document ready
this.foo();
this.nav.render();
},
foo: function(){
//Some functionality goes here for the website, e.g. Display something from an API
},
nav: {
//Functionality to handle the navigation, has different properties
config: {
//Contains the config for nav, e.g. page names + locations
dir: '/directory/to/content/',
pages: {
page_name: wrapper.nav.config.dir + 'page_value'
}
},
render: function(){
//some code
},
routes: function(){
//some code}
}
}
};
$(function(){
wrapper.init();
});
})();
我的问题是,我想前面加上dir
值每page
值(在页面被定义的对象内部),期望得到directory/to/content/page_value
的输出(在这个伪代码的情况下),但是dir
是未定义的,当我试图访问它时,我试过了以下达到我想要的:
- wrapper.nav.config.dir +“page_value”
我一直在最后30分钟试图找出什么我做错了,甚至还想过玩弄硬编码每个页面的URL。
想要做到这一点的原因是我的本地开发服务器和Web主机有不同的目录结构,所以我不想每次我想要开发+发布时重写URL。至于为什么一切都被包裹在一个物体中,我认为这样保持容易。
希望答案很简单,这只是一个业余的错误/缺乏理解。
哪里是代码'wrapper.nav.config.dir +“page_value''行?这不在你上面的例子中。 – jfriend00 2012-01-07 21:33:45
这是我用来尝试和访问目录属性,没有成功 – Daniel 2012-01-07 21:47:09
我也编辑了问题,我试图使用它。 – Daniel 2012-01-07 21:53:35