js 集成模式 07-24

2018-08-26  本文已影响0人  小老虎丶冷心

**
原始继承模式--原型链

      过多的继承了没有用的属性

2:借用构造函数

      不能借用构造函数的原型

     每次借用构造函数都要多走构造函数

3:共享构造原型

   不能随便改造自己的原型   

4:圣杯模式
**
一:原型链;

<script>
            
            Grand.prototype.lastName='jx';
            function Grand(){
                
            }
            var grand= new Grand()
            Father.protptype=grand;
            function Father (){}
            var father=new Father()
            Son.prototype=father;
            
            function Son(){}
            var son=new Son();
            
            
    </script>


二:构造函数;

 <script type="text/javascript">
            function Person(name,age,sex){
                this.name=name;
                this.age=age;
                this.sex=sex;
            }
            function Student(name,age,sex,grade){
                Person.call(this,name,age,sex){
                    this.grade=grade;
                }
            }
            var student=new Student()
        </script>

三:共享原型;

 <script type="text/javascript">
            Father.prototype.name='deng';
            function Father(){
                
            }
            function Son(){
                
            }
            Son.prototype=Father.prototype;
            var son=new Son();
            var father= new Father();
        </script>
    
 inherit
<script type="text/javascript">
            Father.prototype.lastName='yang';
            function Father(){
                
            }
            function Son(){
                
            }
            function inherit(Target,Origin){
                Target.prototype=Origin.prototype;
            }
            inherit(Son,Father);//不能写在var son =new Son();之后,否则不能继承
              var son =new Son();
        
        </script>
以上的这个方法如果给他添加儿子新的属性,如下图,父亲也会同样继承,以为他们都是指向同一个原型,所以为了避免这种情况,进行如下修改

四:圣杯模式

<script type="text/javascript">
            function inherit(Targer,Origin){
                function F(){}
                    F.prototype=Origin.prototype;
                    Targer.prototype=new F();
                }   
                Father.prototype.name='deng';
                function Father(){
                    
                }
                function Son(){
                    
                }
            inherit(Son,Father);
            var son=new Son();
            var father=new Father();
              
 
        </script>

圣杯模式的最终代码

<script type="text/javascript">
            function inherit(Targer,Origin){
                function F(){}
                    F.prototype=Origin.prototype;
                    Targer.prototype=new F();
                    Target.prototype.constructor=Target;//调回他自己
                    Target.prototype.uber=Origin.prototype;//知道谁调用它
                }   
                Father.prototype.name='deng';
                function Father(){
                    
                }
                function Son(){
                    
                }
            inherit(Son,Father);
            var son=new Son();
            var father=new Father();
              
 
        </script>

上一篇 下一篇

猜你喜欢

热点阅读