1

I have an error with transplied class:

Uncaught TypeError: Failed to construct 'FormData': Please use the 'new' operator, this DOM object constructor cannot be called as a function.

at Form.ExtendableBuiltin (http://local.yandex.ru:30002/bundle.js:79395:14)
at new Form (http://local.yandex.ru:30002/bundle.js:79422:103)

My .babelrc:

{
    "presets": ["react", "latest"],
    "plugins": [
        "babel-plugin-syntax-decorators",
        "babel-plugin-transform-decorators-legacy",
        ["babel-plugin-transform-builtin-extend", { // Class Extending Natives
            globals: ["FormData"],
            approximate: true
        }],
        "transform-es2015-arrow-functions",
        // "syntax-async-functions",
        // "transform-async-to-generator",
        // "transform-regenerator",
        "transform-object-rest-spread",
        "transform-rebem-jsx",
        "transform-es2015-typeof-symbol"
    ],
}

My class:

import map from 'lodash/map'

export default class Form extends FormData {
    constructor (data) {
        super()
        map(data, (val, key) => this.append(key, val))
    }
}

A piece of transpiled code:

function _extendableBuiltin(cls) {
    function ExtendableBuiltin() {
        cls.apply(this, arguments);
    }

Where cls is FormData.

Expected something like this:

function _extendableBuiltin(cls) {
    return function ExtendableBuiltin() {
        return new cls(arguments);
    }

babel --version 6.14.0 (babel-core 6.14.0)

webpack --version Version: webpack 1.13.2

What am I doing wrong?

4

1 回答 1

1

您必须将approximate选项设置为false. 从插件 GitHub 存储库

在不支持重新分配现有对象的原型的旧浏览器上,您需要启用近似模式,这将回退到使用简单 ES5 继承来approximate扩展类的 Babel 5 行为,尽管您的结果可能会有所不同,具体取决于你的目标。

于 2016-09-19T08:44:04.860 回答