Prototype Chain I. Relationship between a constructor and a prototype chain
In the blog: http://www.cnblogs.com/shuiyi/p/5305435.html
The diagram
Example:
1<! DOCTYPE html>234<meta charset= "UTF-8" >5<title>Title</title>6<script>7 functionPerson (obj) {8 This. obj = obj | | {}9 This. Name = This. obj.name | | Anonymous;Ten This. Age = This. obj.age | | 18; One This. Sex = This. obj.sex | | Male; A } - -Person.prototype.fn =function () { theConsole.log ("I am a function mounted on the prototype chain"); - } - varP1 =NewPerson ({ -Name: "Xiaoming" + }); - //Console.log (P1.__proto__.sayhi = = = P1.sayhi);//Ture +Console.log ("constructor"); A Console.dir (person); atConsole.log ("instantiated object"); - Console.dir (p1); -Console.log (Person.prototype = = = p1.__proto__);//true -Console.log (personinstanceofObject);//true - -</script> in -<body> to +</body> -Person.prototype = = = p1.__proto__ and the person instanceof object is the reason for true:
Two. prototype pointing<! DOCTYPE html>//All things are objects functionPerson (name,age) { This. Name = Name | | Name; This. Age = Age | | 18; } functionstuent (number) {//this.name = name;//this.age =age; This. Number =Number ; } //Modify Point method one: recommendedStuent.prototype =NewPerson ();//var stu1 = new Stuent ("Number One"); //Modify the Point method two://Stuent.prototype = {//Constructor:person,//age:11// };Console.dir (stuent);//Console.dir (stu1.age);</script>Three. js built-in object's prototype chain1<! DOCTYPE html>234<meta charset= "UTF-8" >5<script>6 //new Object ();7 //new number ();8 //new String ();9 //new Boolean ();Ten //new Function (); One //new Date (); A - //var obj1 = new Object (); - //var obj2 = {}; the //Console.dir (obj1); - //Console.dir (OBJ2); - // - //Console.log (obj1 instanceof Object);//True + //Console.log (obj2 instanceof Object);//True - + varData1 =NewNumber ("123"); A Console.dir (data1); atConsole.log (data1.__proto__);// Number -Console.log (data1.__proto__.__proto__);//Object -Console.log (data1.__proto__.__proto__.__proto__);//NULL -Console.log (data1.__proto__.__proto__ = = = data1.__proto__.__proto__);//ture - //Console.dir (typeof data1.tostring ());//"123" string - in varData2 =true; - //Console.log (data2.tostring ());//"True" to +</script> -<title>Title</title> the *<body> $ Panax Notoginseng</body> -javascript--prototype chain