[js]js中类的继承

凡事总有个开端,也有个tag节点(里程碑).阶段性的划分总结,是一种对精神的慰藉,否则精神就像野马一样,会放弃,会累死.

继承:
子类原型指向父类一个实例

类的继承-模拟系统类

Object -> EventTarget -> node
                            1 element -> HTMLElement -> HTMLDivElement -> div
                            2 attr
                            3 text
                            8 comments
                            9 document-> HTMLDocument -> document
<script>
    //    HTMLDivElement > HTMLElement > Element > Node > EventTarget > object
    function myObject() {

    };
    myObject.prototype = {
        hasOwnProperty: function () {
            console.log("myObject");
        }
    };

    function myEventTarget() {

    };

    //子类继承父类: 子类原型指向父类一个实例
    myEventTarget.prototype = new myObject();
    myEventTarget.prototype.sum = function () {
        console.log('myEventTarget...')
    }
</script>

类的继承-自定义类

<script>
    function A() {
        this.x = 100;    
    }
    A.prototype.getX=function () {
        console.log(this.x);
    };


    function B() {
        this.y = 200;
    }
    B.prototype=new A;
    var b = new B;
    console.log(b.__proto__)
</script>

posted @ 2018-02-06 12:15  mmaotai  阅读(189)  评论(0编辑  收藏  举报