js 原型继承 圣杯模式

圣杯模式:隔离 - 继承

        // 圣杯模式
        var inherit = (function () {
            function F() {}
            return function (Obj, Target) {
                F.prototype = Target.prototype;
                Obj.prototype = new F();
                Obj.prototype.constructor = Obj;
                Obj.prototype.uber = Target.prototype;
            }
        }());

        function People() {}
        function Man() {}
        People.prototype = {
            name: 'wj',
            age: '28',
            say: function () {
                console.log(this.age);
            }
        }
        People.prototype.add = function () {
            this.age++;
        }
        inherit(Man, People);
        var man = new Man();
        man.age = 100;
        console.log(man.age); // 100
        man.add();
        console.log(man.age); // 101
        delete man.age;
        console.log(man.age); // 28

猜你喜欢

转载自www.cnblogs.com/justSmile2/p/9928709.html