JavaScript's prototype

Source: Internet
Author: User

A function is also an object, and all functions have a prototype property that is created and initialized when the function is defined. Prototype is initially an object that has an constructor property that refers back to the function object that owns the prototype property.


Each object contains a shallow copy of the properties of the prototype Property object of its constructor object

Example: var o = new O (); O.PROTOTYPE.P = 5; O.P is a shallow copy of O.PROTOTYPE.P.

That is: Object o contains a shallow copy of the property P of the prototype Property object whose constructor object o

Simplification: The actual is to replace the prototype with O, so by the prototype call function, is actually o call, so can be used this

Analog prototype//function __sys__initfunctionobject (funcobj) {//1. Function objects have a prototype property initialized to the object when the functions are defined Funcobj._ Prototype = new Object (); funcobj._prototype.constructor = Funcobj;} function __sys__copyprototype (funcobj, obj) {for (var name in funcobj._prototype) {//3) when the property of the prototype object changes. Shallow copy obj[ Name] = Funcobj._prototype[name];}} __sys__initfunctionobject (SXX); Invisible function Sxx () {__sys__copyprototype (Sxx, this);//Invisible}sxx._prototype.id = 1; 2. Add an attribute to the prototype Property object var s = new Sxx (); Console.log (s);

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/88/68/wKiom1f1yCbRdF31AABFfxhkqCk480.png "title=" capture. PNG "alt=" Wkiom1f1ycbrdf31aabffxhkqck480.png "/>

This article is from the "doerthous" blog, make sure to keep this source http://doerthous.blog.51cto.com/11762533/1858915

JavaScript's prototype

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.