admin管理员组

文章数量:1393116

This is my main file:

$(document).ready(function () {
  function Page() {
    this.menu = new Menu();
    this.management = new Management();
    this.text = "text";
  }
window.Page= Page();
});

Now I want to access the Page from every other JS file:

I tried this:

console.log(Page.text);

Gives me : Uncaught ReferenceError: Page is not defined

Tried this:

console.log(window.Page.text);

Gives me : Uncaught TypeError: Cannot read property 'text' of undefined

What am I doing wrong?

This is my main file:

$(document).ready(function () {
  function Page() {
    this.menu = new Menu();
    this.management = new Management();
    this.text = "text";
  }
window.Page= Page();
});

Now I want to access the Page from every other JS file:

I tried this:

console.log(Page.text);

Gives me : Uncaught ReferenceError: Page is not defined

Tried this:

console.log(window.Page.text);

Gives me : Uncaught TypeError: Cannot read property 'text' of undefined

What am I doing wrong?

Share Improve this question asked Jan 15, 2013 at 7:24 JaanusJaanus 16.6k52 gold badges150 silver badges205 bronze badges 4
  • can't you move the main function outside of document.ready ? – charlietfl Commented Jan 15, 2013 at 7:27
  • My "guess" would be that window.Page = .. is not executed prior to the console.log statements - the ReferenceError on Page.text will only occur if the window.Page property is not set. Remember that ready is an asynchronous operation/callback. – user166390 Commented Jan 15, 2013 at 7:33
  • (Only variables lookup will result in a ReferenceError; failing property lookups evaluate to undefined - so accessing window.Page evaluates to undefined and (undefined).text results in the TypeError. However, the ReferenceError is the thing to pay attention to.) – user166390 Commented Jan 15, 2013 at 7:39
  • I am getting undefined now for the variable. – Jaanus Commented Jan 15, 2013 at 7:47
Add a ment  | 

4 Answers 4

Reset to default 4

Your issue is that within the Page function you are not creating any new object on the global context. You are creating a new Menu and new Management instance but on the current context.

Plus by calling the Window.Page = Page(), you are assigning the result of the Page function (which is void) to the window.Page object.

I suggest you do something like :

//- from any js file 
    function createPage() {
            var newPage = { title : 'new page', count : '2' };
            return newPage;
          }
    window.Page = createPage();

...

//- and from other js file
    $(document).ready(function () {                
    alert(window.Page.title);
    });

Note, I have replaced your menu and management properties with dummy content for this sample. Sample available @ this JSFiddle

Update : Code has been updated to illustrate the multiple js file proper usage.

Hope this helps

Function definitions don't need to be inside the document.ready() function. Only immediate actions that need to take place when the DOM is ready need to be put in there. So move the function to the toplevel.

You need to use window.Page = new Page();.

Either

window.Page = new Page();

or

function Page(){
   this.menu = new Menu();
   this.management = new Management();
   this.text = "text";
   return this;
}
window.Page = Page();

Then make sure other scripts don't try to use window.Page before it has been declared. You declare it in document.ready() callback function, so it's only going to be accessible once the DOM is ready and the callback function has been fired.

Edit:

without the context I'm not sure this is exactly what you're trying to do, but I think you just need a global Page object with some properties/methods. The easiest way to create it would be

window.Page = {
    menu : new Menu(),
    management = new Management(),
    text = "text"
};

without the document.ready() wrapper. Now obviously Menu and Management need to be defined before this code is executed. If any of these functions relies on DOM just move all your scripts to the end of the document. Any script that needs access to window.Page has to be included after this one.

本文标签: javascriptAccessing global variable from other JS fileStack Overflow