2013-02-10 2 views
3

Javascript에서 프로토 타입을 연구하기 시작했습니다. 나는 그 코드를 (다르게 \ done 다르게) 변경하는 것이 가능하다는 것을 알고 싶다. 내 영어 (셸 프롬프트) 죄송합니다. 그게 나를위한 것입니다. 나는 어떤 충고도 환영 할 것이다. 코드의 의미 기능이 없습니다. 주요 구조 (상속, 객체의 상호 작용). 나는 올바른 방향으로 움직이고 있는가?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); 

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

이 코드 Example of Javascript Duck Typing?에 대한 접근을 도와해야 좋은 스택 오버플로 대답이다 - 오히려 일을 만들어 함께 작은 구성 요소를 결합하려고 엄격하게 상속을 모델로하는 것보다. 그 대답에서 내가 무엇을 의미하는지 보게 될 것이다.

행운을 빌어 요! #deskmosh