一只特立独行的猪 一只特立独行的🐷
一只特立独行的🐷
  • 主页
  • web开发
  • 博客笔记
  • 软件工具
  • 随笔杂谈
  • 文章归档
  • 影音欣赏
  • 我的资源
  • 关于我
  • wechat_fill

js面向对象_基于组合的方式实现继承

  • 2015-11-05
  • Web开发
  • 评论(0)
 
js面向对象_基于组合的方式实现继承
<script type="text/javascript">
    /**
     * 组合的实现方式是属性通过伪造的方式实现,方法通过原型链的方式实现
     * 注意内存模型
     */
    function Parent(name) {
        this.color = ["red","blue"];
        this.name = name;
    }
    Parent.prototype.ps = function() {
        alert(this.name+"["+this.color+"]");
    }

    function Child(name,age) {
        //已经完成了伪造
        Parent.call(this,name);
        this.age = age;
    }
    Child.prototype = new Parent();
    Child.prototype.say = function() {
        alert(this.name+","+this.age+"["+this.color+"]");
    }

    var c1 = new Child("Leon",22);
    c1.color.push("green");
    c1.say();
    c1.ps();
    var c2 = new Child("Ada",23);
    c2.say();
    c2.ps();
    </script>
 

 
Powered by Wordpress, Theme by Wing-magic
©2023 一只特立独行的🐷 All rights reserved
陕ICP备15006707号-5