JS

javascript 对象

javascript object

Posted by Lv Hui on May 1, 2014

声明对象

json形式声明

1
2
3
4
5
6
7
var phone = {
	price:'5000$',
	weight:'150g',
	chat:function(){
		alert('can chat');
	}
}

###构造函数形式声明

1
2
3
4
5
6
7
8
9
10
11
funtion fruit(p,c,w){
	this.price = p;
	this.color = c;
	this.weight = w;
	this.eat = function(){
		alert('can eat');
	}
}

var apple = new fruit('5$','red','50g');
alert(apple.color);

访问对象的属性和方法

1
2
3
4
5
6
7
8
9
var phone = {
	price:'5000$',
	weight:'150g',
	chat:function(){
		alert('can chat');
	}
}
document.write(phone.price);
document.write(phone['price']);

对象的遍历

1
2
3
4
5
for(x in y){
	
}
//x是属性名或方法名
//y是要遍历的对象名

销毁对象或属性

1
2
3
4
5
6
//phone为一个对象

delete phone.price; //删除对象属性

phone = null; //销毁对象