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()); // <>
时间: 2024-10-09 20:25:39