Js语言精粹经典用法
1. 扩充类型的功能
Function.prototype.method = function(name, func){
if(!this.prototype[name]){
this.prototype[name] = func;
}
return this;
}
String.method('deentityfy',function(){
var entity={
quot:'"',
lt:'<',
gt:'>'
};
return function(){
return this.replace(/&([^&;]+);/g,
function(a,b){
var r = entity[b];
return typeof r==='string' ? r:a;
});
};
}());
console.log('<>'.deentityfy()); // <>

浙公网安备 33010602011771号