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]
有人可以解释一下为什么没有调用父类(super class)(Array
)构造函数吗?
最佳答案
仅仅因为 StringStream.prototype
是一个数组,StringStream
构造函数也不会被 Array
替换。
您应该自己实现:http://jsfiddle.net/gBrtf/ .
function StringStream() {
// push arguments as elements to this instance
Array.prototype.push.apply(this, arguments);
}
StringStream.prototype = new Array;
StringStream.prototype.toString = function(){
return this.join('');
};
关于javascript - JavaScript 的简单继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7697914/