JavaScript prototype and __proto__

Source: Internet
Author: User

< script type = "Text/javascript" >
function person () {
Property
This.head = 1;
This.eye = 2;
Method
This.eat = function () {
Alert ("Eat something");
}

}

Methods for extending classes

Person.prototype.run =function () {

Alert ("I'll Run");

}


function Programmer () {
this.coding = function () {
Alert ("I'll Knock the Code");
}
}

Methods for extending classes

Programmer.prototype.run =function () {

Alert ("I'll Go");

}

Inherited
Programmer.prototype = new Person ();

Add a new method to a subclass
Programmer.prototype.debug = function () {
Alert ("I'll Debug the Code");
}

Invoke Example
function docoding () {
var a = new Programmer ();
alert (A.head); Calling the properties of the parent class
A.eat (); Calling a method of the parent class
A.debug (); Methods for calling Subclasses
}

Docoding ();

var p = new Programmer ();

P.run ();

Execute Person.run () why? Mainly see p.__proto__ point is the object of the __proto__.

p.__proto__
person {head:1, eye:2}

From here we can see that the p.__proto__ point is the person object, all will execute Person.run ()

P.__proto__.run ();

I can run.


</script>

Summarize:

1. Prototype is a property of a class (function object). Function: Extend the properties and methods of a class, to inherit other objects (properties and methods)

2. __proto__ is a property of the object inside (var obj = function person () {}). Function: Prototype that points to the corresponding class of the object

JavaScript prototype and __proto__

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.