日期:2014-05-16 浏览次数:20395 次
JS中的对象是一种复合数据类型,将多个数据值集中在一个单元中,允许使用名字来存取这些值。
也可以理解为:对象是一个无序的属性集合,每个属性都有自己的名字和值。
一、创建
//直接加入一个对象直接量,是用逗号分隔开的一对对的属性名和值的列表
var empty = {};
var point = {x:0,y:0};
var circle = { x:point.x,y:point.y+1,radius:2};
var homer={
"name":"Home Simpson",
"age":34,
"married":true,
"occupation":"plant operator",
'email':"homer@example.com"
}//使用 . 来存取对象的属性
var book = {};
book.title = "简·爱";
book.chapter1 = new Object{};
book.chapter1.title = "Introduction to JavaScript";
book.chapter1.pages = 11;
book.chapter2 = { title:"Lexical Structure",pages:6};
alert(
"Outline:"+book.title+"\n\t"+
"Chapter1"+book.chapter1.title+"\n\t"+
"Chapter2"+book.chapter2.title
);function DisplayProertyNames(obj){
var name="";
for(var name in obj){
names=name+"\n";
}
alert(names);
}delete book.chapter2;
//可以使用以下的代码确定一个未知值的类型 if((typeof o=="object")&&(o.constructor==Date)) //instanceof可以检查constructor属性的值,所以上面的代码也可以这样写 if((typeof o=="object"&&(o instanceof Date)))