简体   繁体   中英

Javascript scope issue, inside an anonymous function

Sorry I couldn't be anymore specific with the title.

I'm building a web-site (personal), which displays different content to the user depending on the query string that is used in the url.

eg page=home.html would display home.html

The websites Javascript is wrapped inside an object, with each value containing different data, some pseudo code:

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

My problem is that I'm trying to prepend the dir value to each of the page values (inside the object where the pages are defined), expecting to get the output of (in this pseudo code case) of directory/to/content/page_value , but instead dir is undefined when I'm trying to access it, I've tried the following to achieve what I want:

  • wrapper.nav.config.dir + 'page_value'

I've been playing around with the last 30 minutes trying to find out what I'm doing wrong, and even thought about hard-coding the URL in for each page.

The reasoning for wanting to do this is that my local development server and web host have different directory structures, so I don't want to re-write the URL's each time I want to develop + publish. As for why everything is wrapped inside an object, I thought it would be easier to maintain this way.

Hopefully the answer is simple and it's just an amateur mistake / lack of understanding.

The issue is that you can't refer to a variable that is being defined in that very definition.

So, inside the definition of wrapper , you can't refer to wrapper . And, inside the definition of config , you can't refer to config either and so on.

The usual design pattern for solving this is to initialize as much as you can in the declaration of your data structure and then do the rest in .init() when you can freely access all of it.

Change the first two lines to:

var wrapper = null;
(function(){
   wrapper = {

Otherwise, the wrapper is a local variable to your anonymous function.

The problem is that you're still busy defining the wrapper when you ask for its value, which is why it's still undefined.

The code below fails too:

var x = {
    y:"1",
    z:x.y
}

Why not:

//...
init: function(){
    //Runs on document ready
    this.foo();
    var config = this.nav.config;
    for (var page in config.pages) {
        config.pages[page] = config.dir + config.pages[page];
    }
},
//...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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