作家
登录

Prototype源码浅析 String部分(四)之补充

作者: 来源:www.28hudong.com 2013-03-30 00:35:10 阅读 我要评论

替换 interpolate | sub | scan | truncate | gsubinterpolate : 将字符串看作一个模板,并使用 object 的属性填充它。 sub : 将字符串中前指定个个与 pattern 指定的模式匹配的子串用 replacement 替换 scan : 遍历字符串中与参数 pattern 指定的模式匹配的所有子串。返回原始字符串本身。 truncate : 将字符串截短为指定的长度(包含后缀部分), 并添加一个后缀。 gsub :将字符串中所有与 pattern 指定的模式匹配的值全部用 replacement 替换掉 上面的方法中,最重要的一个方法是gsub,具体说明参见《浅析Prototype的模板类--Template》 sub除了可以限定次数外,其他与gsub完全一致。 复制代码 代码如下: function sub(pattern, replacement, count) { replacement = prepareReplacement(replacement); count = Object.isUndefined(count) ? 1 : count; return this.gsub(pattern, function(match) { if (--count < 0) return match[0]; return replacement(match); }); } scan也是一样的,不过scan最后返回的是字符串本身而已。 interpolate 是将字符串当做模板来用,核心还是gsub truncate 是唯一有点区别的(我现在依稀感觉我分错类了)。 以字符串'fuck the gfw'为例,truncate 的执行'fuck the gfw'.truncate(10,'****')的步骤是: 1、获得前面10 - '****'.length个字符 'fuck t' 2、拼上后缀'****',得到 'fuck t****',长度为10. 处理很简单,源码也简单: 复制代码 代码如下: function truncate(length, truncation) { length = length || 30;//默认长度30 truncation = Object.isUndefined(truncation) ? '...' : truncation;//默认后缀... return this.length > length ? this.slice(0, length - truncation.length) + truncation : String(this); } 另:Prototype的一个方便之处就是随时可以抽取有用的代码作为单独的部分或者收为自己用。下面是单独提出来的模板方法。 复制代码 代码如下: function Template(template, pattern){ this.template = template; this.pattern = pattern || /(^|.|r|n)(#{(.*?)})/; } Template.prototype = (function(){ function evaluate(obj){ return gsub.call(this,function(match){ if(obj == null){ return match[0] + ''; } var before = match[1] || ''; if(before == '\'){ return match[2]; } var ctx = obj; var expr = match[3]; var pattern = /^([^.[]+|[((?:.*?[^\])?)])(.|[|$)/; match = pattern.exec(expr); if (match == null){ return before; } while (match != null) { var comp = match[1].search(/^[/) != -1 ? match[2].replace(/\\]/g, ']') : match[1]; ctx = ctx[comp]; if (null == ctx || '' == match[3]) break; expr = expr.substring('[' == match[3] ? match[1].length : match[0].length); match = pattern.exec(expr); } return before + (ctx === null ? '' : String(ctx)); }); } function gsub(replacement){ var pattern = this.pattern; var result = ''; var match = null; var source = this.template; if (!(pattern.length || pattern.source)) { replacement = replacement(''); return replacement + source.split('').join(replacement) + replacement; } while (source.length > 0) { if (match = source.match(pattern)) { result += source.slice(0, match.index); result += replacement(match) === null ? '' : String(replacement(match)); source = source.slice(match.index + match[0].length); }else { result += source; source = ''; } } return result; } return { constructor : Template, evaluate : evaluate } })(); 复制代码 代码如下: var template = new Template('my age is : #{name.age}'); console.log(template.evaluate({name : {age : 24}}));//my age is : 24 String部分(完)

  推荐阅读

  jQuery-Easyui 1.2 实现多层菜单效果的代码

11年9月份项目刚开始时,找到了园中的疯狂秀才。因为秀才上面的Demo没有多级菜单。也是第一次接触Easyui。好多不是明白。不过后来我们叁还是捣鼓出来了。但是发现我们的项目用不了。就放弃了!一直搁在那……还好最>>>详细阅读


本文标题:Prototype源码浅析 String部分(四)之补充

地址:http://www.17bianji.com/kaifa2/JS/23721.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)