Polyfill for Internet Explorer ECMAScript Set constructor to allow iterable argument

Viewed 1627

I wrote some code using the constructor described in the MDN docs for the Set object. Unfortunately Internet Explorer 11 ignores any iterable argument in the constructor. I had a quick attempt at trying to override the constructor (code below) but with no luck (Set.prototype.size returns - 'this' is not a set object).

var testSet = new Set([0]);
if (testSet.size === 0) {
    //constructor doesnt take an iterable as an argument - thanks IE
    var oldProto = Set.prototype
    Set = function (iterable) {
        if (iterable) {
            iterable.forEach(this.add.bind(this));
        }
    };
    Set.prototype = oldProto;
}

Is there a way to allow the Set constructor to have an iterable argument passed and still work in I.E. ? I guess next best option will be to create some kind of factory method (Set.create) which returns a new Set instance.

1 Answers
Related