Nodejs Utililty Methods Object to String Convert

List of utility methods to do Object to String Convert

Description

The list of methods to do Object to String Convert are organized into topic(s).

Method

toSource(object)
Object.toSource = function(object){
  if( object === null ) return 'null';
  if( object === undefined ) return 'undefined';
  if( typeof object.toSource === 'function' ){
    var source = object.toSource();
    if( typeof source != 'string' ) throw new TypeError('custom toSource() method must return string');
    return source;
  return String(object);
...
toSource(seen)
Object.prototype.toSource = function(seen){
  var source, props = Object.getOwnPropertyNames(this), i = 0, j = props.length, prop;
  seen = seen || [this];
  source = '({';
  for(;i<j;i++){
    prop = props[i];
    source+= prop.toSource() + ': ' + Object.sourceOf(this[prop], seen, '{}');
    if( i < j - 1 ) source += ', ';
  source+= '})';
  return source;
};
json()
Object.prototype.json = function() { 
    return JSON.stringify(this) 
to_s()
Object.prototype.to_s = function () {
  try {
    return JSON.stringify(this.valueOf());
  } catch (ex) {
    return this.valueOf().toString();
};