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( )); // <">
====================================函数调用的时候,内部的函数参数是怎么传递的啊? 比如这里的 function (a,b) a,b的值是指什么?
// 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( )); // <">
====================================函数调用的时候,内部的函数参数是怎么传递的啊? 比如这里的 function (a,b) a,b的值是指什么?
function (a, b) {
var r = entity[b];
return typeof r === 'string' ? r : a;
}
);a是/&([^&;]+);/全部匹配的内容, b是括号中获取的内容,这和是否闭包没有关系,里面的entity[]调用才算是闭包的范畴!< 当前匹配是这个的话,那么
a = '<';
r = 'lt';
上面少写了一个分号,应该是//< 当前匹配是这个的话,那么
a = '<';
r = 'lt';