Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法执行Doug Crockford';s在JSFIDLE中的deentityify示例_Javascript - Fatal编程技术网

Javascript 无法执行Doug Crockford';s在JSFIDLE中的deentityify示例

Javascript 无法执行Doug Crockford';s在JSFIDLE中的deentityify示例,javascript,Javascript,我正在尝试使用JSFIDLE执行Douglas Crockfords J-TBP书中非常好的deentityify示例 String.method('deentityify', function() { // The entity table. It maps entity names to // characters. var entity = { quot: '"', lt: '<', gt: '>'

我正在尝试使用JSFIDLE执行Douglas Crockfords J-TBP书中非常好的deentityify示例

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()); // <">          
String.method('deentityify',function(){
//实体表。它将实体名称映射到
//人物。
var实体={
引用:“”,
lt:'
};
//返回deentityify方法。
返回函数(){
//这是deentityify方法。它调用字符串
//替换方法,查找开始的子字符串
//以“&”结尾,以“;”结尾。如果
//在实体表中,然后替换
//具有表中字符的实体。它使用
//正则表达式(第7章)。
返回此。替换(/&([^&;]+);/g,函数(a,b){
var r=实体[b];
返回类型r=='string'?r:a;
});
};
}());
document.writeln(''''.deentityify());/'但是什么也没有出现。有人想解开这个谜吗

此代码片段依赖于一点糖分,即必须事先定义的
方法。(书中很早就描述了这一点。)一份在线副本和解释在。看起来是这样的:

Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};
您的小提琴的校正版本,包含上述内容,请访问