Javascript: Prototype pattern class inheritance

Source: Internet
Author: User

Prototype mode each function (not exactly a class, an object.) has a prototype property, which is a pointer to an object. Using the prototype object'sBenefitsIs that you can have all object instances share the properties and methods that it contains. 1. Prototype objects (1) When a new function is created, a prototype property is created for the function, which points to the prototype object of the function. (2) By default, all prototype objects automatically get a constructor (constructor) property that contains a pointer to thefunction where the prototype property is locatedThe pointer. (3) The inner part of the instance contains a pointer, called [[Prototype]]. But the pointer to the scriptcompletely invisible (some browsers support a __proto__ to access)。 2. Code read attribute Ordersearch the object instance itself first, without continuing to search for the prototype object pointed to by the pointer. Example: function person () {} person.prototype.name= "Javascript";  var p1 = new Person ();    var p2 = new Person ();  P1.name= "Lufeng";  alert (p1.name);//"Lufeng" alert (p2.name);//"Javascript" uses the delete operator to completely remove instance properties. isPrototypeOf (): Determines if there is a prototype relationship between Objects hasOwnProperty (): detects whether an attribute exists in an instance or exists in a prototype.  Object.keys (), Object.getownpropertynames (): Receives an object as a parameter and returns an array of strings containing all the enumerable properties. See a veryThe egg hurts .Class Inheritance (some HTML5 game frame): Inherit:function (ChildClass, ParentClass) {var Constructor = new function ()            ;            Constructor.prototype = Parentclass.prototype;            Childclass.prototype = new Constructor ();            ChildClass.prototype.constructor = ChildClass;             Childclass.superclass = Parentclass.prototype; if (ChildClass.prototype.constructor = = Object.prototype.constructor) {childClass.prototype.constructor = P            Arentclass; }} feel the code seems a bit messy, I drew a picture:

Javascript: Prototype pattern class 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.