遍歷json 對象的屬性而且動態添加屬性

昨天由於公司的一個需求,因此就研究了一下json對象的屬性的遍歷和動態修改:json

var person= {   
  name: 'zhangsan',   
  pass: '123' ,   
  'sni.ni' : 'sss',   
  hello:function (){   
     for(var i=0;i<arguments.length;i++){   
             //在不知參數個數狀況下可經過for循環遍歷               
             // arguments這個是js 默認提供   
            alert("arr["+i+"]="+arguments[i]);   
     }       
  }   
 }   
     
//遍歷屬性   
 for(var item in person){   
    if(typeof person[item]  === 'string' || typeof dayobj[item]  === 'number'){   
      alert("person中"+item+"的值="+person[item]);   
    }else if(typeof person[item] === 'function'){   
        person[item](1,1);//js 的function的參數能夠動態的改變   
    }    
 }   
//添加屬性   
  
 person.isMe = 'kaobian'; // 這種是屬性名字正常的   
//當屬性名字不正常時,像下面這種,必須用這種形式的,   
 person['isMe.kaobian'] = 'hello kaobian'; //上面的也能夠用下面的形式   
  
 for(var item in person){   
    if(typeof person[item]  === 'string' || typeof dayobj[item]  === 'number'){   
      alert("person中"+item+"的值="+person[item]);   
    }else if(typeof person[item] === 'function'){   
  
        person[item](1,1);   
    }    
 }   

 

出處:http://kaobian.iteye.com/blog/1152239spa

相關文章
相關標籤/搜索