Javascript namespace declaration with function-prototype?

The way I would do it is using the Module pattern You basically encapsulate all your "Module" logic in a self executing function that would return an object having your classes, functions, variables etc... Think of the return value as exposing your Module API Namespace = (function () { /** Class obj **/ var obj = function () { this. Foo = 'bar'; }; obj. Prototype = { start: function () { this.

Foo = 'fubar'; } }; /** Class obj2 **/ var obj2 = function () { this. Bar = 'foo' }; obj2. Prototype = { start: function () { this.

Bar = 'barfoo'; }, end: function () { this. Bar = ''; } }; return { obj : obj, obj2: obj2 }; })(); var o = new Namespace.obj() o.start() In order to further encapsulate the "obj" class methods and constructor we could do the following: Class obj **/ var obj = (function () { /** class Constructor **/ var obj = function () { this. Foo = 'bar'; }; /** class methods **/ obj.

Prototype = { start: function () { this. Foo = 'fubar'; } }; return obj; })() There is also an important feature that comes for free using this pattern, which is "Private variables", consider the following: Class Foo **/ var Foo = (function () { // Private variables var private_number = 200 /** class Constructor **/ var Foo = function () { this. Bar = 0; }; /** class methods **/ Foo.

Prototype = { add: function () { this. Bar += private_number; } }; return Foo; })(); foo = new Foo(); alert(foo. Bar); // 0 foo.add(); alert(foo.

Bar);// 200 alert(foo. Private_number) //undefined.

The way I would do it is using the "Module pattern". You basically encapsulate all your "Module" logic in a self executing function that would return an object having your classes, functions, variables etc... Think of the return value as exposing your Module API. Namespace = (function () { /** Class obj **/ var obj = function () { this.

Foo = 'bar'; }; obj. Prototype = { start: function () { this. Foo = 'fubar'; } }; /** Class obj2 **/ var obj2 = function () { this.

Bar = 'foo' }; obj2. Prototype = { start: function () { this. Bar = 'barfoo'; }, end: function () { this.

Bar = ''; } }; return { obj : obj, obj2: obj2 }; })(); var o = new Namespace.obj() o.start() In order to further encapsulate the "obj" class methods and constructor we could do the following: /** Class obj **/ var obj = (function () { /** class Constructor **/ var obj = function () { this. Foo = 'bar'; }; /** class methods **/ obj. Prototype = { start: function () { this.

Foo = 'fubar'; } }; return obj; })(); There is also an important feature that comes for free using this pattern, which is "Private variables", consider the following: /** Class Foo **/ var Foo = (function () { // Private variables var private_number = 200 /** class Constructor **/ var Foo = function () { this. Bar = 0; }; /** class methods **/ Foo. Prototype = { add: function () { this.

Bar += private_number; } }; return Foo; })(); foo = new Foo(); alert(foo. Bar); // 0 foo.add(); alert(foo. Bar);// 200 alert(foo.

Private_number) //undefined.

Nice approach, +1. – Jiri Aug 21 at 12:25 Thans Amjad, this is great. But now I'm stumbling again.Do I see it right: With this approach it's not possible to make a Namespace.blah() function that is detached form obj?

– Johnny Sterlak Aug 21 at 14:52 1 @Johnny If I understand your question right, just add a blah function to the return object: . . Return { obj : obj, obj2: obj2, blah: function () {/* do something */} }; – Amjad Masad Aug 22 at 9:43 Amjad, thanks a lot.

This is exactly what I wanted! Now I got it. – Johnny Sterlak Aug 22 at 10:44.

Yes because, you cannot use this type of chaining in an object declaration obj. Prototype or obj. Something here, because the language sees obj as a non-object value.

You can fake such an effect like this Namespace = {}; Namespace. Obj =function() { this. Foo="bar"; }; Namespace.obj.prototype.

Start = function(tabinst) { this. Foo="fubar"; }; console. Log( Namespace.obj.

Prototype ); (see this fiddle http://jsfiddle.net/WewnF/ ) EDIT: Wow, I just noticed that what I said was already within the question. I 'm so sorry did not notice that sooner... Well the way you described yourself is the correct method of achieving this. Otherwise you can re-write your code like this - but is not exactly what you 're after and won't work the same (since obj won't be a function itself and you will have to call its main function like this obj.main(); ) Namespace = { obj: { main : function() { this.

Foo="bar"; }, prototype : { start: function(tabinst) { this. Foo="fubar"; } } } } EDIT 2: See this fiddle http://jsfiddle.net/NmA3v/1/ Namespace = { obj: function() { this. Foo="bar"; }, prototype: { obj : { start : function( hi ) { alert( hi ); } } }, initProto : function(){ for( var key in Namespace ) { if( key!

== "prototype" ) { for( var jey in Namespace. Prototype key ) Namespace key . Prototype jey = Namespace.

Prototype key jey ; } } } } Namespace.initProto(); console. Log( Namespace. Obj); var test = new Namespace.obj(); test.

Start( " This will have the exact same effect. Explanation : we are declaring our objects as normal properties-functions, and then use a master prototype object which containers objects with the same names as above, for example for each Namespace. Obj, there is also a Namespace.prototype.

Obj which contains the functions we want to add in the prototype chain. Then with namespace.protoInit(), we iterate through all properties - and extract the functions from the Namespace. Prototype key and add them to Namespace key .

Prototype - succesfully extending the prototype object! A bit unorthodox, but works!

The first snippet in your edit will not work as you might think. Obj. Main and obj.

Prototype are two different independent functions. Yes, this will refer to the same object if you call them without new, but only because it refers to window. So you will make foo global.

– Felix Kling Aug 21 at 12:28 Your second example limits Namespace to only contain one "class" which somehow defeats the purpose of the namespace. – Felix Kling Aug 21 at 12:33 You are correct for the first example, and I feel stupid for not noticing this sooner, but I disagree about the second one. Why does it limit it to only one "class"?

If you use more objects, it will iterate through them and assign to them the correct prototype values. – Pantelis Aug 21 at 12:46 Ah true, you have obj inside prototype.... I missed that. Sorry for that.

I would still say this is not a straightforward approach. – Felix Kling Aug 21 at 12:49.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions