扩展抽象类问题 - javascript
我需要在 javascript 中扩展一个抽象类。 我已经购买了 Javascript 权威指南 - O'Reilly 有一些js中OOP的例子。 我尝试执行其中一个示例来扩展抽象类, 但我收到“AbstractSet.extend 不是函数”。 有人可以帮助我理解这个问题吗? 谢谢
function abstractmethod() { throw new Error("abstract method"); }
function AbstractSet() { throw new Error("Can't instantiate abstract classes");}
AbstractSet.prototype.contains = abstractmethod;
var NotSet = AbstractSet.extend(
function NotSet(set) { this.set = set; },
{
contains: function(x) { return !this.set.contains(x); },
toString: function(x) { return "~" + this.set.toString(); },
equals: function(that) {
return that instanceof NotSet && this.set.equals(that.set);
}
}
);
--- Firebug 控制台 AbstractSet.extend 不是函数 等于:函数(那个){
I need to extend an abstract class in javascript.
I've buy the Javascript The Definitive Guide - O'Reilly
there are some examples of OOP in js.
I've tried to execute one of the example to extends an abstract class,
but i receive the "AbstractSet.extend is not a function".
Can someone help me to understand the problem?
Thanks
function abstractmethod() { throw new Error("abstract method"); }
function AbstractSet() { throw new Error("Can't instantiate abstract classes");}
AbstractSet.prototype.contains = abstractmethod;
var NotSet = AbstractSet.extend(
function NotSet(set) { this.set = set; },
{
contains: function(x) { return !this.set.contains(x); },
toString: function(x) { return "~" + this.set.toString(); },
equals: function(that) {
return that instanceof NotSet && this.set.equals(that.set);
}
}
);
--- Firebug console
AbstractSet.extend is not a function
equals: function(that) {
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有普通的 JavaScript
extend
方法,因此会出现错误。这个代码是直接从书上抄来的吗?作者可能指的是jQuery的extend方法吗?
There is no vanilla JavaScript
extend
method, hence the error.Is this code copied directly from the book? Could the author be referring to jQuery's extend method?