首页 > 代码库 > JAVASCRIPT对象

JAVASCRIPT对象

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

<!DOCTYPE html><html lang="en">    <head>        <meta charset="UTF-8">        <title>TESTJAVASCRIPT</title>    </head>    <body>        <script type="text/javascript">        //var myData=http://www.mamicode.com/new Object();        //定义对象        var myData=http://www.mamicode.com/{            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>

效果如下:

技术分享

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

JAVASCRIPT对象