JAVASCRIPT对象

越是学习就感觉自己知道的东西越来越少,这是一个必然的过程,再次将知识综合回顾一下.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>TESTJAVASCRIPT</title>
    </head>
    <body>
        <script type="text/javascript">
        //var myData=new Object();
        //定义对象
        var myData={
            name:'Jeccy',
            sunny:'suny',
            printMessages:function(){
                document.writeln("Hello "+myData.name+"."+'Today is '+this.weather+'.');
            }
        }
        //增加属性
        myData.dayOfWeek='sunday';
        myData.sayHello=function(){
            document.writeln("Hello");
        }
        //删除属性
        delete myData.name;
        //遍历对象属性
        for(var prop in myData){
            document.writeln("Name:"+prop+" Value:"+myData[prop]+'<p />');
        }
        //判断对象是否存在某属性
        var hasName='name' in myData;
        document.writeln("Hasname:"+hasName);
        </script>
    </body>
</html>

效果如下:

image

注:遍历中用<p />换行是我的浏览器写writeln不自动换行.

posted @ 2017-03-11 15:04  陨落流星  阅读(285)  评论(0编辑  收藏  举报