js遍历对象的属性并且动态添加属性

Java代码   收藏代码
  1.  var person= {  
  2.   name: 'zhangsan',  
  3.   pass: '123' ,  
  4.   'sni.ni' : 'sss',  
  5.   hello:function (){  
  6.      for(var i=0;i<arguments.length;i++){  
  7.              //在不知参数个数情况下可通过for循环遍历              
  8.              // arguments这个是js 默认提供  
  9.             alert("arr["+i+"]="+arguments[i]);  
  10.      }      
  11.   }  
  12.  }  
  13.     
  14. //遍历属性  
  15.  for(var item in person){  
  16.     if(typeof person[item]  === 'string'){  
  17.       alert("person中"+item+"的值="+person[item]);  
  18.     }else if(typeof person[item] === 'function'){  
  19.         person[item](1,1);//js 的function的参数可以动态的改变  
  20.     }   
  21.  }  
  22. //添加属性  
  23.   
  24.  person.isMe = 'kaobian'// 这种是属性名字正常的  
  25. //当属性名字不正常时,像下面这种,必须用这种形式的,  
  26.  person['isMe.kaobian'] = 'hello kaobian'//上面的也可以用下面的形式  
  27.   
  28.  for(var item in person){  
  29.     if(typeof person[item]  === 'string'){  
  30.       alert("person中"+item+"的值="+person[item]);  
  31.     }else if(typeof person[item] === 'function'){  
  32.   
  33.         person[item](1,1);  
  34.     }   
  35.  }   


原文地址:https://www.cnblogs.com/javawebsoa/p/3206350.html