2

I can see here that getters are not supported in IE.

How can I know if some polyfills/Babel plugins exist so I can use it in my webapp?

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
IsraGab
  • 4,819
  • 3
  • 27
  • 46
  • 2
    You can't polyfill syntax. You'd have to compile your new JavaScript to old with e.g. babel. – Jared Smith Aug 28 '18 at 20:37
  • 2
    Are you using computed property names? Otherwise, getters have been supported since IE 9. – John Montgomery Aug 28 '18 at 20:37
  • Refer to here also: https://stackoverflow.com/questions/1077106/javascript-getters-setters-in-ie – Chris Cousins Aug 28 '18 at 20:37
  • I can't see in babel website that they're compiling getters & setters – IsraGab Aug 28 '18 at 20:38
  • As noted in the very page you linked, unless you absolutely require to use `get`, the `defineProperty()` method has very similar behaviour and is supported across all browsers and versions. – Samuel Hulla Aug 28 '18 at 20:47
  • You're right, maybe I will use the defineProperty() method, but I also asking in a more global way. When some functionality is not supported how can I know if a pollyphill or babel can help – IsraGab Aug 28 '18 at 20:49
  • It's a babel plugin for this -> https://babeljs.io/docs/en/babel-plugin-transform-es5-property-mutators.html – Keith Aug 28 '18 at 21:02
  • @IsraGab in general the best way to do this is to use something like [Modernizr](https://modernizr.com/) to figure out what you need. I generally have around 3-4 "tiers" of polyfill bundles and load them based on need. – Jared Smith Aug 29 '18 at 12:06

1 Answers1

1

You must be misunderstood something. On page you've mentioned it says, that getters/setters are supported starting from IE 9 (so, should be also available in IE 11). Check Browser compatibility section.

However get/set syntax is just Syntactic sugar over old Object.defineProperty(). For example:

var bValue = 38;
var o = {
  get b () { return bValue; },
  set b (newValue) { bValue = newValue; }
}

equals to:

var o = {}; // Creates a new object
var bValue = 38;
Object.defineProperty(o, 'b', {
  get() { return bValue; },
  set(newValue) { bValue = newValue; },
  enumerable: true,
  configurable: true
});
console.log(o.b); // 38

So, you can use that, if you really need it like that.

As Jared Smith well pointed it's technically possible to detect at runtime wether new syntax is supported by using eval (another option would be to pass your code as a string into new Function). Anyway, it's not the best way of dealing with old browsers, so if you want to write your code using ES6 syntax, but be able to use it in older browsers, use tools like babel, which will transform your code properly, so it will be supported by older browsers.

Ihor
  • 3,219
  • 2
  • 18
  • 20
  • 1
    Note that it *is* technically possible to check if a syntax is available at runtime using `eval` (and this is arguably one of the few legitimate use cases for `eval`). – Jared Smith Aug 29 '18 at 12:08