It seems there are so many ways to set up a JavaScript application so it is confusing as to which one is correct or best. Are there any difference to the below techniques or a better way of doing this?
MyNamespace.MyClass = {
someProperty: 5,
anotherProperty: false,
init: function () {
//do initialization
},
someFunction: function () {
//do something
}
};
$(function () {
MyNamespace.MyClass.init();
});
Another way:
MyNamespace.MyClass = (function () {
var someProperty = 5;
var anotherProperty = false;
var init = function () {
//do something
};
var someFunction = function () {
//do something
};
return {
someProperty: someProperty
anotherProperty: anotherProperty
init: init
someFunction: someFunction
};
}());
MyNamespace.MyClass.init();
The first technique feels more like a class. I am coming from server-side background if this makes a difference. The second technique seems more redundant and a bit awkward, but I see this used a lot too. Can someone please help shed some light and advise the best way to move forward? I want to create a application with lots of classes talking to each other.
How to combine namespace and class declaration:
Accessing your value:
Now, for the Class and Function: If you use
new
, the wordthis
inside the function will point to it's constructor. If you don't usenew
,this
will point to the namespace. So,Using a Class:
Using the Function:
If you construct the object without
new
,this
will point to the namespace, so the namespace will be "destroyed" becauseMyClass.class_property
andMyClass.class_method
will be added to it.Do neither of those things.
Make a javascript "class":
One with static vars:
With a "constructor" (all of the examples have a "constructor", this one just has parameters to work with):
With all of the above you can do:
But you cannot do (from the outside):
The first example is simply an Object literal - it cannot be instantiated and doesn't have private members. The second example has some incorrect syntax (
var someProperty: 5
should bevar someProperty = 5
) but is using a closure to encapsulate internal private state within a self-invoking anonymous function.The second approach looks better for encapsulating private members, but could be made more "Object-oriented" by making it an instantiable class:
Then you can instantiate it with the 'new' keyword:
Why you should never use
like in second example:
When you use namespace you have to think about it as about declaration, not the instance.
And no metter how much Module anti-patter was popular. Declaration gives you an ability to use the same code with different instances in an expected way for other developers . The quality of code and simplicity of its reading increases. The goal of any developer is to write a simple code to be read, not a shorter or D.R.Y. - but simple to be read and be understanded by another developer. That decreases the number of bugs first. (c) S. McConnell
I use the following syntax for the instantiable classes with namespace
jsfiddle: http://jsfiddle.net/rpaul/4dngxwb3/1/