zoukankan      html  css  js  c++  java
  • javascript面向对象和原型

    /*
    //工厂模式
    function createObject(name,age){
        var obj = new Object();//新建一个对象
        obj.name=name;//新建对象的属性
        obj.age=age;
        obj.run=function(){//新建对象的方法
            return this.name+","+this.age+"...running.";
        }
        return obj;//返回新建的对象引用
    }
    
    
    var box1 = createObject("Carl",20);
    var box2 = createObject("Zhang",25);
    alert(box1.run());
    alert(box2.run());
    
    
    //构造函数
    function Box(name,age){
        this.name=name;//新建对象的属性
        this.age=age;
        this.run=function(){//新建对象的方法
            return this.name+","+this.age+"...running.";
        }
        
    }
    
    
    var box1 = new Box("Carl",20);//对象实例化
    var box2 = new Box("Zhang",25);
    alert(box1.run());
    alert(box2.run());
    alert(box1 instanceof Object);
    
    //构造函数
    function box(name,age){
        this.name=name;//新建对象的属性
        this.age=age;
        this.run=function(){//新建对象的方法
            return this.name+","+this.age+"...running.";
        }
        
    }
    
    
    var box1 = new box("Carl",20);//对象实例化
    var box2 = new box("Zhang",25);
    alert(box1.run());
    alert(box2.run());
    alert(box1 instanceof Object);
    
    
    //构造函数
    function Box(name,age){
        this.name=name;//新建对象的属性
        this.age=age;
        this.run=function(){//新建对象的方法
            return this.name+","+this.age+"...running.";
        }
        
    }
    
    var o = new Object();
    Box.call(o,"Carl",20);//call   对象冒充
    alert(o.run())
    
    
    //构造函数
    function Box(name,age){
        this.name=name;//新建对象的属性
        this.age=age;
        this.run=new Function("return this.name+this.age+'..running...'");
        
    }
    
    var o = new Object();
    Box.call(o,"Carl",20);//call   对象冒充
    alert(o.run());
    
    
    //构造函数
    function Box(name){
        this.name=name;//实例属性
    }
    Box.prototype.name="Zhang";//原型属性
    Box.prototype.age=25;//原型属性
    Box.prototype.run=function(){//原型方法
        return this.name+","+this.age+"...running.";
    }
    
    //构造函数体内的叫实例属性,每次新建的对象,其实例属性所在地址都是不一样的
    //prototype是一个引用,指向一个对象,通过prototype创建的属性,叫做原型属性,每次新建的对象的原型属性地址是一样的。
    
    var box1 = new Box("Carl");//对象实例化
    alert(box1.run());
    alert(box1.constructor);//构造函数,返回构造函数本身
    alert(box1.hasOwnProperty("name"));//判断实例属性中是否含有name属性
    delete box1.name;//删除实例属性
    alert("name" in box1); //true 判断是否存在于实例属性或者原型属性
    
    
    //构造函数
    function Box(name){
        this.name=name;//实例属性
    }
    
    Box.prototype={
        name:"Zhang",//原型属性
        age:25,//原型属性
        run:function(){//原型方法
            return this.name+","+this.age+"...running.";
        }
    }
    var box = new Box();
    alert(box.constructor);//Ojbect
    
    
    //构造函数
    function Box(name){
        this.name=name;//实例属性
    }
    
    Box.prototype={
        constructor:Box,
        name:"Zhang",//原型属性
        age:25,//原型属性
        run:function(){//原型方法
            return this.name+","+this.age+"...running.";
        }
    }
    var box = new Box();
    alert(box.constructor);//Box
    
    
    //构造函数
    function Box(name){
        this.name=name;//实例属性
    }
    
    Box.prototype={
        constructor:Box,
        name:"Zhang",//原型属性
        age:25,//原型属性
        run:function(){//原型方法
            return this.name+","+this.age+"...running.";
        }
    }
    //重写原型
    Box.prototyp={
        name:"Zhang"
    }
    
    var box = new Box();
    alert(box.run());//error
    
    
    var box=[2,1,29,3,0,13];
    alert(box.sort());
    alert(Array.prototype.sort);//查看sort是否为Array的原型函数
    
    alert(String.prototype.addstring);
    String.prototype.addstring=function(){
        return this+"...我是扩展的方法";
    }
    alert("Test addstring".addstring());
    
    
    //动态原型模式
    //构造函数
    function Box(name,age){
        this.name=name;//实例属性
        this.age=age;
        
        //将原型封装到构造函数中
        Box.prototype.run=function(){
            return this.name+this.age+"running...";
        }
    }
    
    var box1= new Box("Carl",10);
    var box2= new Box("Zhang",20);
    alert(box1.run());
    alert(box2.run());
    
    
    //构造函数
    function Box(name,age){
        this.name=name;//实例属性
        this.age=age;
        
        
        //将原型封装到构造函数中
        if(typeof this.run!='function'){//加上这段可以使得下面的原型只初始化一次
            alert("初始化开始");
            Box.prototype.run=function(){
                return this.name+this.age+"running...";
            }
            alert("初始化结束");
            
        }
    }
    
    var box1= new Box("Carl",10);
    var box2= new Box("Zhang",20);
    
    //继承,通过原型链实现
    function Box(){   //继承的函数叫做超类(父类,基类)
        this.name="Carl";
    }
    function Desk(){   //继承的函数叫做子类型(子类,派生类)
        this.age=100;
    }
    //通过原型链继承,超类型实例化后的对象实例,赋值给子类型的原型属性
    Desk.prototype=new Box();//new Box()会将Box构造里的信息和原型里的信息全都交给Desk
    
    var desk = new Desk();
    alert(desk.name);
    
    
    */
    
    /*
    //1.原型链继承 2.借用构造函数继承(对象冒充继承) 3.组合继承(结合前两种)
    
    //4.原型式继承
    
    //临时中转函数
    function obj(o){
        function F(){};
        F.prototype=o;
        return new F();
    }
    
    box={
        name:"Carl",
        age:20,
        family:["哥哥","姐姐","弟弟"]
    };
    
    
    var box1 = obj(box);
    alert(box1.family);
    box1.family.push("妹妹");
    alert(box1.family);
    
    var box2 = obj(box);
    alert(box2.family);
    
    */
    
    
    
    //临时中转函数
    function obj(o){
        function F(){};
        F.prototype=o;
        return new F();
    }
    //寄生函数
    function create(box,desk){
        var f=obj(box.prototype);
        f.constructor=desk;//调整原型构造指针
        desk.prototype=f;
    }
    
    
    function Box(name,age){
        this.name=name;
        this.age=age;
    }
    
    Box.prototype.run=function(){
        return this.name+this.age+" running...";
    
    }
    
    function Desk(name,age){
        Box.call(this,name,age); //对象冒充
    }
    //通过寄生组合继承来实现继承
    create(Box,Desk);//这句话用来代替Desk.prototype=new Box();
    
    var desk =new Desk("Carl",20);
    alert(desk.run());
    alert(desk.constructor);
    alert(desk.constructor.prototype);
    alert(desk.__proto__===desk.constructor.prototype);

    备注:本文部分代码来自李炎辉老师的javascript教程

  • 相关阅读:
    (转)Javascript面向对象编程(二):构造函数的继承(作者:阮一峰)
    (转)Javascript 面向对象编程(一):封装(作者:阮一峰)
    asp.net的3个经典范例(ASP.NET Starter Kit ,Duwamish,NET Pet Shop)学习资料
    (转)Ajax的原理和应用
    在ASP.NET MVC应用程序中实现Server.Transfer()类似的功能
    D2GS1.11 的DC Key的相關設置指南
    Win64位操作系统无法运行暗黑2战网D2GS的解决办法
    PVPGN 暗黑破坏神2 1.11b战网配置问题汇总
    PVPGN1.8.2 + D2GS1.11(38)搭建暗黑破坏神1.11b战网(配置指南)
    FineUI Grid控件右键菜单的实现
  • 原文地址:https://www.cnblogs.com/jinhuazhe2013/p/4314595.html
Copyright © 2011-2022 走看看