即能加在函数也能加在原型

Function.prototype.addMethod = function (name, fn,proto) {
// console.log(proto)
if (proto===true){
this[name] = fn;
}else{
this.prototype[name] = fn;
}
 
return this;
}

var methods = function () { };
// console.log(Methods.addMethod)
methods.addMethod('chek1', function () {
console.log('chek1')
return this;
}).addMethod('chek2', function () {
console.log('chek2')
return this;
}).addMethod('chek3', function () {
console.log('chek3')
return this;
},true)
methods.chek3();
// methods.chek1();//this[name] = fn;
var m = new methods;//this.prototype[name] = fn;
m.chek3();
原文地址:https://www.cnblogs.com/dianzan/p/10790040.html