I have this JavaScript:
var Type = function(name) {
this.name = name;
};
var t = new Type();
Now I want to add this:
var wrap = function(cls) {
// ... wrap constructor of Type ...
this.extraField = 1;
};
So I can do:
wrap(Type);
var t = new Type();
assertEquals(1, t.extraField);
[EDIT] I'd like an instance property, not a class (static/shared) property.
The code executed in the wrapper function should work as if I had pasted it into the real constructor.
The type of Type
should not change.
update: An updated version here
what you were actually looking for was extending Type into another Class. There are a lot of ways to do that in JavaScript. I'm not really a fan of the
new
and theprototype
methods of building "classes" (I prefer the parasitic inheritance style better), but here's what I got: