取得物件所有屬性

4 views
Skip to first unread message

gugod

unread,
Nov 16, 2008, 7:06:38 AM11/16/08
to hsinchu.js
以下 keys 函式可以用來取得物件所有的屬性(property)名稱:

function keys(o) {
var r = [];
for(var a in o) { r.push(a); }
return r;
}

使用方法如:

pet = {
"nick": "little black",
"age": 3,
"type": "cat"
};

properties = keys(pet); // [ "nick", "age", "type" ]

Reply all
Reply to author
Forward
0 new messages