-1

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) {

franco
  • 133
  • 2
  • 10

1 Answers1

1

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?

swaggler
  • 583
  • 2
  • 5
  • 13
  • No, is in the "Core Javascript" section :( I think they are related to ecmascript 5 and my browser (FF 10.0.1) is full ecmascript 5 compliant. (as seen on http://kangax.github.com/es5-compat-table/) Thanks for the answare. :-) – franco Feb 12 '12 at 08:12
  • What is this I don't even. `extend` is most certainly not in any JavaScript implementation, not even ES5. `extend` is typically added by libraries to coat the prototype chain (a js feature many consider to be ugly; I do not, but to each his own) with syntactic sugar. – swaggler Feb 12 '12 at 08:31