创建javaScript 对象
Posted 千彧
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建javaScript 对象相关的知识,希望对你有一定的参考价值。
创建新实例person 并向其添加四个属性:
1 person=new Object(); 2 person.firstname="Bill"; 3 person.lastname="Gates"; 4 person.age=56; 5 person.eyecolor="blue";
person{firstname:"john",lastname:"doe",age:50,eyecolor:"blue"};
1 function person(firstname,lastname,age,eyecolor){ 2 this.first=firstname; 3 this.lastname=lastname; 4 this.age=age; 5 this.eyecolor=eyecol
创建javascript对象后就可以利用对象创建实例:
1 var myFather = new person("Bill","Gates",56,"blue"); 2 var myMother=new person("Steve","Jobs",48,"green");
把方法添加到JavaScript对象
1 function person(firstname,lastname,age,eyecolor){ 2 this.firstname=firstname; 3 this.lastname=lastname; 4 this.age=age; 5 this.eyecolor=eyecolor; 6 7 this.changeName=changeName; 8 function changeName(name){ 9 this.lastname=name; 10 } 11 }
javaScript 类
javaScript类是面向对象的语言,但javaScript不使用类
在javaScript 中,不会创建类,也不会通过类来创建对象
javaScript基于prototype(原型) 而不是基于类的
javaScript for..in循环
for...in会遍历对象中的每个属性
1 var person=={fname:"bill",lname:"gates",age"56}; 2 3 for(x in person) 4 { 5 txt=txt+person[x]; 6 } 7 document.getElementById("demo").innerhtml=txt;
将会输出:
billgates56