Home > Web Front-end > JS Tutorial > Detailed explanation of how javascript obtains objects and copies objects.

Detailed explanation of how javascript obtains objects and copies objects.

伊谢尔伦
Release: 2017-07-21 13:37:42
Original
1315 people have browsed it

Understand 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); 
}
Copy after login

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" }
Copy after login

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]; 
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template