Function.prototype.method=function(name, func){
this.prototype[name]=func; // use prototype to add a function
return this;
}
String.method("ping",function(content){ // add method to String
var c=content;
return function(){alert(this + content);} // use closure go get the variable
}("kaqi")); //invoke method in definition
"Hello ".ping()
<<javascript:the good part>> prototype, closure, module example
最新推荐文章于 2026-01-07 00:05:42 发布
本文详细介绍了JavaScript中闭包的概念以及如何使用闭包在字符串对象上扩展方法,通过实例展示了如何创建自定义方法来增强字符串操作能力。

272

被折叠的 条评论
为什么被折叠?



