Skip to content

imcuttle/babel-plugin-transform-proto-to-assign-robust

Repository files navigation

babel-plugin-transform-proto-to-assign-robust

NPM version NPM Downloads

This plugin allows Babel to transform all proto assignments to a method that will do a shallow copy of all properties with symbol.

Inspired by babel-plugin-transform-proto-to-assign

Why?

When we using es6 class extend syntax in IE<=10 which has not Object.setPrototypeOf and __proto__, so We need use babel-plugin-transform-proto-to-assign to transforming.

  • In
bar.__proto__ = foo
  • Out
var _defaults = function(obj, defaults) {
  var keys = Object.getOwnPropertyNames(defaults)
  for (var i = 0; i < keys.length; i++) {
    var key = keys[i]
    var value = Object.getOwnPropertyDescriptor(defaults, key)
    if (value && value.configurable && obj[key] === undefined) {
      Object.defineProperty(obj, key, value)
    }
  }
  return obj
}

_defaults(bar, foo)

The above transform are worked by babel-plugin-transform-proto-to-assign.

However the _default function DO NOT assign the Symbol static value, but babel-plugin-transform-proto-to-assign-robust do it!

Transform

  • In
bar.__proto__ = foo
  • Out
;(function (obj, defaults) {
  var keys = Object.getOwnPropertyNames(defaults)
  for (var i = 0; i < keys.length; i++) {
    var key = keys[i]
    var value = Object.getOwnPropertyDescriptor(defaults, key)
    if (value && value.configurable && obj[key] === undefined) {
      Object.defineProperty(obj, key, value)
    }
  }
  return obj
})(bar, foo);

About

This plugin allows Babel to transform all **proto** assignments to a method that will do a shallow copy of all properties with symbol

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published