`

String.prototype.replace(str,func) 例中func的参数问题?

阅读更多
<div id="myDiv"></div>
  <script>
	String.prototype.supplant = function(o){
		return this.replace(/{([^{}]*)}/g,
				function (a,b){
					var r = o[b];
					return typeof r==='string'?r:a;
				}
		);
		
	};

	var template ='<table border="{border}">' +
	'<tr><th>Last</th><td>{last}</td></tr>' +
	'<tr><th>First</th><td>{first}</td></tr>'+
	'</table>';
	var data = {
		first:"Carl",
		last:"Hollywood",
		border:"2"
	};
	myDiv.innerHTML = template.supplant(data);
  </script>


请问一下
function (a,b){
			var r = o[b];
			return typeof r==='string'?r:a;
		}

的a,b是怎样传过来的呢?
分享到:
评论
2 楼 falchion 2008-02-29  
多谢了!  终于找到了

引用
replace() 方法的参数 replacement 可以是函数而不是字符串。在这种情况下,每个匹配都调用该函数,它返回的字符串将作为替换文本使用。该函数的第一个参数是匹配模式的字符串。接下来的参数是与模式中的子表达式匹配的字符串,可以有 0 个或多个这样的参数。接下来的参数是一个整数,声明了匹配在 stringObject 中出现的位置。最后一个参数是 stringObject 本身。
1 楼 boin 2008-02-27  
a,b 是正则表达式捕获的子匹配

请搜索JS正则表达式相关的内容。

相关推荐

    你必须知道的495个C语言问题(PDF)

    2.10 我的编译器在结构中留下了空洞, 这导致空间浪费而且无法与外 部数据文件进行”二进制” 读写。能否关掉填充, 或者控制结构域 的对齐方式? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.11...

    Function.prototype.apply()与Function.prototype.call()小结

    老是忘掉这两个东东的用下,写下来做个记录吧...func.apply(null, [1,2,3]); call call传入的参数不固定,和apply相同的是,第一个参数也是代表函数体内的this指向,第二个参数开始往后,每个参数被依次传入函数: v

    深入分析js中的constructor和prototype

    我们在定义函数的时候,函数定义的时候函数本身就会默认有一个prototype的属性,而我们如果用new 运算符来生成一个对象的时候就没有prototype属性。我们来看一个例子,来说明这个 代码如下: function a(c){ this.b =...

    慎用 somefunction.prototype 分析

    } Person.prototype.SayHello = function() { alert&#40;‘Hello, ‘ + this.Name&#41;; } Person.prototype.SayBye = function() { alert&#40;‘Goodbye, ‘ + this.Name&#41;; } 不过,有的时候,为了书写以及...

    C#常用功能类库(20111202最新整理)

    10.SR.ShareFunc.StringFunc.ValidateUnUsedCode 常用验证非法字符、格式转化 五、SR.ShareFunc.WinFunc 11.SR.ShareFunc.WinFunc.FileFunc 11.1 系统垃圾文件清理 11.2 文件的文件名、后缀名、文件路径的处理 ...

    Javascript如何实现扩充基本类型

    可以通过给Function.prototype增加方法来使得该方法对所有函数可用。 通过给Function.prototype增加一个method方法,下次给对象增加方法的时候就不必键入prototype这几个字符了。 Function.prototype.method=...

    func_DWT.zip_DWT function_func_DWT

    function in platform to use this method discrete wavelet transform

    simplewebrtc.js webrtc网页视频开发帮助工具

    func._groupName = group; (this.callbacks[event] = this.callbacks[event] || []).push(func); return this; }; // Adds an `event` listener that will be invoked a single // time then automatically ...

    JS trim去空格的最佳实践

    他对 String 对象扩展了一个 trim() 方法: 代码如下: Function.prototype.method = function(name, func) { this.prototype[name] = func; return this; }; String.method(‘trim’, function() { return this....

    proposal-promise-finally:ECMAScript Promise.prototype.finally的提案,规格和参考实现

    ECMAScript Promise.prototype.finally提案,规格和参考实现规范由起草,遵循。 在上获取polyfill / shim。 该提案目前是。基本原理许多promise库都有一个“ finally”方法,用于注册在promise被实现(实现或拒绝)...

    func2func.m v1.0(2009 年 7 月):更新匿名函数工作区。 STR2FUNC(FUNC2STR(f)) 问题的解决方法。-matlab开发

    这有时是不可取的,我们猜测通过使用&gt;&gt; G = str2func(func2str(F)); 我们解决了这个问题,但随后收到警告&gt;&gt; G(2) ??? 未定义的函数或方法 '@(x)A*x' 用于输入类型为“ double”的参数。 因此,被迫使用 EVAL ...

    python定时任务.docx

    7 print "now excuted func is %s"%string1 8 9 print "start" 10 schedule.enter(2,0,func,(1,)) 11 schedule.enter(2,0,func,(2,)) 12 schedule.enter(3,0,func,(3,)) 13 schedule.enter(4,0,func,(4,)) ...

    IOS页面跳转动画实现

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -&gt; NSTimeInterval { return 1.0 } func animateTransition(transitionContext: UIViewControllerContextTransitioning) ...

    EasyUIImagePickerController

    func selectImageFinishedAndCaches(image: UIImage, cachesIdentifier: String, isCachesSuccess: Bool) { if isCachesSuccess == true { imageView.image = image } self.dismissViewControllerAnimated(true,...

    func-params:运行时函数参数名称确定

    潜在的问题是,除了 ECMAScript 2015 Function.prototype.toString()之外,没有官方的 JavaScript API 来确定运行时函数的参数名称。 该库通过解析Function.prototype.toString()的源代码来确定函数参数的名称。 它...

    function foo的原型与prototype属性解惑

    foo.prototype === Function.prototype &#41;; //false。 当时一直没想明白为啥foo的原型不是Function.prototype。 下面例子让我想当然的认为o.prototype === Function.prototype 应该为true的: 代码如下: function...

    JS仿Base.js实现的继承示例

    本文实例讲述了JS仿Base.js实现的继承。分享给大家供大家参考,具体如下: var Klass = function() {}; Klass.extendClass = (function() { var F = function() {}; return function(C, P) { ...Klass.extend = func

    func包文件.ipynb

    func包文件

    javascript 精粹笔记

    //为构造函数原型添加方法 Function.method=function(name,func){ this.prototype.name=func; } Number.method(“integer”,function(){ return Math[this &lt; 0 ? ‘ceil’:’floor’](this); }); (-10/3).integer...

    实验七-终端设备的控制.zip

    实验七-终端设备的控制,一、问题回答 1.在原始代码中,按下F12,中断响应后,中断服务程序会调用func?它实现的是什么功能? 答:它会通过func_table调用show_stat显示当前进程的信息。 2.在你的实现中,是否把...

Global site tag (gtag.js) - Google Analytics