简体   繁体   English

无法在jsfiddle中执行Doug Crockford的深化示例

[英]Unable to execute Doug Crockford's deentityify example in jsfiddle

I am trying to execute that really nice deentityify example in Douglas Crockfords J-TBP book using 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('&lt;&quot;&gt;'.deentityify()); // <">          

This code-snippet depends on a bit of sugar, namely the method method, that you have to define beforehand. 这段代码片段取决于你必须事先定义的一点糖,即method方法。 (It's described early on in the book.) An online copy and explanation of it are in the "Sugar" section of Crockford's article "Classical Inheritance in JavaScript" . (它在书中很早就有描述。)在线复制和解释它在Crockford的文章“JavaScript中的经典继承”的“Sugar”部分 It looks like this: 它看起来像这样:

Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};

A corrected version of your Fiddle, incorporating the above, is at http://jsfiddle.net/W9Ncd/ . 您的小提琴的更正版本,包含上述内容,请访问http://jsfiddle.net/W9Ncd/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM