먼저 JS 클래스의 상속을 살펴 보겠습니다
<! docType html> <html> <head> <meta charset = "utf-8"> <title> js 클래스의 상속 </title> </head> <body>/ *- 클래식 상속- */<script type = "text/javascript"> // 슈퍼 클래스 var 동물 = 함수 선언 (이름) {name = name =; } // methys.prototype.eat = function () {console.log (this.name + "eat")를 추가합니다. }; // 인스턴스이 Supervar a = 새로운 동물 ( "동물"); // 생성자 개체 클래스 var cat = function (이름, 섹스) {//이 클래스는 슈퍼 클래스 동물의 생성자를 호출하고 매개 변수 이름을 ital.call (this, name)에 전달해야합니다. this.sex = 섹스; } //이 서브 클래스의 프로토 타입 객체는 슈퍼 클래스 cat.prototype = new Animal ()의 인스턴스와 같습니다. // 서브 클래스의 프로토 타입 객체는 슈퍼 클래스의 인스턴스와 같기 때문에 메소드 프로토 타입은 슈퍼 클래스 생성자 console.log (cat.prototype.constructor)와 동일합니다. // 이것은 동물 슈퍼 클래스에 대한 참조이므로 Cat.Prototype.constructor = cat; Console.log (cat.prototype.constructor); // 서브 클래스 자체는 getSex 메소드 cat.prototype.getSex = function () {return this.sex; } //이 서브 클래스 var _m = 새로운 고양이 ( 'cat', 'male'); // OWN Method Console.log (_m.getSex ()); // male // 슈퍼 클래스 메소드 console.log (_m.eat ())를 상속합니다. // cat </script> </body> </html>JS 프로토 타입 상속을 살펴 보겠습니다
<! docType html> <html> <head> <meta charset = "utf-8"> <title> js 프로토 타입 상속 </title> </head> <body> <!-프로토 타입 상속-> <script type = "text/javascript"> // clone () 함수를 만드는 데 사용됩니다. {}; //이 문장은 프로토 타입 상속의 핵심입니다. 함수의 프로토 타입 객체는 객체 literal_f.prototype = obj입니다. 새로운 _f를 반환합니다. } // 객체 문자 그대로 첫 번째 var 동물을 선언합니다. }} // 사람의 서브 클래스를 정의 할 필요가 없으며 복제 var cat = clone (동물)을 수행합니다. // 사람이 제공 한 기본값을 직접 얻거나 속성 및 메소드 Console.log (cat.eat ())를 추가하거나 수정할 수 있습니다. cat.somthing = '오렌지'; console.log (cat.eat ()); // 서브 클래스를 선언하고 복제 var var = clone (cat); </script> </body> </html>우리는 실험 할 수 있습니다. JS 클래스 어린이의 상속이 .constructor == 아버지는 참으로 돌아 오는 반면, 프로토 타입 어린이의 상속.
JS 프로토 타입 상속 및 클래스 상속에 대한 위의 간단한 분석은 내가 공유하는 모든 내용입니다. 나는 당신이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.