What exactly is prototype in JavaScript?

Source: Internet
Author: User

JavaScript is also an object-oriented language, but it is a prototype-based prototype language rather than a class-based language. In JavaScript, classes and objects don't look much different.

Typically, this creates an object:

function Person (name) {    thisfunction() {        alert (this. Name);    }     this. Name = name;} var New person ("Dan");p. Sayhi ();


Above, use the New keyword to create an object instance from an object (a function is also a special object).

In a class-based language, a property or field is usually defined beforehand in a class, but in JavaScript, you can add fields to the class after you create the object.

function animal () {} var New  = "green";

Above, color This field belongs only to the current cat instance.

For post-add fields, what if you want to have all instances of animal?

--Using prototype

function animal () {}animal.prototype.color= "green"; var New animal (); var New animal (); Console.log (Cat.color); // Greenconsole.log (dog.color); // Green


Not only can you add fields through prototype, you can also add methods.

function animal () {}animal.prototype.color= "green"; var New animal (); var New animal (); Console.log (Cat.color); // Greenconsole.log (dog.color); // Green  = Funciton () {    console.log ("Run");} Dog.run ();

Originally through the prototype property, the object's behavior can also be changed after the object is created.

For example, you can add a method to this particular object for an array.

function (elem) {    varthis. indexof (elem);     if (Index >= 0) {        this. Splice (index, 1);    }}  var arr = [1, 2, 3]    ; Arr.remove (2);

In addition to defining properties or methods for an object through prototype, you can also define properties or methods of the class through the object's constructor.

function animal () {    this. Color = "green";       This function () {        console.log ("Run");}    } var New animal (); Mouse.run ();

The above practice also allows all animal instances to share all fields and methods. There is also a benefit that you can use the local variables of the class in the constructor.

function animal () {    varfalse;     this. Color = "green";     this. Run = Funciton () {        if(!  Runalreadh) {            console.log ("Start Running");         Else {            Console.log ("already Running")     }}}

In fact, a more practical approach is to define the field and behavior of a class through prototype by combining the constructor function.

functionanimal () {varRunalready =false;  This. Run =function(){        if(!Runalready) {Console.log (' I am running '); } Else{Console.log ("I am already running"); }}}animal.prototype.color= ' '; Animal.prototype.hide=Funciton () {Console.log ("");}varHorse =Newanimal (); Horse.run (); Horse.hide ();

Prototype allows us to alter the behavior of objects or classes after the object is created, and the fields or methods that are added through the prototype property are shared across all object instances.

What exactly is prototype in JavaScript?

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.