I am creating an object literal, which I'm using as a namespace. The object literal (namespace) happens to contain a constructor function, MyConstructor. I want to extend the prototype of MyConstructor, inside the object literal. If I pull the declaration for MyConstructor out and put it above the declaration of myNamespace, all is well, except MyConstructor is no longer namespaced by myNamespace. How best to make this work? The following fails to compile because
of the '.' in this line:MyConstructor.prototype = {
Object literal provided below.
Thanks, Mike
var myNamespace = {
a: 3,
b: function (msg) {
alert(this.a);
},
MyConstructor: function(xx) {
this.x = xx;
},
MyConstructor.prototype = {
a: 1,
b: function() {
return true;
}
}
}开发者_StackOverflow中文版;
Update: The accepted solution works. However, the very premise of my question was flawed. I would not use an object literal at all in this situation, because it is harder to unit test, as I found out. Instead, I would use a normal object created with a constructor, and put the private objects inside it. Another thing is, a "self/immediately executing function", while beautiful in some ways, is a bit harder to explain to a newbie.
You simply cannot do that in an object literal. Sorry.
As for a way to work around it, use a function to create your myNamespace
. That way it doesn't have to be a literal, but you can still get the effect of a namespace:
var myNamespace = (function(){
var ns = {
a: 3,
b: function (msg) {
alert(this.a);
},
MyConstructor: function(xx) {
this.x = xx;
}
};
ns.MyConstructor.prototype = {
a: 1,
b: function() {
return true;
}
return ns;
}());
So what happens here is that myNamespace is set to the value of a function that is called immediately and returns the actual namespace object that you're after. No other code can access what's in the function, but since it is inside a function you can do anything, not just object literals. Which solves your problem ;)
Just use the namespace
var myNamespace = {
myConstructor: function( x ) {
this.x = x;
}
}
myNamespace.myConstructor.prototype = {
//...
}
Or another variation
var myNamespace = (function(){
function myConstructor(){}
myConstructor.prototype = {};
return {
myConstructor: myConstructor
}
})();
How about, create a generator function that returns your MyConstructor. If you create a function called createMyConstructor, then call it immediately, it will return what you want.
var myNamespace = {
a: 3,
b: function (msg) {
alert(this.a);
},
MyConstructor: (function createMyConstructor() {
var cons = function(xx) {
this.x = xx;
};
cons.prototype = {
a: 1,
b: function() {
return true;
};
return cons;
})()
};
精彩评论