码迷,mamicode.com
首页 > 其他好文 > 详细

String Format

时间:2014-11-18 16:16:28      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:style   io   ar   sp   for   on   log   cti   bs   

String.prototype.format = function() {

    var args = arguments;

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

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

    });

};

String.format = function(template) {

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

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

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

}

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‘));


String Format

标签:style   io   ar   sp   for   on   log   cti   bs   

原文地址:http://my.oschina.net/u/923974/blog/345971

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!