Home  >  Article  >  Web Front-end  >  Code to obtain unknown object attributes in JavaScript_javascript skills

Code to obtain unknown object attributes in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:07:151392browse

In the past two days, I tried to write a demo of AjaxToolkit: AsyncFileUpload. It has an OnClientUploadComplete attribute that can be associated with client JS, so you can write OnClientUploadComplete="uploadComplete", and then define the uploadComplete method:

Copy code The code is as follows:

function uploadComplete(sender, e) {
//Do something here...
}

But, how to get the uploaded file information from e? In addition to looking at the source code of AjaxControlToolkit, you can also use JS:
Copy code The code is as follows:

function uploadComplete(sender, e) {
var ret = "Properties:n";
for (var prop in e) {
var val = e[prop];
if (typeof (val) === "function") {
ret = (prop "()");
}
else {
ret = prop ": " val;
}
ret = ";n";
}
alert(ret);
}

Result:

Code to obtain unknown object attributes in JavaScript_javascript skills

This makes it very clear.

The concept of "associative array" in JS is used here. The attributes of a JS object (including methods, which can also be considered as attributes) are stored in its associative array. Through for...in... Can be traversed to.

Regarding associative arrays, we can use this:

Copy the code The code is as follows:

var dog = new Object();
dog.id = 1;
dog["name"] = "Gougou";
alert("id: " dog["id"] ", name" dog.name);

Get: "id: 1, name: Gougou"

This article is original, please indicate when reprinting: from Freeway —— cnBlogs
Statement:
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