You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by su...@apache.org on 2020/06/10 08:40:41 UTC

[incubator-echarts] branch next updated: ts: fix es class usage for inheritance.

This is an automated email from the ASF dual-hosted git repository.

sushuang pushed a commit to branch next
in repository https://gitbox.apache.org/repos/asf/incubator-echarts.git


The following commit(s) were added to refs/heads/next by this push:
     new fbdb003  ts: fix es class usage for inheritance.
fbdb003 is described below

commit fbdb003fcab54784463370e38f988c3b5b5c432c
Author: 100pah <su...@gmail.com>
AuthorDate: Wed Jun 10 15:13:53 2020 +0800

    ts: fix es class usage for inheritance.
---
 src/util/clazz.ts | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/src/util/clazz.ts b/src/util/clazz.ts
index ddf3549..d1a03ed 100644
--- a/src/util/clazz.ts
+++ b/src/util/clazz.ts
@@ -99,17 +99,26 @@ export function enableClassExtend(rootClz: ExtendableConstructor, mandatoryMetho
         // constructor.
         // If this constructor/$constructor is declared, it is responsible for
         // calling the super constructor.
-        const ExtendedClass = (class {
-            constructor() {
-                if (!proto.$constructor) {
+        function ExtendedClass(this: any, ...args: any[]) {
+            if (!proto.$constructor) {
+
+                if (!isESClass(superClass)) {
+                    // Will throw error if superClass is an es6 native class.
                     superClass.apply(this, arguments);
                 }
                 else {
-                    proto.$constructor.apply(this, arguments);
+                    const ins = zrUtil.createObject(
+                        // @ts-ignore
+                        ExtendedClass.prototype, new superClass(...args)
+                    );
+                    return ins;
                 }
             }
-            static [IS_EXTENDED_CLASS] = true;
-        }) as ExtendableConstructor;
+            else {
+                proto.$constructor.apply(this, arguments);
+            }
+        }
+        ExtendedClass[IS_EXTENDED_CLASS] = true;
 
         zrUtil.extend(ExtendedClass.prototype, proto);
 
@@ -119,10 +128,15 @@ export function enableClassExtend(rootClz: ExtendableConstructor, mandatoryMetho
         zrUtil.inherits(ExtendedClass, this);
         ExtendedClass.superClass = superClass;
 
-        return ExtendedClass as ExtendableConstructor;
+        return ExtendedClass as unknown as ExtendableConstructor;
     };
 }
 
+function isESClass(fn: unknown): boolean {
+    return typeof fn === 'function'
+        && /^class\s/.test(Function.prototype.toString.call(fn));
+}
+
 /**
  * A work around to both support ts extend and this extend mechanism.
  * on sub-class.


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org