inherit inheritance patterns

                function Father () {

                }
                Father.prototype.lastName = "Zhang";
                function Son () {

                }
                Son.prototype = Father.prototype;
                
                function F () {}
                function inherit (Target, Origin) {
                    Target.prototype = Origin.prototype;
                }
                inherit(F, Father);
                Son.prototype = new F ();
                Son.prototype.name = "zhuqi";
                var son = new Son();
                var father = new Father ();

After optimization version:                 function Father () {


                }
                Father.prototype.lastName = "Zhang";
                function Son () {

                }
                
                function inherit (Target, Origin) {
                    function F () {}
                    F.prototype = Origin.prototype;
                    Target.prototype = new F();
            Target.prototype.constuctor = Target;
} inherit(Son, Father); Son.prototype.name
= "zhuqi"; var son = new Son(); var father = new Father ();

The F construct objects as empty, this inheritance pattern is derived down the development of 'Holy Grail mode'. A problem the rest of the succession will occur, such as: to increase the Son of prototype property will be accompanied Father increase together.

Note: inherit function should add that: Target.prototype.constuctor = Target because of inheritance, Son of the Son proto__ .__ constructor will find in, find that F .__ proto__, but F .__ proto__ is inherited from Father, therefore

Son's constructor will point father. So should add that

Guess you like

Origin www.cnblogs.com/oo5lll/p/12077323.html