Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript scope issue, inside an anonymous function
    text
    copied!<p>Sorry I couldn't be anymore specific with the title.</p> <p>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.</p> <p>e.g. <code>page=home.html</code> would display <code>home.html</code></p> <p>The websites Javascript is wrapped inside an object, with each value containing different data, some pseudo code:</p> <pre><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(); }); })(); </code></pre> <p>My problem is that I'm trying to prepend the <code>dir</code> value to each of the <code>page</code> values (inside the object where the pages are defined), expecting to get the output of (in this pseudo code case) of <code>directory/to/content/page_value</code>, but instead <code>dir</code> is undefined when I'm trying to access it, I've tried the following to achieve what I want:</p> <ul> <li>wrapper.nav.config.dir + 'page_value'</li> </ul> <p>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.</p> <p>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.</p> <p>Hopefully the answer is simple and it's just an amateur mistake / lack of understanding.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload