


Detailed explanation of how javascript obtains objects and copies objects.
Jul 21, 2017 pm 01:37 PMUnderstand all the properties and methods of a JS object, and obtain all the properties and methods of an object. The core code and principles are as follows:
function displayProp(obj){ var names=""; for(var name in obj){ names+=name+": "+obj[name]+", "; } alert(names); }
If you know all the properties of this object, you will naturally You can create a new one and assign a value to each attribute, and you can do it, but what if you don't know? How to create an object with the same content?
var obj={ colkey: "col", colsinfo: "NameList" }
The easiest way is to use for in,
For example, obj2 has exactly the same properties as obj
var obj2=new Object(); for(var p in obj) { var name=p;//属性名称 var value=obj[p];//属性对应的值 obj2[name]=obj[p]; }
In fact, this method has certain limitations, the key is for in in js There are certain restrictions. It will not traverse all the properties of the object, but only the enumerable properties. The methods defined by the js core are not enumerable, such as tostring(), but the properties defined in the code are all enumerable. Enumerable (can be specifically defined as non-enumerable). Therefore this method is sufficient.
Whether an object can be exhaustively for in, we can judge through the propertyIsEnumerable attribute, the description is as follows:
propertyIsEnumerable property
Returns a Boolean value, indicating whether the specified property is part of an object and the property Whether it is enumerable.
object.propertyIsEnumerable(proName)
Parameters
object
Required. an object.
proName
Required. A string value for the property name.
Description
If proName exists in object and can be exhausted using a For...In loop, then the propertyIsEnumerable property returns true. The propertyIsEnumerable property returns false if the object does not have the specified property or if the specified property is not enumerable. Typically, predefined properties are not enumerable, whereas user-defined properties are always enumerable.
propertyIsEnumerable property does not consider objects in the prototype chain.
The above is the detailed content of Detailed explanation of how javascript obtains objects and copies objects.. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to copy lyrics from QQ Music How to copy lyrics

Recommended: Excellent JS open source face detection and recognition project

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

JavaScript and WebSocket: Building an efficient real-time weather forecasting system
