String Format

String.prototype.format = function() {app

    var args = arguments;this

    return this.replace(/{(\d+)}/g, function(match, i) { spa

      return typeof args[i] != 'undefined' ? args[i] : match;prototype

    });orm

};io

String.format = function(template) {
console

    if(0 == arguments.length) return null;function

    var args = Array.prototype.slice.call(arguments, 1);form

    return String.prototype.format.apply(template, args);call

}

if(typeof jQuery != 'undefined') {

    jQuery.extend({

        format: function(template) {

            return String.format.apply(template, arguments);

        }

    });

}

// console.log('{0} {1}'.format('hello', 'world'));

// console.log(String.format('{0} {1}', 'hello', 'world'));

// console.log($.format('{0} {1}', 'hello', 'jQuery'));

相關文章
相關標籤/搜索