//https://xxxgitone.github.io/2017/06/10/JavaScript%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E4%B8%83%E7 %a7%8d%e6%96%b9%e5%bc%8f/Source functionPerson () {} Person.prototype.name= ' Jiang ';//adding properties and methods to an object varPerson1 =NewPerson (); varPerson2 =NewPerson (); Console.log (Person1.name)//JiangConsole.log (Person2.name)//Jiang //Console.log (object.getprototypeof (Person1));//Output specified object prototype //Console.log (Person.prototype); Output Prototypes //function Person () { //} //var person1 = new Person (); //Console.log (Person1.proto = = = Person.prototype)//True the Proto property of the object points to the function prototype //person1 instanceof Person//True instanceof operator handles the relationship between objects and functions //Person.prototype.isPrototypeOf (Person1)//True points to the object calling this method returns true to invoke the property or method added through the prototype //Person.prototype.isPrototypeOf (Person2)//True //object.getprototypeof (person1) = = = Person.prototype//TRUE//Get object prototype //function Person () { //} //Person.prototype.constructor = = person//TRUE//This property is actually referring the prototype object to the associated constructor //function Person () { //} //var person1 = new Person () //Person1.constructor = = = person//TRUE//That is not an instance Person1 also has. constructor attribute, not actually, through the prototype chain found above the prototype Person.prtototype functionPerson () {}//Prototype PropertiesPerson.prototype.name = ' Jiang '; varPerson1 =NewPerson ()//Instance PropertiesPerson1.name = ' J '; Console.log (Person1.name)//J//If it is not found on the instance properties, it will be found in the prototype of the instance, if not yet on the prototype, continue to the prototype of the prototype to find, until the end Object.prototype functionPerson () {}//Prototype PropertiesPerson.prototype.name = ' Jiang '; varPerson1 =NewPerson () console.log (person1.name)//Jiang//The Person1 instance in the above code does not have a name attribute, but can still output the value, which is found on the prototype functionPerson () {}varPerson1 =NewPerson ()//Instance PropertiesPerson1.name = ' J '; Person1.hasownproperty (' Name ')//True//Use method hasOwnProperty, property only exists in the instance to return True functionPerson () {} Person.prototype.age= ' 100 '; varPerson1 =NewPerson () Person1.name= ' J '; ' Name 'inchPerson1//true' Age 'inchPerson1//true//The hasOwnProperty method mentioned earlier can be used to detect if a property is an instance property and in will traverse all properties, whether on an instance or on a prototype //The in operator is used in two ways, used separately and in the For-in loop, first on the base code //Fon-in, it iterates through all the enumerable (enumerated) attributes that can be accessed through the object, whether on an instance or on a prototype for(varPropinchPerson1) {Console.log (prop)//name Age } //Object.keys () This method can get the names of all enumerable properties of an object varKeys =Object.keys (Person1) Console.log (keys)//["Name"] varKeys =Object.keys (Person.prototype) Console.log (keys)//["Age"] //Prototype Object Person.prototype's prototype is Object.prototype
JS prototype prototype