JavaScript prototype in the use of detailed knowledge _ basics

Source: Internet
Author: User
Tags inheritance

First look at the following code:

function Machine (Ecode, horsepower) {
  this.ecode = Ecode;
  This.horsepower = horsepower;
}

function ShowMe () {
  alert (this.name + "" + This.ecode + "" + This.horsepower);
}

var machine = new Machine ("Code1");
Machine.name = "Machine1";
Machine.showme = ShowMe;
Machine.showme ();

Such a piece of code, although the establishment of a machine object, through the MACHINE.SHOWME = ShowMe, so that the SHOWME function into a machine object machine method.

However, just look at the above two functions (one is the constructor of the object, one is the normal method) has no relation, such code, not so "elegant", therefore, there is prototype.

Machine
function Machine (ecode, horsepower) {
  this.ecode = Ecode;
  This.horsepower = horsepower;
}
Pay special attention to this sentence, Machine.prototype is initialized at the first time,
//When the Machine is invoked as a constructor, the engine value is not changed
Machine.prototype.engine = This.ecode + "" + This.horsepower;
Machine.prototype.showme = function () {alert (THIS.name + "" + This.ecode + "" + This.horsepower);}

Using the above code, all the machine objects have a ShowMe method.

But pay special attention to:

Prototype is only faced with instances, not with class objects. (JS, the class itself is an object) so Machine.showme (), will report the error, because machine this object is not ShowMe method.

So, how do we use the ShowMe method? A new machine instance must be created, and only the machine instance will have this method.

var machine = new Machine ("Code1");
Machine.name = "Machine1";
Machine.showme (); Output Machine1 code1 15.

With prototype, we are more likely to implement inheritance relationships. For example, I now write a car class, need to inherit the current machine class, just write the following code can be:

Cars
Function car (name, Ecode, horsepower) {
  this.name = name;
  Invokes the constructor of the parent class so that the object of the car has ecode, horsepower property
  Machine.call (this, ecode, horsepower);
The car's prototype points to Machine so that the car object has any properties and methods for the Machine prototype, such as showme
car.prototype = new Machine ();

Here's how to invoke the parent constructor, and how to get the prototype of the parent class, and the annotations are clear, not to be discussed.

Then, we can create a new object to test:

The object that creates a new class car.
var Xiali = new Car ("Xiali", "AAA",);
alert (xiali.engine);
Xiali.showme ();

Above, is the basic application of prototype, but also is the most important application of prototype.

Master of the use of prototype, in the future process, for the construction and inheritance of objects, there will be a deeper understanding.

See more JavaScript syntax, you can focus on: JavaScript reference tutorial, JavaScript Code style guide, and also hope that many people support the cloud-Habitat community.

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.