前端笔试练习一
请编写一段程序,将一个对象和它直接、间接引用的所有对象的属性字符串放入一个数组。如var o = {a:1,{b:2,c:{d:1}}}这里o经过处理后,应该得到["a","b","c","d"]
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <script type="text/javascript"> 5 window.onload = function(){ 6 7 //请编写一段程序,将一个对象和它直接、间接引用的所有对象的属性字符串放入一个数组。如 8 9 //var o = {a:1,{b:2,c:{d:1}}}这里o经过处理后,应该得到["a","b","c","d"] 10 11 12 var o = {a:1,b:{c:{d:1}}};//输出 a b c d 13 //var o = {a:1,b:{c:2}}; 14 alert(getProperties(o)); 15 } 16 function getProperties(obj){ 17 var property = []; 18 for(var key in obj){ 19 property.push(key); 20 } 21 for(var key in obj){ 22 if(typeof obj[key] == 'object'){ 23 return property.concat(arguments.callee(obj[key])); 24 } 25 } 26 return property; 27 } 28 </script> 29 </head> 30 31 <body> 32 33 </body> 34 </html>