javascript面向对象之继承

<script language="JavaScript" type="text/javascript">
<!--
        //定义extend方法
        Object.extend = function (destination, source) {
            for (property in source) {
                destination[property] = source[property];
            }
            return destination;
        }
        Object.prototype.extend = function (object) {
            return Object.extend.apply(this, [this, object]);
        }
        //定义class1
        function class1() {
            //构造函数
        }
        //定义类class1的成员
        class1.prototype = {
            method: function () {
                alert("class1");
            },
            method2: function () {
                alert("method2");
            }

        }
        //定义class2
        function class2() {
            //构造函数
        }
        //让class2继承于class1并定义新成员
        class2.prototype = (new class1()).extend({
            method: function () {
                alert("class2");
            }
        });

        //创建两个实例
        var obj1 = new class1();
        var obj2 = new class2();
        //试验obj1和obj2的方法
//        obj1.method();
//        obj2.method();
//        obj1.method2();
        obj2.method2();
//-->
</script>

posted on 2013-03-02 19:57  唐山张  阅读(82)  评论(0)    收藏  举报

导航