Skip to content

Instantly share code, notes, and snippets.

@ilearnjavascript
Created March 27, 2019 23:40
Show Gist options
  • Save ilearnjavascript/ed25ff623e3d4c7bf0e31805812ffd6e to your computer and use it in GitHub Desktop.
Save ilearnjavascript/ed25ff623e3d4c7bf0e31805812ffd6e to your computer and use it in GitHub Desktop.
es6 - classes - 10.js
var es5_Person = function (firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
var john = new es5_Person('John', 'Doe');
es5_Person.prototype.greet = function () {
console.log('Hello I am ' + this.firstname + ' ' + this.lastname);
}
var es5_Soldier = function (firstname, lastname, weapon) {
// call person object with current this context
es5_Person.call(this, firstname, lastname);
this.weapon = weapon;
}
// The es5_Soldier.prototype creates a new object which has
// the es5_person.prototype as its __proto__
es5_Soldier.prototype = Object.create(es5_Person.prototype);
// add new method to all soldier instances
es5_Soldier.prototype.shoot = function () {
console.log('Peng Peng with ' + this.weapon);
}
var jackTheSoldier = new es5_Soldier('Jack', 'The Soldier', 'AK47');
jackTheSoldier.greet(); // outputs: Hello my name is Jack The Soldier
jackTheSoldier.shoot(); // outputs: Peng Peng with AK47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment