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( "Hello World" );
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!