///<class>继承Array类</class>
HotelInfoSet = function(){
Array.apply(this);
}HotelInfoSet.prototype = new Array();HotelInfoSet.prototype.count = function(){
return this.length;
}HotelInfoSet.prototype.add = function(hotelInfo){
return this.push(hotelInfo);
}HotelInfoSet.prototype.AddRange = function(hotelInfoSet){
this.concat(hotelInfoSet);
}var hotelInfoSet = new HotelInfoSet();
hotelInfoSet.add("aa");
hotelInfoSet.add("bb");
alert(hotelInfoSet.length);hotelInfoSet的长度为0,数据添加不到hotelInfoSet中,是不是继承Array有问题?
HotelInfoSet = function(){
Array.apply(this);
}HotelInfoSet.prototype = new Array();HotelInfoSet.prototype.count = function(){
return this.length;
}HotelInfoSet.prototype.add = function(hotelInfo){
return this.push(hotelInfo);
}HotelInfoSet.prototype.AddRange = function(hotelInfoSet){
this.concat(hotelInfoSet);
}var hotelInfoSet = new HotelInfoSet();
hotelInfoSet.add("aa");
hotelInfoSet.add("bb");
alert(hotelInfoSet.length);hotelInfoSet的长度为0,数据添加不到hotelInfoSet中,是不是继承Array有问题?
解决方案 »
- CSDN 留言版的引用留言功能是怎么做的?
- 请问这段代码错在哪里?
- 各路英雄帮我看看这让我郁闷的。。。(正则)
- JS中 如何取道变量的地址?如何取克隆一个对象副本?
- 请教各位高手domain这个属性怎么用啊,帮帮我啊,各位大侠!
- 关于button和table的问题
- 很简单的Javascript,很容易的,关于window.open()的用法。
- 请问怎样将颜色关键字转换为相应的RGB值?
- 各大虾,有没有办法让window.showModalDialog打开的页面关闭的时候,不刷新父页。
- 请问这个语句的值:var a = document.getElementById("abc").id;
- 用javasrcipt 怎样重写url
- 怎样隐藏浏览器的地址里的url,现在我仅仅是隐藏了地址栏而已。
<SCRIPT LANGUAGE="JavaScript">
<!--
HotelInfoSet = function(){
this._strings = new Array();
} //HotelInfoSet.prototype = new Array(); HotelInfoSet.prototype.count = function(){
return this._strings.length;
} HotelInfoSet.prototype.add = function(hotelInfo){
return this._strings.push(hotelInfo);
} HotelInfoSet.prototype.AddRange = function(hotelInfoSet){
this.concat(hotelInfoSet);
} var hotelInfoSet = new HotelInfoSet();
hotelInfoSet.add("aa");
hotelInfoSet.add("bb");
hotelInfoSet.add("bb");
hotelInfoSet.add("bb");
hotelInfoSet.add("bb");
alert(hotelInfoSet.count());
//-->
</SCRIPT>
Array.apply(this);
} HotelInfoSet.prototype = new Array();
举个例子你看:
1.构造继承的局限性<SCRIPT LANGUAGE="JavaScript">
<!--
function MyDate(){
this.base = Date;
this.base.apply(this, arguments);
}
var date = new MyDate();
alert(date.toGMTString)//返回undefined
//-->
</SCRIPT>
2.原型继承的局限性<SCRIPT LANGUAGE="JavaScript">
<!--
function MyDate(){
}
MyDate.prototype = new Date();
var date = new MyDate();
alert(date.toGMTString)//显然也没达到效果//-->
</SCRIPT>
所以核心对象并不能像我们自定义对象那样工作,所以只能通过像我上面的实例继承达到效果。。
不知道 我理解得是否正确
HotelInfoSet = function(){
return Array.apply(this);
} HotelInfoSet.prototype = Array.prototype; HotelInfoSet.prototype.count = function(){
return this.length;
} HotelInfoSet.prototype.add = function(hotelInfo){
return this.push(hotelInfo);
} HotelInfoSet.prototype.AddRange = function(hotelInfoSet){
this.concat(hotelInfoSet);
} var hotelInfoSet = new HotelInfoSet();
hotelInfoSet.add("aa");
hotelInfoSet.add("bb");
alert(hotelInfoSet.length);
alert(hotelInfoSet[1]);
this._arr = new Array();
} HotelInfoSet.prototype.count = function(){
return this._arr.length;
} HotelInfoSet.prototype.add = function(hotelInfo){
return this._arr.push(hotelInfo);
}
var hotelInfoSet = new HotelInfoSet();
hotelInfoSet.add("aa");
hotelInfoSet.add("bb");
alert(hotelInfoSet.count());
那样麻烦就是
http://blog.csdn.net/muxrwc/archive/2007/10/25/1843775.aspx
类似这样的。。