Friday 21 April 2017

Override print representation of a JavaScript array [duplicate]

This question already has an answer here:

I'm trying to take an array object and change its print representation - just of that object, not of all arrays in the program. I hoped setting the toString property would do the job, but it doesn't:

var a = [1, 2, 3]

// Prints using the default representation
console.log(a)

// Try to override toString
a.toString = function() {
  return 'some new representation'
}

// Still uses the default representation
console.log(a)

What am I missing?



via rwallace

No comments:

Post a Comment