返回首页 | 申博娱乐APP下载

合作共赢、快速高效、优质的网站建设提供商

更多精品源码-尽在织梦模板-www.moke8.com

网站开发javascript下string.format函数弥补

时间:2017-09-28 编辑:admin

又从头阅读了一遍Andrew的原文,在原文下面的谈论中,赫然发现也有人早提出参数个数的成果,异常懒散的楼猪直接复制原文谈论答复了一下,一起还发现说漏了很重要的一个留心点Array.prototype.slice。
下面共同弥补说明压服一下:

1、string.format的参数个数
在Andrew的原文中,从前有人指出:

eric d. Hi, thanks for that brilliant article. Made a lot of things a lot clearer!
Note: new RegExp("%([1-" + arguments.length + "])", "g"); will fail passed 9 arguments (the regexp would be "%([1-10])" so it will only match %0 and %1).

I think an easy fix would be something like:
function format(string) { var args = arguments; var pattern = new RegExp("%([0-9]+)", "g"); return String(string).replace(pattern, function(match, index) { if (index == 0 || index = args.length) throw "Invalid index in format string"; return args[index]; }); };
(Sorry for nitpicking, I understand it was only an example and brevety is the main objective, but its a great function to have)

Posted on: January 20th 2009, 12:01 am

这个留言的家伙给足了作者体面,称“I understand it was only an example and brevety is the main objective, but its a great function to have”。正本,原文中界说的正则表达式可以验证的数字规模是...正本如此啊,哈哈,楼猪心虚的笑了。

2、Array.prototype.slice
将arguments变换成Array的挑选是通过Array.prototype.slice.call(arguments);方法变换的,也就是说类数组方法的目标都可以通过slice的方法完结施行到Array的变换,提到类数组目标的变换,真的很有必要从头记载
浏览:

网站建设

流程

    网站建设流程