JS class-Type Inheritance and prototype-type inheritance detailed _javascript skills

Source: Internet
Author: User
Tags inheritance

The example of this article for everyone to share the JS class-type inheritance and prototype inheritance related code for your reference, the specific contents are as follows

1.js Class-Type inheritance

//--Class inheritance--*///First declare a superclass function person (name) {this.name = name}///Add method to the prototype object for this superclass getName Person.prototype.getNa me = function () {return this.name}//Instantiate this super var a = new person (' Darren1 ') Console.log (A.getname ());//darren1//re-declare Class F Unction programmer (name, sex) {//The constructor of the superclass person is invoked, and the parameter name is passed to it Person.call (this, name); this.sex = sex =//The prototype object of this subclass
is equal to the instance of the superclass Programmer.prototype = new Person (); Because the subclass's prototype object equals an instance of the superclass, Prototype.constructor This method is also equal to the superclass constructor, and you can test it yourself, without this step, Console.log (
Programmer.prototype.constructor This is a reference to the person superclass, so you have to assign a new value to yourself Console.log (Programmer.prototype.constructor);
/*function person (name) {this.name = name;} */Programmer.prototype.constructor = programmer;

Console.log (Programmer.prototype.constructor); 
/*function programmer (name, sex) {Person.call (this, name); this.sex = sex;} The//subclass itself adds the Getsex method Programmer.prototype.getSex = function () {return this.sex}//Instantiate this subclass var _m = new Programmer (' Dar
Ren2 ', ' Male '); Its own method Console.log (_m.getsex ());Male//Inheritance Superclass method Console.log (_m.getname ());//darren2
 

2.js prototype inheritance

/*--Prototype Inheritance--*/
//clone () function is used to create a new class Person object
var clone = function (obj) {
4
var _f = function () {};
This sentence is the core of archetypal inheritance, the prototype object of the function is object literal
_f.prototype = obj;
return new _f;
}
First declare an object literal
var person = {
name: ' Darren ',
getname:function () {return
this.name
}
}
//You do not need to define a subclass of person, as long as you perform a clone,
var programmer = Clone (person);
You can directly obtain the default values provided by person, or you can add or modify properties and Method
alert (Programmer.getname ())
programmer.name = ' Darren2 '
alert (Programmer.getname ())

Declare subclasses, execute a clone then
var Someone = Clone (programmer);

The above is the entire content of this article, I hope to learn JavaScript program to help you.

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.