zoukankan      html  css  js  c++  java
  • DOW

    //工厂函数
    function createPerson(name,age,job){
    var o = new Object();
    o.name=name;
    o.age=age;
    o.job=job;
    o.sayName=function(){
    alert(this.name);
    }
    return o;
    }
    var person1=createPerson("唐",18,"唐门");
    var person2=createPerson("张",19,"张门");
    //构造函数
    function Person(name,age,job){
    this.name=name;
    this.age=age;
    this.job=job;
    this.sayName=function(){
    alert(this.name);
    };
    }
    var person3=new Person("王",20,"王门");
    var person4=new Person("刘",21,"刘门");
    //原型模式
    function Person1(){
    }
    Person1.prototype.name="陈";
    Person1.prototype.age=22;
    Person1.prototype.job="陈门";
    Person1.prototype.sayName=function(){
    alert(this.name);
    };
    var person5 =new Person1();
    person5.sayName();
    var person6 =new Person1();
    person6.sayName();
    alert(person5.sayName == person6.sayName);
    //组合使用构造函数和原型模式
    function Person2(name,age,job){
    this.name=name;
    this.age=age;
    this.job=job;
    this.friends =["陈","刘"];
    }
    Person2.prototype={
    constructor:Person2,
    sayName:function(){
    alert(this.name);
    }
    }
    var person7=new Person2("红",20,"红门");
    var person8=new Person2("黄",21,"黄门");
    person7.friends.push("林");
    alert(person7.friends);
    alert(person8.friends);
    alert(person7.friends === person8.friends);
    alert(person7.sayName === person8.sayName);
    //动态原型模式
    function Person3(name,age,job){
    //属性
    this.name=name;
    this.age=age;
    this.job=job;
    //方法
    if(typeof this.sayName != "function"){
    Person3.prototype.sayName = function(){
    alert(this.name);
    };
    }
    }
    var friend = new Person3("高",20,"高门");
    friend.sayName();
    //寄生构造函数模式
    function Person4(name,age,job){
    var o = new Object();
    o.name=name;
    o.age=age;
    o.job=job;
    o.sayName=function(){
    alert(this.name);
    };
    return o;
    }
    var friend1=new Person4("天",20,"天门");
    friend1.sayName();
    //稳妥函数
    function Person5(name,age,job){
    //创建要返回的对象
    var o =new Object();
    //可以在这里定义私有变量和函数
    //添加方法
    o.sayName=function(){
    alert(this.name);
    };
    //返回对象
    return o;
    }
    //原型链
    function SuperType(){
    this.prototype = true;
    }
    SuperType.prototype.getSuperValue = function(){
    return this.prototype;
    };
    function SubType(){
    this.subproperty = false;
    }
    //继承了SuperType
    SubType.prototype = new SuperType();
    SubType.prototype.getSuperValue = function(){
    return this.subproperty;
    };
    var instance = new SubType();
    alert(instance.getSuperValue());
    //借用构造函数
    function SuperType1(){
    this.colors =["red","blue","green"];
    }
    function SubType1(){
    //继承了SuperType1
    SuperType1.call(this);
    }
    var instance1 = new SubType1();
    instance1.colors.push("black");
    alert(instance1.colors);
    var instance2 = new SubType1();
    alert(instance2.colors);
    //传递参数
    function SuperType2(name){
    this.name=name;
    }
    function SubType2(){
    //继承了SuperType2,同时还传递了参数
    SuperType2.call(this,"钱");
    //实质属性
    this.age=29;
    }
    var instance3 = new SubType2();
    alert(instance3.name);
    alert(instance3.age);
    //组合继承
    function SuperType3(name){
    this.name = name;
    this.colors =["red","blue","green"];
    }
    SuperType3.prototype.sayName =function(){
    alert(this.name);
    };
    function SubType3(name,age){
    //继承属性
    SuperType3.call(this,name);

                this.age =age;
             }
             //继承方法
             SubType3.prototype = new SuperType3();
             SubType3.prototype.sayAge = function(){
                alert(this.age);
             };
             var instance4 = new SubType3("钟",22);
             instance4.colors.push("black");
             alert(instance4.colors);
             instance4.sayName();
             instance4.sayAge();
    
             var instance5 = new SubType3("罗",23);
             alert(instance5.colors);
             instance5.sayName();
             instance5.sayAge();
    
    
             //寄生组合式继承
              function SuperType4(name){
                 this.name = name;
                 this.colors =["red","blue","green"];
             }
             SuperType4.prototype.sayName =function(){
                alert(this.name);
             };
              function SubType4(name,age){
                //继承属性
                SuperType4.call(this,name);//第二次调用SuperType4()
    
                this.age =age;
             }
             SubType4.prototype = new SuperType4;//第一次调用SuperType4()
             SubType4.prototype.constructor = SubType4;
             SubType4.prototype.sayAge = function(){
                alert(this.age);
             };
  • 相关阅读:
    读书笔记——《图解HTTP》(四)
    读书笔记——《图解HTTP》(三)
    读书笔记——《图解HTTP》(二)
    读书笔记——《图解HTTP》(一)
    「功能笔记」Linux常用Shell命令(终端命令)备忘录
    「Leetcode」字符串相关问题选编
    「Leetcode」二叉搜索树相关题目简析
    【补遗】 Let me sleep(NCD 2019, Gym
    「专题训练」Collecting Bugs(POJ-2096)
    「专题训练」游走(BZOJ-3143)
  • 原文地址:https://www.cnblogs.com/ArielChen/p/6282457.html
Copyright © 2011-2022 走看看