标签:定义 函数 cti cts script title 按钮 一个 object
创建了对象的一个新实例,并向其添加了四个属性:
person=new Object();//不要var person.firstname="Bill"; person.lastname="Gates"; person.age=56; person.eyecolor="blue";
替代代码:
var person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};
使用对象构造器:
function person(firstname,lastname,age,eyecolor) { this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; } var tercher=new person("Bill","Gates",56,"blue");
在构造器函数内部定义对象的方法:
function person(firstname,lastname,age,eyecolor) { this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; this.changeName=changeName; function changeName(name) { this.lastname=name; } }
调用:
myMother.changeName("Ballmer");
循环遍历对象的属性:
<!DOCTYPE html> <html> <body> <p>点击下面的按钮,循环遍历对象 "person" 的属性。</p> <button onclick="myFunction()">点击这里</button> <p id="demo"></p> <script> function myFunction() { var x; var txt=""; var person={fname:"Bill",lname:"Gates",age:56}; for (x in person) { txt=txt + person[x]; } document.getElementById("demo").innerHTML=txt; } </script> </body> </html>
结果:BillGates56
标签:定义 函数 cti cts script title 按钮 一个 object
原文地址:http://www.cnblogs.com/wuqiuxue/p/7687603.html