Object.create()
const elfFcnStore = {
attack(){
return "attack with " + this.weapon;
}
}
function createElf(name,weapon){
let newElf = Object.create(elfFcnStore);
newElf.name = name;
newElf.weapon = weapon;
return newElf;
}
Constructor Functions
function Charactor0(name, weapon) {
this.name = name;
this.weapon = weapon;
}
Charactor0.prototype.attack = function () {
return "attack with " + this.weapon;
};
Charactor0.prototype.build = function () {
const self = this;
function building(){
return self.name + 'builds a house';
}
return building();
};
const peter = new Charactor0("peter", "peterweapon");
console.log(peter.attack());
ES6 Class
class Charactor {
constructor(name, weapon) {
this.name = name;
this.weapon = weapon;
}
attack() {
return "attack with " + this.weapon;
}
}
class Elf extends Charactor {
constructor(name, weapon, type) {
super(name, weapon);
this.type = type;
}
}
class Orge extends Charactor {
constructor(name, weapon, color) {
super(name, weapon);
this.color = color;
}
makeFort() {
return "a fort has been made";
}
}
const fiona = new Elf("Fiona", "ninja stars", "house");
const dolby = new Orge("Dolby", "cloth", "green");
console.log(Elf.__proto__ === Charactor);
console.log(fiona.__proto__ === Elf.prototype);
console.log(dolby.__proto__ === Orge.prototype);
console.log(Orge.prototype.__proto__ === Charactor.prototype);