函数中的this与 this.prototype
函数中的this添加函数是加在对象上,而this.prototype是添加在原型上,通过prototype的指向来一级一级查找
prototype是构造函数访问原型对象,__proto__是对象实例访问原型对象
Function.prototype.add=function(name,fn){ this.prototype[name]=fn; return this; } var Methods=function(){ this.addObj = function () { console.log("my is add to obj") } }; Methods.add('a', function () { console.log("a") }).add('b', function () { console.log("b")}) console.log(Methods) console.log(Methods.prototype) var che=new Methods(); che.addObj()//"my is add to obj" che.a()//b
console.log(che.constructor) console.log(che.constructor == Methods)//true
che.constructor指向构造函数