多的不说,直接上练习例子:
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>
时间: 2024-10-26 09:35:23