JS手写面试题 --- 寄生组合继承


JS手写面试题 --- 寄生组合继承

题目描述:实现一个你认为不错的 js 继承方式

实现代码如下:

    function Parent(){
        this.name = name;
        this.say = () => {
            console.log(111);
        };
    }

    Parent.prototype.play = () => {
        console.log(222)
    };

    function Children(name) {
        Parent.call(this);
        this.name = name;
    }

    Children.prototype = Object.create(Parent.prototype);
    Children.prototype.constructor = Children;

    let child = new Children("111");
    console.log(child); // Children {name: "111", say: ?}
    console.log(child.name); // 111
    child.say(); // 111
    child.play(); // 222

请忽略下面的内容!

【投稿说明】
博客园是面向开发者的知识分享社区,不允许发布任何推广、广告、政治方面的内容。
博客园首页(即网站首页)只能发布原创的、高质量的、能让读者从中学到东西的内容。
如果博文质量不符合首页要求,会被工作人员移出首页,望理解。如有疑问,请联系contact@cnblogs.com。【投稿说明】
博客园是面向开发者的知识分享社区,不允许发布任何推广、广告、政治方面的内容。
博客园首页(即网站首页)只能发布原创的、高质量的、能让读者从中学到东西的内容。
如果博文质量不符合首页要求,会被工作人员移出首页,望理解。如有疑问,请联系contact@cnblogs.com。

相关