JavaScript 的简单继承
function StringStream() {}
StringStream.prototype = new Array();
StringStream.prototype.toString = function(){ return this.join(''); };
调用 new StringStream(1,2,3)
给出一个空数组
x = new StringStream(1,2,3)
,
StringStream[0]
__proto__: Array[0]
有人可以解释一下为什么没有调用超类的(Array
)构造函数吗?
function StringStream() {}
StringStream.prototype = new Array();
StringStream.prototype.toString = function(){ return this.join(''); };
Calling new StringStream(1,2,3)
gives an empty array
x = new StringStream(1,2,3)
gives
StringStream[0]
__proto__: Array[0]
Can someone please explain why the superclass' (Array
) constructor is not called?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅仅因为
StringStream.prototype
是一个数组,StringStream
构造函数也不会被Array
替换。您应该自己实现:http://jsfiddle.net/gBrtf/。
Just because
StringStream.prototype
is an array, theStringStream
constructor is not replaced withArray
as well.You should implement that yourself: http://jsfiddle.net/gBrtf/.