I'm trying to create a function within a namespace that will get me a new object instance.
I get syntax error trying to do the following:
var namespace = {
a : function(param){
this.something = param;
},
a.prototype.hi = function(){
alert('hi');
},
b : function(){
var t = new a('something');
}
};
What is the correct syntax for doing this? Is it not possible to do within the namespace object? I don't want to declare the namespace first. Thanks
I don't want to declare the namespace first.
You can do weird things like this:
var namespace = {
init: function() {
a.prototype.whatever = function(){};
},
a: function(x) {
this.x = x;
}
}
namespace.init();
var myA = new namespace.a("x");
But why not try the module pattern if you want to encapsulate everything? It's way cleaner:
var namespace = (function() {
function a() {};
function b(name) {
this.name = name;
};
b.prototype.hi = function() {
console.log("my name is " + this.name);
}
return {
a: a,
b: b
}
})();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With