將自定義對象和用內置對象初始化一個變量,使變量成爲對象實踐了一下:
不懂將代碼表框,只好這麼寫...
代碼如下:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
- <title>Learning Object in JavaScript!</title>
- <script type="text/javascript" >
- //This function can refill the page with the value of an object
- function Show(Object)
- {
- for (var ptr in Object){
- document.write(Object[ptr]);
- document.write("<br/>");
- }
- }
- //Define an object "People"
- function Peoples(name,age)
- {
- this.name=name;
- this.age=age;
- }
- </script>
- </head>
- <body>
- <script type="text/javascript" >
- //Create a new Object with Object
- var p=new Object();
- p.name="Baby Aloha";
- p.age=20;
- //Go through the Object
- for (var prop in p)
- alert(p[prop]);
- //Create a object with the object
- var q=new Peoples();
- q.name="Andrew";
- q.age=19;
- </script>
- <input type="button" value="Show Props!" onclick="Show(q);"/>
- <p>Click the button to show information I`ve set to variable q.</p>
- </body>
- </html>