美文网首页
js中实现多继承(合并多个class)

js中实现多继承(合并多个class)

作者: wolfBite | 来源:发表于2020-06-29 20:13 被阅读0次

    const mixinClass = (base, ...mixins) => {

        const mixinProps = (target, source) => {

          Object.getOwnPropertyNames(source).forEach(prop => {

            if (/^constructor$/.test(prop)) { return; }

            Object.defineProperty(target, prop, Object.getOwnPropertyDescriptor(source, prop));

          })

        };

        let Ctor;

        if (base && typeof base === 'function') {

          Ctor = class extends base {

            constructor(...props) {

              super(...props);

            }

          };

          mixins.forEach(source => {

            mixinProps(Ctor.prototype, source.prototype);

          });

        } else {

          Ctor = class {};

        }

        return Ctor;

      };

    相关文章

      网友评论

          本文标题:js中实现多继承(合并多个class)

          本文链接:https://www.haomeiwen.com/subject/xycwihtx.html