Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing ES6 Set in javascript

I'm running into problems when trying to inherit from the new Set available in ecmascript 6. The class is defined as such:

function SelectionManager () {
  Set.call(this);
}

SelectionManager.prototype = Object.create(Set.prototype);

SelectionManager.prototype.add = function (unit) {
  unit.setIsSelected(true);
  Set.prototype.add.call(this, unit);
};

/* Some functions left out */

When trying to call add I get the following error: TypeError: Set operation called on non-Set object

The code is available at http://jsfiddle.net/6nq1gqx7/

The draft for ES6 clearly states that it should be possible to subclass Set, what is the correct way of doing so?

like image 924
Linus Unnebäck Avatar asked Dec 20 '25 03:12

Linus Unnebäck


2 Answers

Looks like this is being tracked:

https://github.com/google/traceur-compiler/issues/1413

like image 55
Brian Genisio Avatar answered Dec 21 '25 18:12

Brian Genisio


No correct way for now. Chrome/V8 (as most other browsers) does not yet properly support subclassing of builtins -- in part because it potentially is quite complicated to implement, and in part because the precise semantics are still wildly in flux, and have just been overthrown at the latest ES6 meeting, with no final resolution yet (as of October 2014).

like image 30
Andreas Rossberg Avatar answered Dec 21 '25 17:12

Andreas Rossberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!