给String添加方法

<script type="text/javascript">
<!--
Function.prototype.method=function(name,fn){
if(!this.prototype[name]){
this.prototype[name]=fn;
return this;
}
}

String.method("aa",function(){
alert('ffff');
});
String.method("entityify",function(){
var g={
"<":"&lt;",
">":"&gt;",
"&":"&amp;",
'"':"&quot"
};
return this.replace(/[<>&\"]/g,function(c){
alert(c);
return g[c];
});
});

alert("<ga&".entityify()); //显示&lt;ga&amp;
//-->
</script>

posted on 2015-08-18 23:27  allenzhou  阅读(1656)  评论(0)    收藏  举报