【发布时间】:2014-04-07 09:42:40
【问题描述】:
我在查看Javascript the good parts 时遇到了这个例子,作者试图解释如何能够调用超类:
Object.method('superior', function (name) {
var that = this,
method = that[name];
return function ( ) {
return method.apply(that, arguments);
};
});
使用此代码的示例:
super_get_name = that.superior('get_name');
但是 chrome 无法识别 Object 上的 method。我尝试使用defineProperty 做同样的事情,但这也没有用.. 有什么帮助吗?
更新: 此方法是否会导致与 jQuery 的任何已知冲突?只要我将以下内容放在页面中的第一个 js 文件中:
我在 jquery-1.11.0.js 的第 2062 行收到此错误:
Uncaught TypeError: Object function (name) {
var that = this,
method = that[name];
return function ( ) {
return method.apply(that, arguments);
};
} has no method 'exec'
这是受影响的代码:
// Filters
for ( type in Expr.filter ) {
if ( (match = matchExpr[ type ].exec( soFar )) && (!preFilters[ type ] ||
(match = preFilters[ type ]( match ))) ) {
matched = match.shift();
tokens.push({
value: matched,
type: type,
matches: match
});
soFar = soFar.slice( matched.length );
}
}
知道发生了什么吗?
【问题讨论】:
-
在那本书的某处他确实定义了一个自定义
method函数。使用它! -
@Bergi 似乎使用它会弄乱我的 jquery .. 有没有办法避免它?
标签: javascript oop design-patterns