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(  ));  // <">
    
    
====================================函数调用的时候,内部的函数参数是怎么传递的啊? 比如这里的 function (a,b) a,b的值是指什么?

解决方案 »

  1.   

    this.replace(/&([^&;]+);/g,
                function (a, b) {
                    var r = entity[b];
                    return typeof r === 'string' ? r : a;
                }
            );a是/&([^&;]+);/全部匹配的内容, b是括号中获取的内容,这和是否闭包没有关系,里面的entity[]调用才算是闭包的范畴!&lt; 当前匹配是这个的话,那么 
    a = '&lt'; 
    r = 'lt';
      

  2.   


    上面少写了一个分号,应该是//&lt; 当前匹配是这个的话,那么  
    a = '&lt;';  
    r = 'lt';
      

  3.   

    看了string.replace()的描述,明白了第二个参数是函数的情况。