JS: Good parts - Ch.5 - Inheritance

Prototypial

by Denise Nepraunig

JavaScript

/* 
All texts and infos I have taken from:
'JavaScript: The Good Parts' by Douglas Crockford
*/

// Inheritance - Functional
// the reason to do it this way is, 
// so that we get privacy.


var mammal = function (spec) {
    var that = {};
    that.get_name = function () {
        return spec.name;
    };
    that.says = function () {
        return spec.saying || '';
    };
    
    return that;
};

var myMammal = mammal({name: 'Herb the mammal'});
console.log("myMammal:", myMammal);
console.log("myMammal.get_name:", myMammal.get_name());

var cat = function (spec) {
    spec.saying = spec.saying || 'meow';
    var that = mammal(spec);
    that.purr = function(n) {
        var s = 'purr purr purr';
        return s;
    };
    that.get_name = function () {
        return 'Meow meow ' + spec.name + '!';
    };
    return that;
};

var myCat = cat( {name: 'Henrietta'} );
console.log("myCat:", myCat);
console.log("myCat.get_name:", myCat.get_name());