美文网首页前端笔记本
js 面向对象(2)

js 面向对象(2)

作者: 大乔是个美少女 | 来源:发表于2018-01-24 10:27 被阅读0次
        // 组合方式
        function Parent3 () {
            this.name = 'Parent3';
            this.play = [1, 2, 3];
        }
        function Child3 () {
            Parent3.call(this); // 父级的构造函数执行了两次
            this.type = 'child3';
        }
        Child3.prototype = new Parent3();

        var s3 = new Child3();
        var s4 = new Child3();
        s3.play.push(4);
        console.log(s3.play, s4.play);

        // 组合继承优化1
        function Parent4 () {
            this.name = 'Parent4';
            this.play = [1, 2, 3];
        }
        function Child4 () {
            Parent4.call(this);
            this.type = 'child4';
        }
        Child4.prototype = Parent4.prototype; // 引用类型

        var s5 = new Child4();
        var s6 = new Child4();
        s5.play.push(4);
        console.log(s5, s6);
        console.log (s5 instanceof Parent4);
        console.log (s5 instanceof Child4);
        // 如何判断s5是child4的实例还是parent4的实例
        console.log (s5.constructor)
        // 从父类的原型链中继承了constructor

        // 组合继承优化2
        function Parent5 () {
            this.name = 'Parent5';
            this.play = [1, 2, 3];
        }
        function Child5 () {
            Parent4.call(this);
            this.type = 'child5';
        }
        Child5.prototype = Object.create(Parent5.prototype); // 引用类型 父子原型链隔离
        Child5.prototype.constructor = Child5; // 重写child5的构造函数
        var s7 = new Child5();
        var s8 = new Child5();
        s7.play.push(4);
        console.log(s7, s8);
        console.log (s7.constructor);
    </script>
image.png

相关文章

  • JS面向对象精要(二)_函数

    JS面向对象精要(一)_原始类型和引用类型JS面向对象精要(二)_函数JS面向对象精要(三)_理解对象JS面向对象...

  • JS面向对象精要(三)_理解对象

    JS面向对象精要(一)_原始类型和引用类型JS面向对象精要(二)_函数JS面向对象精要(三)_理解对象JS面向对象...

  • JS面向对象精要(四)_构造函数和原型对象

    JS面向对象精要(一)_原始类型和引用类型JS面向对象精要(二)_函数JS面向对象精要(三)_理解对象JS面向对象...

  • JS面向对象精要(五)_继承

    JS面向对象精要(一)_原始类型和引用类型JS面向对象精要(二)_函数JS面向对象精要(三)_理解对象JS面向对象...

  • JS面向对象

    JS面向对象入门 1、面向对象语言概念面向对象语言主要包括 类、对象、封装、多肽。2、面向对象的编程思想面向过程思...

  • 创建js对象_new+构造函数

    1、js面向对象 2、new构造函数创建

  • JS第四天

    一、面向对象JS JS面向对象初始 1、属性与方法 使用属性解决循环绑定变量污染 2、类字典结构使用 结构 拓展 ...

  • js对象构建

    1首先明确一点js不是面向对象的语音,但js具有极大的灵活性可以模拟面向对象,甚至面向过程乃至全世界。 2 js中...

  • js面向对象(2)

    4.js里面的继承

  • js 面向对象(2)

网友评论

    本文标题:js 面向对象(2)

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