Prototype extension of JavaScript objects (JS object-oriented inheritance)

Source: Internet
Author: User

<script type= "Text/javascript" >
function person (name, age) {
This._name = name;
This._age = age;
var _self = this;
This.say = function () {
Console.log (This._name + '-' + this._age);
SetTimeout (function () {
Console.log (_self._name);
}, 2000);
}

}
Person.prototype.foot = function () {
Console.log ("I foot" + this._age);
}

var xiaoming = new Person (' xiaoming ', 18);
Xiaoming.foot ();
Console.log (Json.stringify (xiaoming));
</script>

The meaning of prototype in the software world is the original form of the Table object. The prototype in JavaScript.

All objects of the function type of JS have a prototype property. This prototype property itself is an object type, so you can add arbitrary properties and methods to the prototype object.

Inside JavaScript, the object's properties and method traceability mechanisms are implemented through the so-called prototype chain. When an object is constructed with the new operator, the prototype object of the constructor is also assigned to the newly created object as a prototype object built into the object. The prototype object built into the object should be invisible to the outside, although some browsers (such as Firefox) can let us access this built-in prototype object, but it is not recommended.
This is the "inheritance"of JavaScript!
With inheritance we can extend the existing objects (including JS built-in objects, custom objects) conveniently!

Prototype extension of JavaScript objects (JS object-oriented inheritance)

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.