babel-plugin-transform-proto-to-assign-robust
v6.0.0
Published
This plugin allows Babel to transform all **proto** assignments to a method that will do a shallow copy of all properties with symbol.
Downloads
3
Maintainers
Readme
babel-plugin-transform-proto-to-assign-robust
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);