JS's prototype basic article

Source: Internet
Author: User

function Hiclass () {this.sayhi = function () {alert ("HI");}} var obj = new Hiclass (); alert (hiclass.prototype);//outputs [Object, Object]alert (obj.prototype);//outputs undefined

When inheriting a class in a prototype way, be aware of the properties and methods that may be overridden by other prototype methods. If it is HiClass.prototype.prop = ' value ';
Before hiclass.prototype = new Helloclass (), execute hiclass.prototype = new Helloclass (), and then Hiclass attribute will not be prop in the instance, this should be well understood. But be aware that there are bugs in the code that are not accidentally.

function Hiclass () {this.sayhi = function () {alert ("HI");}} function Helloclass () {This.sayhello = function () {alert ("Hello");}} HiClass.prototype.prop = ' value '; Hiclass.prototype = new Helloclass (); HiClass.prototype.prop = ' value '; var obj = new Hiclass (); Obj.sayhello (); alert (Obj.prop);

There is a prototype chain in JavaScript, when invoking a method on an object instance or acquiring a property, first see the instance corresponding to the class has no corresponding definition, no word will follow the prototype chain always find, not found is undefined.

Function object2 ()  {    this.sayhi = function () {         alert ("Hi object2");     }} Function object3 ()  {    this.sayhi = function () {         alert ("Hi object3");     }         this.sayhello = function () {         alert ("Hello Object3");     }     } Object2.prototype = new object3 (); Var obj = new object2 (); Obj.sayHi (); O Bj.sayhello (); Object.prototype.foo = function () {    alert ("f00  Object ");};o Bj.foo ();

Call Obj.sayhi (); When the sayhi is defined in Object2, the definition of the call itself is called.

When calling Obj.sayhello (), SayHello is not defined in Object2, but because prototype points to OBJECT3, the OBJECT3 is found and executed.

When calling Obj.foo (), SayHello is not defined in Object2 and OBJECT3, but because all instances in JS inherit object, Foo can be found in the prototype of object and then executed.

If the attributes and methods added in the instance have the same name as the attributes and methods in the prototype, there is this property and method relative to the instance and prototype, but only because of the order in which the prototype chain is accessed, the properties and methods in the instance are accessed first. This is illustrated by the following example:

function Test () {};  TEST.PROTOTYPE.PROP1 = ' prop value '; var obj = new Test (); obj.prop1 = ' instance value '; alert (OBJ.PROP1);//outputs instance Valuealert (Obj.hasownproperty ("Prop1"));//outputs truedelete Obj.prop1;alert (OBJ.PROP1); Outputs prop Valuealert (Obj.hasownproperty ("Prop1"));//outputs Falsealert ("Prop1" in obj);//outputs truedelete Test.prototype.prop1;alert (OBJ.PROP1);//outputs undefined
function Testcls () {}testcls.prototype = {prop1: ' value1 ', prop2: ' value2 '};myobj = new testcls (); alert (MYOBJ.PROP2);


JS's prototype basic 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.