javascript对象学习笔记

时间:2014-10-06 16:12:20   收藏:0   阅读:151

多的不说,直接上练习例子:

1.对象的属性、创建、构造函数、方法

<html xmlns="http://www.w3.org/1999/xhtml">

<body>

<script type="text/javascript">

//函数:用作对象的方法
function Rectangle_area() {return this.width * this.height;}
function Rectangle_perimeter() {return this.width * 2 + this.height * 2;}
function Rectangle_set_size(w, h) { this.width = w; this.height = h;}
//object_Rectangle 的构造函数
function object_Rectangle(w, h) 
{
	//初始化对象的属性
	this.width = w;
	this.height = h;
	//定义对象的方法
	this.area = Rectangle_area;
	this.perimeter = Rectangle_perimeter;
	this.set_size = Rectangle_set_size;
}
//创建object_Rectangle对象,调用它的方法
var r = new object_Rectangle(3, 4);
var a = r.area();
document.write(a);
</script>

</body>
</html>


评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!