1. Object.getownpropertynames ()The Object.getownpropertynames method returns an array whose members are the key names of all properties of the object itself, and do not contain inherited property key names. Object.getownpropertynames (Date)//["Parse", "arguments", "UTC", "Caller", "name", "Prototype", "Now", "Length"] The properties of the object itself, some can be enumerated (enumerable), some can not be enumerated,The Object.getownpropertynames method returns all key names. Get only those attributes that can be enumerated, using the Object.keys method. Object.keys (Date)//[] 2. Object.prototype.hasOwnProperty ()the hasOwnProperty method of an object instance returns a Boolean value that determines whether a property definition is on the object itself or is defined on the prototype chain. Date.hasownproperty (' length ')//Truedate.hasownproperty (' toString ')//FalseThe hasOwnProperty method is the only method in JavaScript that does not traverse the prototype chain when it handles object properties. 3. In operator and for...in loopThe In operator returns a Boolean value that indicates whether an object has a property. It does not distinguish whether the property is an attribute of the object itself, or an inherited property' Length ' in date//True ' toString ' in date//TrueThe in operator is often used to check whether a property exists4. Object copy if you want to copy an object, you need to do the following two things. Ensure that the copied object has the same prototype prototype object as the original object. Ensure that the copied object has the same properties as the original object. function CopyObject (orig) {var copy = Object.create (object.getprototypeof (orig)); Copyownpropertiesfrom (copy, orig); return copy;} function Copyownpropertiesfrom (target, source) {object.getownpropertynames (source). ForEach (function (propkey) {var desc = object.getownpropertydescriptor (source, Propkey); Object.defineproperty (target, Propkey, desc);}); return target;}
Js-20170829-object Objects and inheritance