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']);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值