|
类由 3 个部分组成:
构造函数是类的一个方法,在每次创建该类的对象时,构造函数都将被调用。一般情况下,会在构造函数中初始化对象的某些属性,即使构造函数里什么都不做,我们也要编写一个空的构造函数。构造函数可以有多个甚至是 0 个参数。
定义一个类:
[JavaScript] 纯文本查看 复制代码 function CustomerBooking(bookingId, customerName, film, showDate)
{
this.customerName = customerName;
this.bookingId = bookingId;
this.showDate = showDate;
this.film = film;
}
注意 Javascript 里定义类的方法和其它语言的不同,在其它语言里,需要对类进行正式的定义,比如在 C++ 中,需要用 class 关键字来定义一个类,但 JavaScript 则不需要。上面,我们定义了一个 CustomerBooking() 的构造函数,而构造函数又和类名完全相同,所以这在定义出构造函数的同时,也就定义了一个类。当创建该类的一个实例(对象)时,构造函数被自动调用。
这里有一个 this 关键字,它表示的是当前的对象。其中,customerName, bookingId, showDate 和 film 都是当前对象的属性。这就好如我们用数组这个内置对象的 length (表示数组长度) 一样。
创建对象:
创建类的对象时使用 new 关键字,如 var firstBooking = new CustomerBooking;
下面是一个电影院电影资讯和相关订票信息的实例,在该实例中展示了类的定义,对象的创建,属性的赋值,以及方法的使用,代码如下:
[code=javascript]<html>
<body>
<h2>Summary of bookings</h2>
<script language="JavaScript" type="text/javascript">
// 声明一个 CustomerBooking 类
function CustomerBooking(bookingId, customerName, film, showDate)
{
this.customerName = customerName;
this.bookingId = bookingId;
this.showDate = showDate;
this.film = film;
}
//获取客户名方法
CustomerBooking.prototype.getCustomerName = function()
{
return this.customerName;
}
//设置客户名的方法
CustomerBooking.prototype.setCustomerName = function(customerName)
{
this.customerName = customerName;
}
//获取上映日期方法
CustomerBooking.prototype.getShowDate = function()
{
return this.showDate;
}
//设置上映日期方法
CustomerBooking.prototype.setShowDate = function(showDate)
{
this.showDate = showDate;
}
//获取电影名方法
CustomerBooking.prototype.getFilm = function()
{
return this.film;
}
//设置电影名方法
CustomerBooking.prototype.setFilm = function(film)
{
this.film = film;
}
//获取订票ID
CustomerBooking.prototype.getBookingId = function()
{
return this.bookingId;
}
//设置订票ID
CustomerBooking.prototype.setBookingId = function(bookingId)
{
this.bookingId = bookingId;
}
// 声明一个“电影院”类
function cinema()
{
this.bookings = new Array(); //bookings 属性是一个数组,用来存储订票信息
}
cinema.prototype.addBooking = function(bookingId, customerName, film, showDate)
{
//bookings 数组里存储了订票信息,数组的每一个元素是一个 CustomerBooking 类
this.bookings[bookingId] = new CustomerBooking(bookingId,
customerName, film, showDate);
}
//定义 getBookingsTable 方法用以输出订票信息表格
cinema.prototype.getBookingsTable = function()
{
var booking;
var bookingsTableHTML = "<table border=1>"; //表格宽度设置
for (booking in this.bookings) //使用 for .. in 遍历整个数组
{
bookingsTableHTML += "<tr><td>"; //表格的每一行表示一组电影及其订票信息,共4列
bookingsTableHTML += this.bookings[booking].getBookingId(); //获取订票ID
bookingsTableHTML += "</td>";
bookingsTableHTML += "<td>";
bookingsTableHTML += this.bookings[booking].getCustomerName(); //获取客户名
bookingsTableHTML += "</td>";
bookingsTableHTML += "<td>";
bookingsTableHTML += this.bookings[booking].getFilm(); //获取影片名
bookingsTableHTML += "</td>";
bookingsTableHTML += "<td>";
bookingsTableHTML += this.bookings[booking].getShowDate(); //获取影片的放映日期
bookingsTableHTML += "</td>";
bookingsTableHTML += "</tr>";
}
bookingsTableHTML += "</table>"; //表格结束
return bookingsTableHTML;
}
var londonOdeon = new cinema(); //创建一个电影院类的对象
londonOdeon.addBooking(342, "Arnold Palmer","Toy Story", "15 July 2009 20:15");
londonOdeon.addBooking(335, "Louise Anderson",
"The Shawshank Redemption", "27 July 2009 11:25");
londonOdeon.addBooking(566, "Catherine Hughes",
"Never Say Never", "27 July 2009 17:55");
londonOdeon.addBooking(324, "Beci Smith","Shrek", "29 July 2009 20:15");
document.write(londonOdeon.getBookingsTable()); //输出整个表格
</script>
</body>
</html>[/mw_shl_code]
运行输出:
上面还看到 prototype ,在 JavaScript 中的大部分对象都具有的一个属性,通过 prototype 属性可以创建新的属性和方法。因此,无论何时想为类创建新的方法时,都可以使用如下所示代码:className.prototype.methodName = function(method parameter list)
{
//方法的具体实现代码
} |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|