首页 > 网站 > WEB开发 > 正文

javascript 之string.format

2024-04-27 14:20:48
字体:
来源:转载
供稿:网友

javascript 之string.format

function(){    'use strict';    if(String.PRototype.format)        return;            String.prototype.format = function(args){            var _dict = typeof(args) == 'object' ? args : arguments;                return this.replace(/{([^{}]+)}/g,function(s,n){            return _dict[n]===undefined?s:_dict[n];        });        };    })();

useage:

var str = '{name} is {age} years old.';str.format({name:'Tom',age:25});//”Tom is 25 years old.”
str = '{0} is {1} years old.';str.format('Jerry',15);//”Jerry is 15 years old.”str.format(['Bill',10]);//"Bill is 10 years old."

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表