3

Javascriptでプロトタイプの勉強を始めました。そのコードを変更(改善\別の方法で)することが可能であることを知りたいです。私の英語(シェルプロンプト)でごめんなさい。それは私だけです。アドバイスを歓迎します。コードの意味のある機能はありません。その主な構造(継承、オブジェクトの相互作用)。私は正しい方向に動いていますか?

最初

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);

2番目

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');
4

1 に答える 1

1

これは、Javascript Duck Typing のコード例へのアプローチに役立つ優れたスタック オーバーフローの回答です。- 物事を厳密に継承でモデル化するのではなく、小さなコンポーネントを組み合わせて物事を作成するようにしてください。その答えで私が何を意味するかがわかります。

頑張れデスコア!#デスクモッシュ

于 2013-02-11T21:43:57.203 に答える