How to copy an object in js and obtain the values corresponding to all its attributes and attributes

Source: Internet
Author: User

How to copy an object in js, such as the next js object.

If you know all the attributes of this object, you can naturally create a new one, and then assign values to each attribute, you can do it, but if you do not know? How to create an object with the same content?
Copy codeThe Code is as follows:
Var obj = {colkey: "col", colsinfo: "NameList "}

The simplest is to use for in,

For example, obj2 has the same attributes as obj.
Copy codeThe Code is as follows:
Var obj2 = new Object ();
For (var p in obj)
{
Var name = p; // attribute name
Var value = obj [p]; // value corresponding to the attribute
Obj2 [name] = obj [p];
}

In fact, this method has certain restrictions. The key is that for in js has certain restrictions. It does not traverse all the attributes of the object, but only traverses the enumerated attributes, methods defined by the core of js cannot be enumerated, for example, tostring (). However, all attributes defined in the Code can be enumerated (which can be defined as non-enumerated by special definitions ). Therefore, this method is enough.

Whether an object can be used for in is exhaustive. We can use the propertyIsEnumerable attribute to determine whether an object can be used for in, which is described as follows:
PropertyIsEnumerable attributes
Returns a Boolean value indicating whether the specified attribute is a part of an object and whether the attribute is enumerable.
Object. propertyIsEnumerable (proName)
Parameters
Object
Required. An object.
ProName
Required. The string value of an attribute name.
Description
If the proName exists in the object and you can use a... If the In loop is exhausted, the propertyIsEnumerable attribute returns true. If the object does not have a specified attribute or the specified attribute is not enumerable, The propertyIsEnumerable attribute returns false. Typically, predefined attributes are not configurable, and user-defined attributes are always configurable.
The propertyIsEnumerable attribute does not consider objects in the prototype chain.

Related Article

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.