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.
-
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
5 Answers
Reset to default 2This 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)