[英]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.