代码改变世界

javascript面向对象中的对象创建、继承、封装等实现方式

2012-10-26 15:01  Zhuang miao  阅读(248)  评论(0编辑  收藏  举报
 /**
 *包含私有成员的对象创建方法
 * @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']);