2017-09-20 9 views
-1

이것은 제 첫 질문이므로 제대로 처리하지 못할 수 있습니다. 나는 JS와 Node JS를 배우려고 노력하고있다. util.inherits 함수로 고민하고 있습니다.내 생성자가 슈퍼 생성자의 메서드를 상속하지 않는 이유는 무엇입니까?

util.inherits 함수를 사용했지만 Human 생성자의 속성 및 메서드를 Male 인스턴스에서 사용할 수없는 이유를 알 수 없습니다. james instance of Human이 true이면 확실한 것은 james 개체가 Human 메서드에 액세스 할 수 있어야합니다.

이제는 util.inherits 사용이 권장되지 않지만 이해를 돕기 위해 작동하지 않는 이유를 이해하고 싶습니다.

var util = require('util'); 

function Human(){ 
    this.firstName = 'James'; 
    this.secondName = 'Michael'; 
    this.age = 8; 
    this.humanFunction = function(){ 
     console.log('I am a human'); 
    } 
} 

function Male(){ 
    this.gender = 'male'; 
    this.power = 5; 
    this.who = function(){ 
     return 'I am Male'; 
    }; 
} 

util.inherits(Male, Human) 

let james = new Male(); 

console.log(james instanceof Human); //true 
console.log(james.firstName); //undefined 
james.humanFunction(); //james.humanFunction is not a function 
+1

질문 : JavaScript 클래스를 사용하는 대신 '유틸리티'라는 패키지를 사용하는 이유는 무엇입니까? 자동으로이를 수행 할 수 있습니까? –

+2

'Male' 생성자의 첫 줄에'Human.call (this);'을 추가하십시오. – 4castle

+0

@ Mike'Pomax'Kamermans 나는 수업을 알고 있고 이미이 기능을 수행하고 있음을 알고 있습니다. 단지 여기에 나의 이해에 결함이 있다는 것을 묻는 것만으로도 클래스를 사용하여 이것을 할 수는 있지만 실제로 util.inherits를 사용하여 작동하지 않는 이유를 알고 싶습니다. –

답변

0

It 's 2017, Node.js tells you not to use this. 대신, 실제 클래스 표기법 사용 : 그 노드의 일이 현대적인 버전이기 때문에

// our initial class 
class Human { 
    constructor(first, second, age) { 
    this.firstName = first; 
    this.secondName = second; 
    this.age = age; 
    this.gender = 'unspecified' 
    } 

    sayWhat() { 
    console.log(`I am a human, and my gender is ${this.gender}`); 
    } 
} 

// our 'Male' class, a subclass of Human: 
class Male extends Human { 
    constructor(first, second, age) { 
    super(first, second, age) 
    this.gender = 'male'; 
    } 
} 

을 그리고 우리가 같은 코드를 호출하지만, 문자열 템플릿으로 수행

let james = new Male('james', 'michael', 8); 
console.log(`is james a human?: ${james instanceof Human}`); 
console.log(`james's first name is: ${james.firstName}`); 
console.log(`james says: ${james.sayWhat()}`); 
+0

동일한 것을 달성하는 대체 방법이 있지만. 이것은 OP가 대답하려고하는 질문에 대답하지 않습니다. 그는 왜 코드가 작동하지 않는지에 대한 이해를 원했습니다. 단순히 대안을 제공하고 코드가 작동하지 않는 이유를 설명하지 않으면이 질문에 대한 이해를 돕는 잠재력이있는 방문자를 효과적으로 도울 수 없습니다. 그는 상속을 달성하기 위해 2017 년 이전의 방법을 사용하고있는 코드를 발견하게 될 것입니다. 자바 스크립트 길을 계속 이어가고 있다고 가정하면 어떻게 대답할까요? – Eladian

+1

그리고 같은 질문을하는 미래의 방문자는이 패턴을 더 이상 사용하지 않는 것을 배웁니다. 왜냐하면 그것은 나쁜 패턴이므로 더 이상 사용해서는 안됩니다 *. 자바 스크립트를 계속 사용하는 사람이라면 누구나 현대 JS에서 무엇을해야하는지 배우는 것이 더 낫습니다. –

+0

@Eladian 사실입니다. 실제로 코드가 작동하지 않는 이유를 실제로 이해합니다. –

0

Human.call를 추가하십시오 (이); Male() 함수

var util = require ('util');

function Human(){ 
    this.firstName = 'James'; 
    this.secondName = 'Michael'; 
    this.age = 8; 
    this.humanFunction = function(){ 
     console.log('I am a human'); 
    } 
} 

function Male(){ 
    this.gender = 'male'; 
    this.power = 5; 
    this.who = function(){ 
     return 'I am Male'; 
    }; 
    Human.call(this); 
} 

util.inherits(Male, Human) 

let james = new Male(); 

console.log(james instanceof Human); //true 
console.log(james.firstName); //undefined 
james.humanFunction(); //james.humanFunction is not a function