Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm newbie in js. I redefined push() method in Array Object like below..

Array.prototype.push = function(item) {
    this[this.length] = '[' + item + ']';
    return this;
};

var arr = new Array();
arr.push('my');
console.debug(arr);
console.debug(arr[0]);

arr.push('name');
console.debug(arr);
console.debug(arr[1]);

arr.push('is');
console.debug(arr);
console.debug(arr[2]);


// output

[] --> <1>
[my]
[] --> <2>
[name]
[] --> <3>
[is]

but I can't understand why <1>,<2>,<3> is empty.

share|improve this question
    
Are you debugging in chrome by any chance? See stackoverflow.com/questions/4057440/… –  Crescent Fresh Mar 25 '11 at 2:44
add comment

2 Answers

up vote 1 down vote accepted

If you remove the brackets being concatenated, it works.

jsFiddle.

It looks like push is called internally a bit, and this may be why it is not working.

Also, there shouldn't be any reason to re-implement push yourself.

share|improve this answer
    
I too noticed this. Extremely odd behavior. –  Reid Mar 25 '11 at 2:47
    
it appears to happen only in webkit browsers. –  kjy112 Mar 25 '11 at 2:49
    
I am testing in Chrome 10 :) –  alex Mar 25 '11 at 2:50
    
This does not seem to be the case for me. I justed tested in Chromium 10.0.648.133 (Linux) and the issue is still prevalent. –  tbranyen Mar 25 '11 at 3:09
add comment

Try using console.debug(arr.join(',')); instead of console.debug(arr);.

As in this jsfiddle.

The output is now

[my]
[my]
[my],[name]
[name]
[my],[name],[is]
[is]

Tested on Chrome.

As for the strange behavior of debug.console() when printing arrays, I suspect that it also uses push() on arrays while building the output string. If, for example, you replace the '['+item+']' with '<<'+item+'>>' you get some whackiness in the Firebug console, as in this jsfiddle.

share|improve this answer
    
This is a very informative post and most definitely the answer. Chrome Tools wackiness. –  tbranyen Mar 27 '11 at 23:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.