So, I know how to apply the prototype property to modify constructors that will effectively affect instances. Theoretically, the prototype should work on a normal function as well (though not necessarily have an effect if it's not a prototype of a certain instance). So I tried the code (createNewFunction is a normal function that's not a constructor)(full code is at the end)
createNewPerson.prototype.xx = (function() { //alerts hello
alert("hello");
})();
I'm wondering what this actually does as to how xx is appended to the function. Is it stored as variable xx that is equal to the anon function? Or is stored as this.xx = function? How will this compare to storing the following code:
createNewPerson.xx = (function() { //alerts hello
alert("hello");
})();
This also makes me wonder as to how this this line will be stored(As in if it will be stored as var mm = 3, or it won't append at all?):
createNewPerson.mm = 3;
Full code for reference:
function createNewPerson(name) {
var obj = {};
obj.name = name;
obj.greeting = function() {
alert('Hi! I\'m ' + this.name + '.');
};
return obj;
}
createNewPerson.mm = 3; //does nothing when i tested it
createNewPerson.xx = (function() { //alerts hello
alert("hello");
})();
createNewPerson.prototype.xx = (function() { //alerts hello
alert("hello");
})();
var salva = createNewPerson('Salva');