이 기사에서는 JS의 프로토 타입 사용에 대해 설명합니다. 참조를 위해 공유하십시오. 특정 분석은 다음과 같습니다.
JS의 Phototype는 JS의 어려운 부분입니다.
이 기사는 다음과 같은 지식을 기반으로합니다.
1 프로토 타입 설계 모드
.NET에서 Clone ()을 사용하여 프로토 타입 방법을 구현할 수 있습니다.
프로토 타입 방법의 주요 아이디어는 이제 클래스 A가 있다는 것입니다. 클래스 B를 만들고 싶습니다.이 클래스는 A에 의해 프로토 타입으로 확장 될 수 있습니다. 우리는 B의 프로토 타입 A라고 부릅니다.
2 JavaScript 방법은 세 가지 범주로 나눌 수 있습니다.
클래스 A 방법
B 객체 방법
C 프로토 타입 방법
예는 다음과 같습니다.
기능인 (이름) {this.name = 이름; // 객체 메소드 this.introading = function () {alert ( "내 이름은"+this.name); }} // class method people.run = function () {alert ( "I can run");} // 프로토 타입 메소드 people.prototype.introduceChinese = function ( "내 이름은"+this.name);} // test var p1 = new People ( "Windking"); p1.introading (); people.run (); p1.introducechinese ();3 obj1.func.call (OBJ) 메소드
그것은 OBJ를 OBJ1로 간주하고 func 방법을 호출하는 것을 의미합니다.
좋아, 다음 문제는 하나씩 해결됩니다.
프로토 타입은 무엇을 의미합니까?
JavaScript의 각 객체에는 프로토 타입 속성이 있습니다. JavaScript에서 객체의 프로토 타입 속성에 대한 설명은 다음과 같습니다. 객체 유형 프로토 타입에 대한 참조를 반환하십시오.
A. 프로토 타입 = 새로운 B ();
프로토 타입을 이해하는 것이 상속과 혼동되어서는 안됩니다. A의 프로토 타입은 B의 인스턴스입니다. A는 B의 모든 방법과 특성을 복제했다는 것을 이해할 수 있습니다. A는 B의 방법과 특성을 사용할 수 있습니다. 여기서 강조되는 것은 상속보다는 복제입니다. A의 프로토 타입은 B의 인스턴스이며 B의 프로토 타입은 A의 인스턴스이기도합니다.
실험의 예를 살펴 보겠습니다.
함수 baseClass () {this.showmsg = function () {alert ( "baseClass :: showmsg"); }} function extendClass () {} extendClass.prototype = new BaseClass (); var instance = new extendClass (); instance.showmsg (); // show baseclass :: showmsg먼저 Baseclass 클래스를 정의한 다음 ExtentClass를 정의하려고하지만 ExtendClass의 프로토 타입에 ShowMSG 객체 방법이 포함되어 있으므로 BaseClass 인스턴스를 사용하려고합니다.
extendClass.prototype = new BaseClass ()는 다음과 같이 읽을 수 있습니다. ExtendClass는 프로토 타입 클로닝으로베이스 클래스 인스턴스를 사용하여 작성됩니다.
그렇다면 ExtendClass 자체가 BaseClass 메소드와 동일한 이름의 메소드가 포함되어 있으면 어떻게 될까요?
다음은 확장 실험 2입니다.
함수 baseClass () {this.showmsg = function () {alert ( "baseClass :: showmsg"); }} 함수 extendClass () {this.showmsg = function () {alert ( "extendClass :: showmsg"); }} extendclass.prototype = new BaseClass (); var instance = new ExtendClass (); instance.showmsg (); // show extendClass :: showmsg실험적 증거 : 함수가 실행될 때 먼저 온톨로지의 함수로 이동하여 검색합니다. 발견되면 실행됩니다. 발견되지 않으면 기능을 검색하기 위해 프로토 타입으로 이동합니다. 또는 프로토 타입은 같은 이름의 기능을 복제하지 않을 것임을 이해할 수 있습니다.
그러면 또 다른 새로운 질문이 있습니다.
ExtendClass 인스턴스 인스턴스를 사용하여 BaseClass의 객체 메소드 ShowMSG를 호출하려면 어떻게해야합니까?
답은 전화를 사용하는 것입니다.
ExtendClass.prototype = new BaseClass (); var instance = new ExtendClass (); var baseInstance = new BaseClass (); BaseInstance.showmsg.call (인스턴스); // show baseclass :: showmsg
여기서 baseinstance.showmsg.call (인스턴스); "Call Instance as BaseInstance로 객체 메소드 ShowMSG 호출"으로 읽으십시오.
좋아, 누군가 여기서 baseclass.showmsg.call (인스턴스);
이것이 객체 방법과 클래스 방법의 차이입니다. 우리가 전화하고 싶은 것은 Baseclass의 객체 방법입니다.
마지막으로, 다음 코드를 명확하게 이해하면이 기사가 이미 이해된다.
<script type = "text/javaScript"> 함수 baseClass () {this.showmsg = function () {alert ( "baseClass :: showmsg"); } this.baseshowmsg = function () {alert ( "baseclass :: baseshowmsg"); }} baseClass.showmsg = function () {alert ( "baseClass :: showmsg static");} function extendClass () {this.showmsg = function () {alert ( "extendClass :: showmsg"); }} extendClass.showmsg = function () {alert ( "extendClass :: showmsg static")} extendClass.prototype = new BaseClass (); var instance = new ExtendClass (); instance.showmsg (); // show extendClass :: showmsginstance.baseShowmsg (); // show baseclass :: Baseshmsginstance.showmsg (); // show extendClass :: showmsgbaseclass.showmsg.call (인스턴스); // show baseclass :: showmsg staticvar baseinstance = new BaseClass (); baseInstance.showmsg.call (인스턴스); // show baseclass :: showmsg </script>이 기사가 모든 사람의 JavaScript 프로그래밍에 도움이되기를 바랍니다.