3

开始研究 Javascript 中的原型。我想知道是否可以更改(改进\以不同的方式)该代码。对不起我的英语(shell提示符)。那只是为了我。我会欢迎任何建议。没有意义的代码功能。它的主要结构(继承,对象的交互)。我正在以正确的方式前进?

第一的

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

第二

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 的代码示例?- 尝试将小组件组合在一起来创建事物,而不是严格使用继承来建模事物。你会明白我在那个答案中的意思。

祝你好运死核!#deskmosh

于 2013-02-11T21:43:57.203 回答