Javascript String supplant(obj)

Description

Javascript String supplant(obj)


String.prototype.supplant = function (obj) {
  function replacer(_, name) {
    var value = obj[name]
    var type = (typeof value)

    if (value instanceof Array) return value.toString();
    if (type === 'string')      return value;
    if (type === 'number')      return value;

    return name
  }//from  w w  w  . j  a  v  a2 s.  c  o  m
  return this.replace(/{([^{}]*)}/g, replacer);
};



PreviousNext

Related