zoukankan      html  css  js  c++  java
  • javascript面向对象中的对象创建、继承、封装等实现方式

     /**
     *包含私有成员的对象创建方法
     * @param bname
     * @constructor
     */

    var Book =function(bname){
        var name,isbn;
        this.GetName=function(){
            return name;
        };
        this.GetISBN=function(){
            return isbn;
        };
        this.SetName=function(newname){
            name=newname;
        };
        this.SetISBN=function(newisbn){
            isbn=newisbn;
        };
        this.SetName(bname);
     }
    Book.prototype={
        Display:function(){
           return "Name:"+this.GetName()+ " ISBN:"+this.GetISBN();
        }
    }
    Book.prototype.FormatDisplay=function(){
        return "Name:"+this.GetName()+ ", ISBN:"+this.GetISBN();
        };


    /**
     *包含静态成员的创建对象的方法
     *CheckIsbn只被实例化一次
     * var book=new Book("");
    */

    var Book=(function(){
        var numOfBook=0;
        function CheckIsbn(isbn){
           return isbn.match("\\d(3)-\\d(5)");
        };
        var ctr= function(bname){
            var name,isbn;
            this.GetName=function(){
                return name;
            };
            this.GetISBN=function(){
                return isbn;
            };
            this.SetName=function(newname){
                name=newname;
            };
            this.SetISBN=function(newisbn){
                if(CheckIsbn(newisbn)) isbn=newisbn;
            };
            numOfBook++;
            if(numOfBook>10) throw new Error("Book: Only 10 instances of Book can be Created!");

            this.SetName(bname);
        };
        ctr.GetNumber=function(){
            return numOfBook;
        }
        return ctr;
    })();

    /**
     * 类式继承的实现
     * @param subClass
     * @param superClass
     
    */

    function extend(subClass, superClass){
        var  F=function(){};
        F.prototype=superClass.prototype;
        subClass.prototype=new F();
        subClass.prototype.constructor=subClass;

        subClass.superclass =superClass.prototype;
        if(superClass.prototype.constructor==Object.prototype.constructor){
            superClass.prototype.constructor=superClass;
        }
    }

    function Person(name){
        this.name=name;
    };
    Person.prototype.getName=function(){
        return this.name;
    };

    function author(name,book){
        author.superclass.constructor.call(this,name);
        this.book=book;
    };

    extend(author,Person);

    author.prototype.getBooks=function(){
        return this.book;
    }
    author.prototype.getName=function(){
        var name=author.superclass.getName.call(this);
        return name+ this.getBooks();
    };


    /**
     * 原型式继承
     * @type {Object}
     
    */
    var Person = {
        name:'default',
        getName:function(){
            return this.name;
        }
    };
    function clone(cloneClass){
        var F=function(){};
        F.prototype=cloneClass;
        return new F();
    }
    var reader=clone(Person);
    reader.getName();


    /**
     * 掺元类,通过扩充的方式让类共享函数
     * @param receivingClass
     * @param givingClass
     * @param params
     
    */
    function augment(receivingClass,givingClass,params){
        if(params){
            console.log('传进来的参数为:'+params.join(','));
            for(var i= 0,len=params.length;i<len;i++){
                receivingClass.prototype[params[i]]=givingClass.prototype[params[i]];
            }
        }
        else{
        for (var methodName in givingClass.prototype) {
            if(!receivingClass.prototype[methodName]){
                receivingClass.prototype[methodName]=givingClass.prototype[methodname];
            }
        }
        }
    };

    var Mixin=function(){};
    Mixin.prototype={
        serialize:function(){
            var output=[];
            for(key in this){
                output.push(key+":"+this[key]);
            }
            return output.join(',');
        },
        wMsg:function(){
             alert("Welcome");
        }
    };
    function author(name){this.name=name};

    var a=new author("miaow");

    augment(author,Mixin,['serialize','wMsg']);
  • 相关阅读:
    PyQt作品 – PingTester – 多点Ping测试工具
    关于和技术人员交流的一二三
    Pyjamas Python Javascript Compiler, Desktop Widget Set and RIA Web Framework
    Hybrid Qt applications with PySide and Django
    pyjamas build AJAX apps in Python (like Google did for Java)
    PyQt 维基百科,自由的百科全书
    InfoQ:请问为什么仍要选择Java来处理后端的工作?
    Eric+PyQt打造完美的Python集成开发环境
    python select module select method introduce
    GUI Programming with Python: QT Edition
  • 原文地址:https://www.cnblogs.com/mz121star/p/2741234.html
Copyright © 2011-2022 走看看