Home >Web Front-end >JS Tutorial >js遍历、动态的添加数据的小例子_javascript技巧

js遍历、动态的添加数据的小例子_javascript技巧

WBOY
WBOYOriginal
2016-05-16 17:31:39930browse
复制代码 代码如下:

function jsonObj(){
    var person= {name: 'zhangsan',pass: '123' ,'sni.ni' : 'XMQ',back:function (){
        for(var i=0;i        //在不知参数个数情况下可通过for循环遍历
        // arguments这个是js 默认提供
        alert("arr["+i+"]="+arguments[i]);
        }
        }
        };
    //遍历属性
    for(var item in person){
        if(typeof person[item] === 'string'){
            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'){
            alert("person中"+item+"的值="+person[item]);
            }else if(typeof person[item] === 'function'){
                person[item](1,1);
                }
                }

}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn