Js-20170829-object Objects and inheritance

Source: Internet
Author: User
Tags hasownproperty

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

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.