【发布时间】:2013-11-20 02:28:32
【问题描述】:
浏览 Javascript: The Good Parts 5.4 章的示例代码,以下用于演示使用函数式模式调用超级方法:
Object.method('superior', function (name) {
var that = this, method = that[name];
return function () {
return method.apply(that, arguments);
};
});
这将按如下方式使用(其中“cat”是另一个定义了“get_name”函数的构造函数):
var coolcat = function (spec) {
var that = cat(spec),
super_get_name = that.superior('get_name');
that.get_name = function (n) {
return 'like ' + super_get_name( ) + ' baby';
};
return that;
};
但是在运行示例代码时,F12 工具显示如下:
Uncaught TypeError: Object function Object() { [native code] } has no method 'method'.
我在这里错过了什么?
【问题讨论】:
-
您是否忘记在书中添加
Function.prototype.method = function (name, func) { this.prototype[name] = func; return this; };?
标签: javascript