1

Using this recomentations https://stackoverflow.com/a/15192747/3620727 I tried extand FormData to implement a new method, but it's not working. Follow my code:

function Form(form){
    FormData.apply(this, form);
}

Form.prototype = new FormData();
Form.prototype.constructor = Form;

The console shows me this message:

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

WHere am I going wrong?

Community
  • 1
  • 1
Doglas
  • 642
  • 1
  • 11
  • 22
  • The error message is rather weird, which browser? Anyway, try fixing the second argument of apply, it has to be an Array. You should also use `Form.prototype = Object.create(FormData.prototype)`. – plalx Feb 23 '16 at 18:48
  • I changed `Form.prototype = Object.create(FormData.prototype);` and `FormData.apply(this, [form]);` But the error is exatly the same... – Doglas Feb 23 '16 at 19:00
  • Where are you executing this code? Can I see the definition of FormData? – plalx Feb 23 '16 at 19:05
  • In fact, I do not set FormData, its definition is native from javascript [DataForm Reference](https://developer.mozilla.org/pt-BR/docs/Web/API/FormData) obs: I don't know if do this with a native 'class' is a absurd – Doglas Feb 23 '16 at 19:11
  • 1
    Right, I totally forgot about that. Native classes can hardly be extended because their constructors are usually factories (perhaps always). Therefore, they always return a new instance and do not apply mutations to `this`. Your error message is weird however... your code produces no errors in IE11, although it doesn't work as expected because of the above reasons. – plalx Feb 23 '16 at 19:44

0 Answers0