Stevanov, Patterns, Classical 1

Parent, child etc.

by BrownieBoy

HTML

<input type="button" onclick="doAction();" value="doAction()"><br>
<br>
<br>

<input name="TestField" value="" id="TestField"></form>

JavaScript

/*
// No good because a Parent's property will "shine through" to the
// Child if the Child has no equivalent of that property.
function inherit(C, P) {
    C.prototype = new P();
}
*/

function inherit(C, P) {
    var F = function () {};
    F.prototype = P.prototype;
    C.prototype = new F();
}

function Parent(name) {
    this.name = name || 'Adam';
}

Parent.prototype.say = function() {
    return this.name;
};

function Child(name) {
    Parent.apply(this, arguments);    // Call parent constructor
}

inherit(Child, Parent);

function doAction() {
    var kid = new Child("Seth");
    console.log(kid.say());
    delete kid.name;
    console.log(kid.say());    
}