Javascript原型建议

时间:2013-02-10 17:52:37

标签: javascript prototype

开始在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');

1 个答案:

答案 0 :(得分:1)

这是一个很好的堆栈溢出答案,它可以帮助您处理代码Example of Javascript Duck Typing?的方法 - 尝试将小组件组合在一起来创建事物,而不是严格地使用继承对事物进行建模。你会在那个答案中看到我的意思。

祝你好运! #deskmosh