js 、json 、数组遍历(for in)


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="Keywords" content="关键字,关键词">
    <meta name="Description" content="描述和简介">
    <title>Title</title>
    <style type="text/css">
        *{margin:0;padding:0;}
        body,ul,li,ol,dl,dd,p,h1,h2,h3,h4,h5,h6{ margin:0;}
        a{text-decoration:none;}
        img{border:none;}
        ol,ul{list-style:none;}
        #box{
            border: 1px solid red;
        }
    </style>
</head>
<body>
    <!--
        json的写法
            大括号中写键值对
            var a = {属性:值,属性:值}
    -->
    <div id="box"></div>

    <script>
        var oBox = document.getElementById("box");

        var a = {
            name : "女神",
            sex : "女",
            marry : false,
            age : 18,
            sayHellow : function (){
                alert("hello!");
                console.log(this);
            },
            weight : "53kg",
            qq : 16618,
            height : 161
        };
        //alert(a.name);
        oBox.innerHTML = "名字: "+a.name+",年龄: "+a.age+",性别: "+a.sex+",婚姻状况: "+(a.marry?"已婚":"未婚");

        alert(typeof a.age);

       // a.sayHellow();
/*
        window.dachui();
        function dachui(){
            alert("你好,大锤");
            console.log(this);
        };
*/

       // alert(a["name"]);

/*        var x = "name";
        alert(a[x]);*/

    /*
    * 遍历json里面的属性,在不清楚json里有多少个属性时,可用for in循环
    *
    * */
    for(var dachui in a){
        //console.log(dachui);
        //console.log(typeof dachui);
        //console.log("属性: "+key+"值: "+a[key]);
    };

    /*
    * for in 遍历数组
    *
    * */
/*    var arr = ["1",4,7,9,0];
    for(var key in arr){//key 的类型是string
        console.log(typeof key);
    };*/

    /*
    * 新增属性
    *
    * */
    a.phone = "156";//新增的属性会按照字母的顺序排序
    //console.log(a);

    /*
    * 删除属性
    *
    * */
    //a.marry = ""; //清空,不是删除
    delete a.marry;
    //console.log(a);

    /*
    * 添加下标属性(数字属性)
    *
    * */
/*    a[2] = "嘿嘿嘿";
    a[0] = "哈哈哈哈";*/

    /*
    * json没有length属性
    *
    * */
    a.length = "不是长度";
    alert(a.length);

   console.log(a);
    </script>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值