0

I've defined the following javascript object:

var WealthyLaughingDuckControl = {
    users: [],
    setUsers: function(new_users) {
        this.users = new_users;
    }
};

I would like it to be available inside the chrome/firebug's javascript console:

> WealthyLaughingDuckControl
// console result goes here

At now, I get:

> ReferenceError: WealthyLaughingDuckControl is not defined

How can I do that?

ducin
  • 25,621
  • 41
  • 157
  • 256
  • 1
    do you have the page where we can see this? Also please tell me you don't have that object within $(document).ready statement – James Daly Mar 29 '13 at 22:00
  • Oops... It is wrapped with `$(document).ready( function() {` :/... thanks for help! – ducin Mar 29 '13 at 22:04

2 Answers2

4

remove the var keyword, or append it to window: window.WealthyLaughingDuckControl

sites
  • 21,417
  • 17
  • 87
  • 146
  • Could you please take a look at [this answer](http://stackoverflow.com/a/1470494/769384) - the author said there's no difference between var and non-var if it's declared in the global scope. I did define my variable in the global scope and in fact - with `var` it's inaccessible inside js console, and without - it's accessible. Is this answer incorrect? – ducin Mar 29 '13 at 21:52
  • Maybe you are not in the global scope when using `var`, review in the code of your actual page instead of the code you write in your editor, maybe your environment wraps any javascript code you write, what exactly is your environment? is it plain Javascript? – sites Mar 29 '13 at 21:58
  • My env is NetBeans IDE. I don't expect it to wrap it in anything. Well, my variable was defined in the `$(document).ready( function() {` though... So it was not global, afterall (sorry...) – ducin Mar 29 '13 at 22:03
3

Define WealthyLaughingDuckControl at the top level i.e. not in any function.

Musa
  • 96,336
  • 17
  • 118
  • 137