JS Constructor, prototype inheritance

Source: Internet
Author: User

constructor that defines the parent type of all fliers
function Flyer (fname,fspeed) {
This.fname=fname;
This.fspeed=fspeed;
}
Flyer.prototype.fly=function () {//fly placed in Flyer.prototype
Console.log (this.fname+ "to" +this.fspeed+ "speed");
}
Define the first flier: Bee, inheriting and extending the parent type Flyer
Define unique constructors, inherit and extend the flyer constructor
function Bee (fname,fspeed,award) {
Flyer.apply (this,arguments);//1. Methods that hijack another object, inheriting the properties of another object
The second parameter is the one I want to pass to the flyer parameter.
This.award=award; Unique properties:
}
Bee.prototype.get=function () {
Console.log ("Get Properties unique to the object:" +this.award);
}
2. Let the prototype object of the subtype inherit the prototype object of the parent type
Object.setprototypeof (Bee.prototype,flyer.prototype); Equivalent to Bee.__proto__=flyer
var bee = new Bee ("Little Bee", "1 Life");
var flyer = New Flyer ("Bee", "1 Life");
Console.log (flyer);//flyer {fname: "Bee", fspeed:40}
Console.log (BEE); Bee {fname: "Little Bee", Fspeed:30, Award: "1 Life"}
Bee.fly ();
Bee.get ();

Js Apply method Detailed: http://www.360doc.com/content/13/0807/19/13328522_305431575.shtml

Use of the Apply method in JS: http://www.cnblogs.com/delin/archive/2010/06/17/1759695.html

JS implementation of several ways to inherit: http://www.cnblogs.com/humin/p/4556820.html

JS Constructor, prototype inheritance

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.