标签:
一、前言
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> </head> <body> <script> //创建直接的实例 persion = new Object(); persion.firstname = "Bill"; persion.lastname = "Gates"; persion.age = 56; persion.eyecolor = "blue"; document.write(persion.firstname + " is " + persion.age + " years old.<br/>"); //替代语法(使用对象literals) persion = {firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"}; document.write(persion.firstname + " is " + persion.age + " years old.<br/>"); //使用构造器构造对象 function persionV(firstname,lastname,age,eyecolor){ this.firstname = firstname; this.lastname = lastname; this.age = age; this.eyecolor = eyecolor; //将方法添加到JavaScript对象中 this.changeName = changeName; function changeName(name){ this.lastname = name; } } myFather = new persionV("Bill","GateV1",89,"red"); document.write(myFather.firstname + " is " + myFather.age + " years old.<br/>"); myFather.changeName("hehehe"); document.write(myFather.firstname + " " + myFather.lastname + " is " + myFather.age + " years old.<br/>"); </script> </body> </html>
objectName.propertyName
具体例子:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> </head> <body> <script> var message = "Hello World!"; var x = message.length; document.write(x) </script> </body> </html>
objectName.methodName()
具体例子:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> </head> <body> <script> var message = "Hello World!"; var x = message.toUpperCase(); document.write(x) </script> </body> </html>
标签:
原文地址:http://www.cnblogs.com/ChenKeng/p/4267288.html