原型链与面向对象

      //创建对象有几种方法
      var o1={name:'o1'};
      var o11=new Object({name:'o11'});

      var M=function(){this.name='o2'};
      var o2=new M();

      var p={name:'o3'};
      var o3=Object.create(p);

      // 原型、构造函数、实例、原型链
      // 构造函数(prototype)->原型对象(__proto__)->原型对象
      //     |                      |
      //    new()                constructor()
      //    实例                  构造函数

      // instanceof的原理
      // 判断实例是否属于某个对象
      // javascript中的对象都有一个__proto__属性,这个是对象的隐式原型,指向该对象的父对象的原型(prototype)。显式的原型对象使用prototype,但是Object.prototype.__proto__=null;

      // 判断某个对象a是否属于某个类A的实例,可以通过搜索原型链。

      // 实例对象属性查找顺序是:实例对象内部---->构造函数原型链---->实例对象父对象的原型链。


      //继承机制
      function A(){
      }
      A.prototype.name='licui';

      function B(){

      }
      B.prototype = new A();

      var a = new A();
      var b = new B();
      //b.name = 'hello';

      console.log('A:',A);
      console.log('B:',B);
      console.log('a:',a);
      console.log('b:',b);

      console.log('A.prototype',A.prototype);
      console.log('B.prototype',B.prototype);
      console.log('a._proto_',a.__proto__);
      console.log('b._proto_',b.__proto__);

      console.log('a instanceof A:',a instanceof A);
      console.log('a instanceof Object:',a instanceof Object);
      console.log('b instanceof B:',b instanceof B);
      console.log('b instanceof A:',b instanceof A);
      console.log('b instanceof Object:',b instanceof Object);



      // new运算符
      // 创建对象
      /**
       * 类的声明
       */
      var Animal = function () {
          this.name = 'Animal';
      };

      /**
       * es6中class的声明
       */
      class Animal2 {
          constructor () {
              this.name = 'Animal2';
          }
      }

      /**
       * 实例化
       */
      console.log(new Animal(), new Animal2());

      /**
       * 借助构造函数实现继承
       */
      function Parent1 () {
          this.name = 'parent1';
      }
      Parent1.prototype.say = function () {
        console.log(123)

      };
      function Child1 () {
          Parent1.call(this);
          this.type = 'child1';
      }
     // console.log(new Child1(), new Child1().say());//只能继承构造函数属性和方法
     // 无法实现函数复用,每个子类实例都持有一个新的fun函数,太多了就会影响性能,占用内存多!

      /**
       * 借助原型链实现继承
       */
      function Parent2 () {
          this.name = 'parent2';
          this.play = [1, 2, 3];
      }
      function Child2 () {
          this.type = 'child2';
      }
      Child2.prototype = new Parent2();

      var s1 = new Child2();
      var s2 = new Child2();
      s1.play.push(4);

      console.log(s1.play, s2.play);//属性实例共享,更改一个,其中一个也会更改
     // s1.play.push(4);

      /**
       * 组合方式
       */
      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
       * @type {String}
       */
      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();
      console.log(s5, s6);

      console.log(s5 instanceof Child4, s5 instanceof Parent4);
      console.log(s5.constructor);

      /**
       * 组合继承的优化2
       */
      function Parent5 () {
          this.name = 'parent5';
          this.play = [1, 2, 3];
      }
      function Child5 () {
          Parent5.call(this);
          this.type = 'child5';
      }
      Child5.prototype = Object.create(Parent5.prototype);
      Child5.prototype.constructor=Child5;

      var s7=new Child5();
      console.log(s7 instanceof Child5,s7 instanceof Parent5);
      console.log(s7.constructor);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值