最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Accessing global function variables in Javascript - Stack Overflow

programmeradmin4浏览0评论

Somehow I thought you could define a function as a global variable and access its internal variables. Example:

var func = function(){
    var connected = true;
}

console.log(func.connected);

However, this still es up as "undefined". I thought it would be interesting to "namespace" certain variables like this.

I don't want to use objects/lists/dictionaries (how you prefer to call them) because you can delete those elements.

Somehow I thought you could define a function as a global variable and access its internal variables. Example:

var func = function(){
    var connected = true;
}

console.log(func.connected);

However, this still es up as "undefined". I thought it would be interesting to "namespace" certain variables like this.

I don't want to use objects/lists/dictionaries (how you prefer to call them) because you can delete those elements.

Share Improve this question asked Dec 28, 2010 at 15:08 Jelle De LoeckerJelle De Loecker 22k29 gold badges104 silver badges146 bronze badges 3
  • 1 So what if you can delete them? You can delete anything else, too. – SLaks Commented Dec 28, 2010 at 15:11
  • I don't want to use objects because you can delete those elements Sorry to disappoint you but functions are objects too: delete func. Indeed everything in javascript are objects. – slebetman Commented Dec 28, 2010 at 16:20
  • Yes, but you can't delete a variable that has been declared using var – Jelle De Loecker Commented Dec 29, 2010 at 3:05
Add a ment  | 

5 Answers 5

Reset to default 2

This is not possible.
In fact, it doesn't even make sense.
Each call to a function produces a separate set of local variables.

You should use objects to create namespaces, even though you can delete them.

If you want to, you can also make a class:
Note that you need to make an instance of the class:

function MyClass() { 
    this.connected = true;
}

var myInstance = new MyClass();
console.log(myInstance.connected);

However, you should not use classes to create singleton namespaces; there is no point.

Instead, you should write

var myNamespace = { connected: true };

console.log(myNamespace.connected);

var inside a function makes it private. use this.connected = true to make it public.

var func = function(){
    this.connected = true;
}

PS - As far as I know, all properties of an object are deletable unless they're non-enumerable, which I don't think you can easily specify. You should use this.connected even though it is deletable.

There is a good readup here on public/private methods and "privileged" methods.

EDIT: I assumed you knew about instantiating.. anyway just do x = new func to create an instance, then x.connected.

by using var it is private.

use it like this:

var func = function(){
    this.connected = true;
}
var namespace = new func();

console.log(namespace.connected);

remember that it needs to be instantiated.

You can use JSON notation like this:

var func = {
   connected:true,
   func:function(){
     func.connected = true;
   }
}
console.log(func.connected);
var MyClass = function() {
    function clazz() {
        this.message = "Hello"; //instance variable
    }

    clazz.connected = true; //static variable

    return clazz;
}();


alert(MyClass.connected)
alert(new MyClass().message)
发布评论

评论列表(0)

  1. 暂无评论