0
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?

1
  • who gave you this code and why ? :( Commented Oct 8, 2011 at 15:56

1 Answer 1

2

Just because StringStream.prototype is an array, the StringStream constructor is not replaced with Array as well.

You should implement that yourself: 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('');
};
Sign up to request clarification or add additional context in comments.

2 Comments

So there is no way to call the constructor of Array obj and passing the arguments instead of invoking the push method?
@Markos Evlogimenos: You cannot combine new and .apply so I guess there is no way. Anyway, why are you trying to create a constructor that is equivalent to Array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.