Heads-up when copying by reference
The fact that objects (including functions and arrays) are copied by reference could sometimes lead to results you don't expect.
Let's create two constructor functions and add properties to the prototype of the first one:
> function Papa() {}
>function Wee() {}
>Papa.prototype.name = 'Bear';
>Papa.prototype.owns = ["porridge", "chair", "bed"];Now, let's have Wee inherit from Papa (either extend() or extend2() will do):
>extend2(Wee, Papa);
Using extend2(), the Wee function's prototype inherited the properties of Papa.prototype as its own.
>Wee.prototype.hasOwnProperty('name');
true
>Wee.prototype.hasOwnProperty('owns');
true
The name property is primitive so a new copy of it is created. The property owns is an array object so it's copied by reference:
>Wee.prototype.owns; ["porridge", "chair", "bed"] >Wee.prototype.owns=== Papa.prototype.owns; true
Changing the Wee function's copy of name doesn't affect Papa:
>Wee.prototype.name...