JavaScript subclasses use Object.getprototypeof to invoke parent-class methods to parse _javascript techniques

Source: Internet
Author: User

Each function has a prototype attribute, called a prototype. Each object also has a prototype, which can be accessed through __proto__ in the Firefox/safari/chrome/opera, and no related interfaces are provided in IE6/7/8.

Copy Code code as follows:

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);
}

Defines the parent class person, the subclass man. New Man object, print out all attributes.

The ECMAScript V5 adds a static Getprototypeof method (Firefox/chrome implemented) to object to obtain the prototype of the objects. It can imitate the super of Java.

Copy Code code as follows:

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 ();//prototype inheritance

Man.prototype.m2 = function () {
Object.getprototypeof (This). METHOD2 ();
}


var mans = new Man ();
MAN.M1 ();
MAN.M2 ();

The M1 method hanging on this in the subclass man calls the method1 on this in the parent class person, and the M2 method on the prototype calls prototype on the parent class METHOD2.

As you can see, the object prototype includes not only the properties on its constructor prototype, but also the properties on this in the constructor. Of course, because of the context in JavaScript, this in the parent class cannot be automatically converted in subclasses and requires some skill to do so.

This is the case in Java

Copy Code code as follows:

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) {
Mans man1 = new Man ("Jack");
MAN1.M1 ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.