[英]Unable to execute Doug Crockford's deentityify example in jsfiddle
我正在尝试使用jsfiddle在道格拉斯Crockfords J-TBP书中执行那个非常好的深化示例
String.method('deentityify', function() {
// The entity table. It maps entity names to
// characters.
var entity = {
quot: '"',
lt: '<',
gt: '>'
};
// Return the deentityify method.
return function() {
// This is the deentityify method. It calls the string
// replace method, looking for substrings that start
// with '&' and end with ';'. If the characters in
// between are in the entity table, then replace the
// entity with the character from the table. It uses
// a regular expression (Chapter 7).
return this.replace(/&([^&;]+);/g, function(a, b) {
var r = entity[b];
return typeof r === 'string' ? r : a;
});
};
}());
document.writeln('<">'.deentityify()); // <">
这段代码片段取决于你必须事先定义的一点糖,即method
方法。 (它在书中很早就有描述。)在线复制和解释它在Crockford的文章“JavaScript中的经典继承”的“Sugar”部分 。 它看起来像这样:
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
您的小提琴的更正版本,包含上述内容,请访问http://jsfiddle.net/W9Ncd/ 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.