if (typeof Function.prototype.method !== 'function') {
Function.prototype.method = function(name,implementation){
console.log(name);
Function.prototype[name] = implementation;
return this;
}
}
var Person = function(name) {
// body...
this.name = name;
}.method('getName',function(){
return this.name;
}).method('setName',function(){
this.name = name;
return this;
});
var ab = new Person('hehhh');
console.log(ab);
console.log(typeof ab);
ab.getName(); // 这个地方出错了,囧
ab.setName('eve').getName();
console.log(typeof Function.prototype.setName);
console.log(typeof Function.prototype.getName);
请问一下,为什么我注释这个地方会出错。出错情况如标题。
javascript Uncaught TypeError: undefined is not a function
一般不建议使用
Function.prototype
,可以试下用这种方式定义: