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.

Jul 21, 2017 pm 01:37 PM
javascript js copy

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!

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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to copy lyrics from QQ Music How to copy lyrics How to copy lyrics from QQ Music How to copy lyrics Mar 12, 2024 pm 08:22 PM

How to copy lyrics from QQ Music How to copy lyrics

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

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 Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

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 WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

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

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

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

PS copy layer shortcut key PS copy layer shortcut key Feb 23, 2024 pm 02:34 PM

PS copy layer shortcut key

Learn to use copy-paste shortcuts Learn to use copy-paste shortcuts Jan 13, 2024 pm 12:27 PM

Learn to use copy-paste shortcuts

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

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

See all articles