Home > Web Front-end > JS Tutorial > body text

How does a JS subclass use Object.getPrototypeOf to call the parent class?

零到壹度
Release: 2018-03-22 11:46:09
Original
1734 people have browsed it

This time I will show you how to use Object.getPrototypeOf to call the parent class in a JS subclass. The following is a practical case. Let’s follow the editor’s footsteps and take a look.

Each function has a prototype attribute, called the prototype. Each object also has a prototype, Firefox/Safari/Chrome/Opera It can be accessed through __proto__, and there is no relevant interface provided in IE6/7/8.

function Person(){
this.method1 = function(){}
}
Person.prototype.method2 = function(){}
function Man(){}
Man.prototype = new Person();
Man.prototype.m1 = function(){}
Man.prototype.m2 = function(){}
var m = new Man();
for(var a in m.__proto__){
alert(a);
}
Copy after login

defines the parent class Person and the subclass Man. new a Man object and print out all properties.

ECMAScript V5 adds a static getPrototypeOf method to Object (Firefox/Chrome has implemented it ), used to obtain the prototype of the object. It can be used to imitate Java's super.

function Person(){
this.method1 = function(){alert(1)}
}
Person.prototype.method2 = function(){alert(2);}
function Man(){
this.m1 = function(){
Object.getPrototypeOf(this).method1();
}
}
Man.prototype = new Person();//原型继承
Man.prototype.m2 = function(){
Object.getPrototypeOf(this).method2();
}
var man = new Man();
man.m1();
man.m2();
Copy after login


The m1 method hanging on this in the subclass Man calls the method1 hanging on this in the parent class Person, and the m2 method hanging on the prototype calls method2 on the parent class prototype.

It can be seen from the above that the object prototype not only includes the attributes on its constructor prototype, but also includes the attributes on this in the constructor. Of course, due to context reasons in JavaScript, this in the parent class cannot be automatically converted in the subclass, and some skills are required to complete it.

This is how it works in Java

package bao1;
class Person {
    private String name;
    Person(String name) {
      this.name = name;
  }
  public void method1() {
      System.out.println(this.name);
    }
  }
      class Man extends Person{
        Man(String name) {
          super(name);
    }
    public void m1() {
       super.method1();
      }
    }
     public class Test 
     {public static void main(String[] args) {
     Man man1 = new Man("Jack");man1.m1();
    }
  }
Copy after login

The above is the detailed content of How does a JS subclass use Object.getPrototypeOf to call the parent class?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!