Can one extend the Es6 EventTarget constructor?

830 views
Skip to first unread message

Jan Badenhorst

unread,
May 5, 2018, 6:50:17 AM5/5/18
to Closure Compiler Discuss
How should I extend the ES6 EventTarget constructor?

The following ES6 code is valid - I think. 
(It's just a verbatim copy from the MDN Web Docs (https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/EventTarget)):

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @language_out ES5
// ==/ClosureCompiler==

class MyEventTarget extends EventTarget {
  constructor(mySecret) {
    super();
    this._secret = mySecret;
  }

  get secret() { return this._secret; }
};


but fails to compile cleanly in advanced mode to ES5, giving the warnings:

JSC_TYPE_MISMATCH: actual parameter 2 of $jscomp.inherits does not match formal parameter
found : function(this:EventTarget): ?
required: Function at line 1 character 28
class MyEventTarget extends EventTarget {
^
JSC_CONFLICTING_EXTENDED_TYPE: cannot extend this type; constructors can only extend constructors at line 2 character 2
constructor(mySecret) {
^

When using SIMPLE_OPTIMISATIONS it produces code, but when that code is run in the browser it errors with:

b = new MyEventTarget() 
Uncaught TypeError: Failed to construct 'EventTarget': Please use the 'new' operator, this DOM object constructor cannot be called as a function.
at new MyEventTarget (<anonymous>:52:23)
at <anonymous>:1:5

Am I doing something wrong?

Regards
Jan 

dpa...@chromium.org

unread,
Nov 15, 2018, 5:43:17 PM11/15/18
to Closure Compiler Discuss
See https://github.com/google/closure-compiler/issues/3143. There is a workaround until Closure compiler's externs get fixed.
Reply all
Reply to author
Forward
0 new messages