It sounds like you're trying to modify the behavior of constructor at runtime. You've said:
I would like to modify the code of constructor using dot notation (if its possible in this case), e.g. I would like to add more properties to my constructor. If the code was like this:
var constructor = function(name, surname, town){
var person = { Name: name, Surname: species, Town: town };
return person;
};
...then I guess I could modify my constructor like this: {constructor.person.phoneNumber = 554457; but in my original code constructor has no var inside so I cant taget obj
As @deceze pointed out, no, you couldn't do that even if constructor had a variable in it; that variable is entirely private to the function, it's not exposed as a property of constructor.
The only way1 to modify constructor's behavior at runtime is to wrap it in a new function, like this:
(function() {
var original = constructor;
constructor = function() {
var obj = original.apply(this, arguments);
obj.phoneNumber = 554457;
return obj;
};
})();
That calls the original version with all of the arguments it receives (and the same this), then adds the additional property, and returns it.
1 Okay, technically, you could decompile it with toString (on browsers that support it; support is now required as of ES5, so it's pretty good), use text manipulation to change its contents, then use new Function to turn it back into a function and assign the result to constructor, but A) It's a really bad idea, and B) The function would lose its context, and so if it relied on closing over any variables that aren't globals, it would stop working (hence [A]).
constructor, or by doing something at runtime?speciesis not passed to the function. And yes, please clarify your question. Show us what do you want to achive, how you want to use the sing dot/bracket notationvar personinside the constructor, you couldn't doconstructor.person...either. Why do you want to do this on the constructor, when it seems pretty clear that you should be adding your phone number to the instance instead?!new.