Javascript Prototype Tip

Started studying prototype in Javascript. I want to know that this code can be changed (improved / done differently). Sorry for my english (shell prompt). This is just for me. Any advice would be welcome. There is no point in the functionality of the code. Its basic structure (inheritance, object interaction). Am I moving correctly?

the first

function extend(Child, Parent) {
    var F = function() { }
    F.prototype = Parent.prototype;
    Child.prototype = new F();
    Child.prototype.constructor = Child;
    Child.superclass = Parent.prototype;
}
function Human() {
}
Human.prototype = {
  can_speak: true,
  gender: 'sex',
  say_hello: function() {
    console.log(this.name+' say hello!');
  },
  constructor: function(name,age) {
    this.name = name;
    this.age = age;
  },
  whoami: function() {
    console.log('My name - '+this.name+'. My gender - '+this.gender+'. My age - '+this.age);
  }
}
function Man(name,age) {
  this.gender = 'male';
  this.power = 50;
  this.poke = function() {
    console.log(this.name+' - I`m a man!!!');
  }
  Man.superclass.constructor.call(this, name,age);
}
function Woman(name,age) {
  this.gender = 'female';
  this.power = 30;
  this.give = function() {
    console.log(this.name+' - I`m give to someone(((');
  }
  Woman.superclass.constructor.call(this, name,age);
}
function Boy(name,age, main) {
  Boy.superclass.constructor.call(this, name,age);
  this.main = main;
}
function Oldman(name,age) {
  Oldman.superclass.constructor.call(this, name,age);
}
function Girl(name,age) {
  Girl.superclass.constructor.call(this, name,age);
}
function Oldwoman(name,age) {
  Oldwoman.superclass.constructor.call(this, name,age);
}
extend(Man, Human);
extend(Woman, Human);
extend(Boy, Man);
extend(Oldman, Man);
extend(Girl, Woman);
extend(Oldwoman, Woman);

Stella = new Girl('Stella', 17);
John = new Boy('John', 18, 'trolling');
John.poke();
console.log(John);

      

second

function Man(name) {
  this.name = name;
  this.age = 20;
  this.band_name = '';
  this.band = '';
  console.log(this.name+' was created');
}
Man.prototype = {
  say_hello: function() {
    return 'Hello from '+this.name;
  },
  rename_band: function(new_name) {
    console.log(this.name+' was renamed his band '+this.band_name+' to '+new_name);
    this.band.name = new_name;
    this.band.members.forEach(function(e) {
      e.band_name = new_name;
    });
  },
  rename: function(new_name) {
    console.log(this.name+' was renamed to '+new_name);
    this.name = new_name;
  }
}
function band(name) {
  this.name = name;
  this.members = new Array();
}
band.prototype = {
  add: function() {
    c = arguments.length;
    for(i=0; i<c;i++) {
      arguments[i].band_name = this.name;
      arguments[i].band = this;
      console.log(arguments[i].name+' was invited to '+this.name);
      this.members.push(arguments[i]);
    } 
  },
  change_name: function(new_name) {
    console.log('Band '+this.name+' was renamed to '+new_name);
    this.name = new_name;
    this.members.forEach(function(e) {
      e.band_name = new_name;
    });
  },
  remove_member: function(member_name) {
    c = this.members.length;
    for(i=0; i<c; i++) {   
      n = this.members[i].name;
      if(n == member_name) {
        console.log(n+' was remove from the '+this.name);
        this.members[i].band_name = '';
        this.members[i].band = '';
        this.members.splice(i, 1);
        return;
      }
    }
    console.log(member_name+" wasn't found in "+this.name);
  },
  split: function(reason) {
    c = this.members.length;
    this.members.forEach(function(e) {
      e.band = '';
      e.band_name = '';
    });
    this.members.splice(0, c);
    console.log('Band '+this.name+' was splited. Reason - '+reason);
  },
  print: function() {
    str = 'Band - '+this.name+'. Members:';
    this.members.forEach(function(e) {
      str += e.name+'('+e.age+'); ';
    });
    console.log(str);
  }
}

var Davy = new Man('Davy');
var John = new Man('John');
var Alex = new Man('Alex');
var hardcore = new band('hardcore');
hardcore.add(Davy, John, Alex);
hardcore.change_name('deathcore');
hardcore.print();
hardcore.remove_member('Davy');
John.rename_band('lol');
hardcore.add(Davy);
Davy.rename('Joshua');
hardcore.print();
hardcore.split('bad guitarist');

      

+3


source to share


1 answer


this is a great answer that should help you with a code approach Javascript Duck Typing Example? - try combining small components together to create things rather than model things strictly with inheritance. You will see what I mean in this answer.



good luck deathcore! #deskmosh

+1


source