I know that if I add a function to an constructors prototype that it will then be available to every object that uses the constructor. And I know that if you add a function as an (object literal I think it's called?) that you would have to access it as a property of the constructor itself. (Difference between adding function to prototype and object literal in javascript).
But what if you declare the new function as 'this.xxx()' within the constructor. How is that any different from declaring a property on the prototype?
My guess is that declaring the function object on the prototype would result in delegation (and only one copy of that function would exist). Vs with 'this', every constructed object would have its own copy of the function.
Is that correct?
function A () {
}
A.prototype.printNum = function () {
console.log('hi');
}
x = new A ();
x.printNum(); // hi
function B () {
this.printNum = function () {
console.log('hi');
}
}
y = new B ();
y.printNum(); // hi