JavaScript 获取未知对象的属性
这两天尝试写个AjaxToolkit:AsyncFileUpload的Demo, 它有个OnClientUploadComplete属性可以关联客户端JS, 于是可以写OnClientUploadComplete="uploadComplete", 然后定义uploadComplete方法:
1 function uploadComplete(sender, e) {
2 //Do something here...
3 }
1 function uploadComplete(sender, e) {
2 var ret = "Properties:\n";
3 for (var prop in e) {
4 var val = e[prop];
5 if (typeof (val) === "function") {
6 ret += (prop + "()");
7 }
8 else {
9 ret += prop + ": " + val;
10 }
11 ret += ";\n";
12 }
13 alert(ret);
14 }
结果:
这样就很明白了.
这里是用了JS里"关联数组"的概念, JS对象的属性(包括方法, 也可认为是属性)是存放到它的关联数组里面的, 通过for...in...就可以遍历到.
关于关联数组, 我们可以这么用:
1 var dog = new Object();
2 dog.id = 1;
3 dog["name"] = "Gougou";
4 alert("id: " + dog["id"] + ", name" + dog.name);
得到: "id: 1, name: Gougou"
本文系☆大森林☆创作于博客园, 转载请保留此说明 更多请访问 freeway.cnblogs.com |