js对象

来源:互联网 发布:如何看待网络水军 编辑:程序博客网 时间:2024/05/18 02:55

js对象

js中的所有事物都是对象:字符串,数值,数组,函数....

此外,js允许自定义对象

js提供多个内建对象:String,Date,Array........

对象只是带有属性和方法的特殊数据类型

eg: var   message="hello lwx";

var  x=message.length;    var  y=message.toUpperCase();

创建js对象

创建新对象有两种不同的方法:

1.定义并创建对象的实例

2.使用函数来定义对象,然后创建新的对象实例


第一种:

例如:person=new Object();

person.firstname="bill";

person.lastname="gates";

person.eyecolor="blue";


person{firstname="bill",lastname="gates",eyecolor:"blue"};

第二种:

function   person(firstname,lastname,eyecolor)

{

this.firstname=firstname;

this.lastname=lastname;

this.eyecolor=eyecolor;

}

var  mybrother=new person("","","");

var  myfather=new person("","","");

注意:js是面向对象的语言,但是js不使用类

js  ---- for ... in 循环

var  person={fname:"bill",lname:"gates",age:56};

for  (x  in  person)   {txt=txt+person[x]};

结果是:billgates56












原创粉丝点击