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.  }   



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM