https://github.com/imcuttle/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
https://github.com/imcuttle/babel-plugin-transform-proto-to-assign-robust
proto-to-assign symbol
Last synced: 9 months ago
JSON representation
This plugin allows Babel to transform all **proto** assignments to a method that will do a shallow copy of all properties with symbol
- Host: GitHub
- URL: https://github.com/imcuttle/babel-plugin-transform-proto-to-assign-robust
- Owner: imcuttle
- License: mit
- Created: 2018-08-18T16:42:50.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-01-07T14:07:44.000Z (over 4 years ago)
- Last Synced: 2025-03-23T02:33:59.157Z (about 1 year ago)
- Topics: proto-to-assign, symbol
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: License
Awesome Lists containing this project
README
# babel-plugin-transform-proto-to-assign-robust
[](https://www.npmjs.com/package/babel-plugin-transform-proto-to-assign-robust)
[](https://www.npmjs.com/package/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`](https://github.com/babel/babel/tree/6.x/packages/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
```javascript
bar.__proto__ = foo
```
- Out
```javascript
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
```javascript
bar.__proto__ = foo
```
- Out
```javascript
;(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);
```