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? Varobj {colkey: & quot; c... SyntaxHighlighter. all (); 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.