We know a way to define a common object in JavaScript, such as:
Let obj == 1= ' string 'function() {alert (' func '= [' x ', ' Y ']; Console.log (obj.num); // 1console.log (obj.string); // "string"Console.log (obj.func); // function () {alert (' func ')}Console.log (Obj.arr); // ["X", "Y"]
Or:
Let obj = { 1, ' string ', function() {alert (' func ')}, arr: [' x ', ' Y ']}
Constructor method:
function animal () { this. Name = ' Animal'new animal (); Console.log ( Obj.name); // "Animal"
Wait a minute.
However, each object has its own prototype attribute, for example, we add a prototype attribute to an object:
function animal () { = ' animal '; = 1new= ten; Console.log (obj.name); // ' Animal 'console.log (obj.x); // Ten // 1
Above we have added the name and X property to the Animal object prototype, and in the following new constructor, obj, it inherits the animal object itself, so we can find the Name property value in obj, but once obj gives animal the attribute ' x ' When the value of the value is re-assigned to 10, it is changed, but the ' X ' value on the animal prototype is unchanged, because the property in obj is the first to find its own defined property, if not found, will go to its prototype chain to find, that is, animal.
Let's take another look at the case:
Console.log (typeof// "function" in obj) //true ' in ' Keyword will also find its prototype chain up, so name is present Console.log (Obj.hasownproperty (' x ')) // true Console.log (Obj.hasownproperty (' name ')) //false
Why does the ToString method come from the Obj object and the animal object we just didn't define the ToString method? This is also a priority issue, and it will always look up from obj until this property is found, and if no will return undefined, each Object object prototype Ririe has the ToString method by default. The hasOwnProperty () method is used to determine whether an object has a property or object that you give a name to, it uses it to judge the current object, and cannot determine whether the property exists on the prototype chain of the current object, because Obj does not define the Name property, and all returns FALSE.
Another method for inheriting the prototype chain of an object is introduced:
New Object ({ ' cat ', y:+ == 1; Console.log (Obj.hasownproperty (' X ')) //trueconsole.log (obj.hasownproperty (' y ')) //false Console.log (obj.name) //catconsole.log (OBJ.Y)/ /
Introduction to JavaScript Creation object and object prototype chain properties