/* //这样做并不好,污染了原生的Function Function.prototype.checkEmail = function(){ //验证邮箱 console.log(1); } var fn = function(){}; fn.checkEmail(); */ //可以添加统一添加方法的功能方法 /* Function.prototype.addMethod = function(name,fn){ this[name] = fn; } var fn = function(){}; fn.addMethod('checkName',function(){ console.log('验证姓名'); }); fn.addMethod('checkEmail',function(){ console.log('验证邮箱'); }); fn.checkName(); fn.checkEmail(); */ //链式添加&&链式调用 /* Function.prototype.addMethod = function(name,fn){ this[name] = fn; return this; } var fn = function(){}; fn.addMethod('checkName',function(){ console.log('检查名字'); return this; }).addMethod('checkEmail',function(){ console.log('检查邮件'); return this; }); fn.checkName().checkEmail(); */ //类式调用方式 Function.prototype.addMethod = function(name,fn){ this.prototype[name] = fn;//赋值到对象的原型上 } var fn = function(){}; fn.addMethod('checkName',function(){ console.log('检查名字'); }); fn.addMethod('checkEmail',function(){ console.log('检查邮箱'); }); var methods = new fn(); methods.checkName(); methods.checkEmail();