<!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 http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>Js</title> </head> <body> <script type="text/javascript"> //在Js中有八种内置对象,String,Number,Boolean,Date,Array,Function,RegExp,Math //Js为每个对象提供了构造器(Math除外),也就意味着能够通过new关键字来创建。 var myDate = new Date(); var myStr = new String("hello"); document.writeln(myDate); document.writeln(myStr); </script> </body> </html>
使用对象直接量创建自定义对象
<!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 http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>Js</title> </head> <body> <script type="text/javascript"> //在Js中创建自定义对象的一个最简单方法,就是使用对象直接量。对象直接量 //将对象的属性放在大括号里面,不同的属性用逗号作为分隔符,属性和属性值 //之间使用冒号作为分隔符。 var monitor = { price:1800, factory:"temp", type:"C2", size:17, open:new Function("document.writeln('hello')"), }; document.writeln(monitor.price); document.writeln(monitor.factory); document.writeln(monitor.type); document.writeln(monitor.size); monitor.open(); </script> </body> </html>