이 기사는 참조를위한 JS 유형 상속 및 프로토 타입 상속과 관련된 코드를 공유합니다. 특정 내용은 다음과 같습니다
1.JS 클래스 상속
/ *- 클래식 상속- * // 먼저 슈퍼 클래스 함수 직원 (이름) {this.name = name;} //이 슈퍼 클래스 getName person.prototype.getName = function ()의 프로토 타입 객체에 메소드를 추가합니다. 클래스 기능 프로그래머 (이름, 섹스) {//이 클래스는 슈퍼 클래스 사람의 생성자를 호출하고 매개 변수 이름을 person.call (this, name)에게 전달해야합니다. 이 서브 클래스의 프로토 타입 객체는 슈퍼 클래스 프로그래머의 인스턴스와 동일합니다. prototype = new person (); // 서브 클래스의 프로토 타입 객체는 슈퍼 클래스의 인스턴스와 같기 때문에 방법 프로토 타입과 동일합니다. 직접 테스트 할 수 있습니다. 이 단계를 사용할 수없는 경우 console.log (programmer.prototype.constructor는 SuperClass의 사람을 참조하기 때문에 자신의 Console.log (programmer.prototyp.constructor);/ *function person (name) {this.name = name;} */programmer.protoctor =를 재 할당해야합니다. 프로그래머; console.log (programmer.prototyp.constructor);/ *함수 프로그래머 (이름, 섹스) {person.call (this, name); subclass 자체는 getsex method.protmer.protopty (return this.get.sex; 'male'); // 자체 메소드 console.log (_m.getSex ()); // male // 슈퍼 클래스 메소드 console.log (_m.getName ()); // darren2를 상속합니다2.JS 프로토 타입 상속
/ *- 프로토 타입 상속- * /// clone () 함수는 새로운 클래스 사람 객체 var clone = function (obj) {4var _f = function () {}; // 프로토 타입 상속의 핵심입니다. 함수의 프로토 타입 객체는 객체 literal_f.prototype = obj; return new _f;} // 객체 문자 그럴을 먼저 선언합니다. var person = {name : 'darren', getName : function () {return this.name;}} // 사람의 서브 클래스를 정의 할 필요가 없습니다. 복제 var 프로그래머 = 클론 (person); // 사람이 제공 한 기본값을 직접 얻거나 어울리는 기본 값을 얻거나 수정할 수 있습니다 (programmer.getname ()). 'darren2'Alert (programmer.getName ()) // 서브 클래스를 선언하고 일단 복제를 수행하고 복제를 수행하십시오.위의 내용은이 기사에 관한 모든 것입니다. 모든 사람들이 JavaScript 프로그래밍을 배우는 것이 도움이되기를 바랍니다.