Originally posted this here, and it was helpfully suggested to post in this forum.
In the past couple of years I've returned part time to programming after a 15 year gap. I was C/UNIX. So, I've picked up PHP, Java and C++ ok, but have struggled with JavaScript.
Finally I think I've found a way to 'create' classes that can inherit and wondered if anyone would care to comment. Here is an example:
<!doctype html>
<head>
<title>Basic</title>
</head>
<body>
<div id="d1"></div>
<script type="text/javascript">
function Base( options ) {
var that = this;
options = options || {};
Object.keys( options ).forEach( function( item ) {
that[item] = options[item];
});
}
function Creature( options ) {
this.legs = 4;
Base.call( this, options );
console.log("New creature");
}
Creature.prototype.showNumberOfLegs = function() {
console.log( "Number legs " + this.legs );
};
function Mammal( options ) {
this.fur = true;
Creature.call( this, options );
console.log("New mammal");
}
Mammal.prototype = Object.create( Creature.prototype );
Mammal.prototype.showFur = function() {
console.log( "Fur " + this.fur );
};
var c = new Creature();
c.showNumberOfLegs();
var m = new Mammal({ legs: 6, fur: false });
m.showNumberOfLegs();
m.showFur();
</script>
</body>
</html>