How to copy an object in js and obtain the values of all 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?

Var obj = {colkey: "col", colsinfo: "NameList"} var obj = {colkey: "col", colsinfo: "NameList,


For example, obj2 has the same attributes as obj.
Var obj2 = new Object (); for (var p in obj) {var name = p; // attribute name var value = obj [p]; // The value obj2 [name] = obj [p];} var obj2 = new Object (); for (var p in obj) {var name = p; // attribute name var value = obj [p]; // value of 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 judge whether the attribute is as follows: The propertyIsEnumerable attribute returns a Boolean value, specifies whether the specified attribute is a part of an object and whether the attribute is enumerable. The object. propertyIsEnumerable (proName) parameter is mandatory. An object. ProName is required. The string value of an attribute name. 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: 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.