• 继承方法-->最终模式


            function inherit(Target,Origin){
                function F(){};
                F.prototype = Origin.prototype;  //
                Targrt.prototype = new F();
                Target.prototype.constructor = Target;  //增加自己的constructor
                Target.prototype._super = Origin;  //找到自己的超类
            }


    function F(){};    中间函数
    F.prototype = Origin.prototype;  
    Targrt.prototype = new F();
    或者

    function proto(target,obj){
            function Fn(){};
            Fn.prototype = obj.prototype;
            target.prototype = new Fn();
        }
        function proto2(target,obj){
            function Fn(){};
            Fn.prototype = obj.__proto__;
            target.__proto__ = new Fn();
        }
        function son(){
            this.name = 'tom';
        };
        function father(){
            this.name = 'jim';
        };
        father.prototype = {
            lastName:'Green'
        }
        //proto(son,father);
        var s1 = new son();
        var f1 = new father();
        proto2(s1,f1);
        console.log(s1.lastName);

    类似的方法:原型继承

    function Obj(o){
            function f(){};
            f.prototype = o;
            return new f();
        }
        
        function person(name,age){
            this.name = name;
            this.age = age;
        }
    
        person.prototype.say = function(){
            console.log(this.name+' : '+this.age);
        }
        var p1 = Obj(new person('zhangsan',23));
        var p2 = Obj(new person('zhangsan',23));
        console.log(p1);
        console.log(p2);
        console.log(p1.__proto__ === p2.__proto__);//false
        console.log(p1.__proto__.__proto__ === p2.__proto__.__proto__);//true
        p1.name1 = 'zhangsan';
        p2.name1 = 'lisi';
        p1.__proto__.name = 'liuliu';
        console.log(p1);
        console.log(p2);
        //so要把原型的constructor回归到原型
        p1.__proto__.constructor = person;
        p1.__proto__._superProto = person.prototype;
        console.log(p1.__proto__.constructor);
        console.log(p1.__proto__._superProto);

    闭包形式

            var inherit = (function(){
                var F = function(){}
                return function(Target,Origin){
                    F.prototype = Origin.prototype;
                    Target.prototype = new F();
                    Target.prototype.constructor = Target;
                    Target.prototype._super = Origin;
                    console.log(1);
                }
            }())
            function Father(){};
            Father.prototype.name = 'zhang';
            function Son(){};
            inherit(Son,Father);
            var father = new Father();
            var son = new Son();
            console.log(son.name);
    问题:
    var father = new Father(); var son = new Son();//son指向一个Son()对象 ,(son.__proto__ === Son.prototype)Son.prototype指向一个空间,
    inherit(Son,Father);//Son.prototype指向了另一个空间,但是son.__proto__指向的空间并没有变化,所以name属性找不到 
    console.log(son.name);//undefined

    实例:
            function Father(){};
            Father.prototype.lastName = 'Zhangsan';
            var F = new Father();
            function Son(){};
            Father.prototype = new Son();
            var P = new Father();
            console.log(F.lastName);//zhangsan
            console.log(P.lastName);//undefined
  • 相关阅读:
    MSSQL
    Dapper
    Asynchronous Programming
    PHP on CentOS (LAMP) and wordpress
    CSS
    TypeScript & JavaScript
    AngularJS 2.0
    ReadMe.md MarkDown file
    shortcuts on Windows and MacOS
    移动平台的meta标签
  • 原文地址:https://www.cnblogs.com/jokes/p/9234779.html
Copyright © 2020-2023  润新知