Javascript Object Prototype Convert to String

Description

Javascript Object Prototype Convert to String

// Writing a more useful toString() method for the Object class
Object.prototype.toString = function() {
  let result = "";
  for (prop in this) 
    result += prop + ": " + this[prop].toString() + ", ";
  return result;//from   w  ww  .  j  av  a2  s . c o m
}

person = {name:"Elvis", age:57};

console.log(person.toString());  // name: Elvis, age: 57, 



PreviousNext

Related