[英]TypeError: Object function Object() { [native code] } has no method 'observe'
[英]TypeError: Object function Object() { [native code] } has no method 'method'
仔細閱讀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工具顯示以下內容:
未捕獲的TypeError:對象函數Object(){[native code]}沒有方法'method'。
我在這里想念什么?
道格拉斯·克羅克福德(Douglas Crockford)使用以下內容(在本書第4頁中定義)
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
這是因為在您的代碼中未定義method method
,因此請在本書中查找作者定義method method
。
顯然@Andreas已經找到了該方法,現在我記得了。
使用method method
,以便在任何對象上調用該方法時,都在該對象上定義一個方法,其中該方法的name
是傳遞給method
的name
參數,而該method
的實現是func
函數參數。
您需要將此內容包含在控制台中才能正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.