JavaScript实现继承---extend函数

来源:互联网 发布:怎样投诉淘宝网 编辑:程序博客网 时间:2024/06/06 01:24

JavaScript实现继承---extend函数

    博客分类: 
  • JavaScript练习
JavaScriptprototype 
Js代码  收藏代码
  1. /* Extend Function */  
  2.   
  3. function extend(subClass,superClass){  
  4.     var Func = function(){} ;  
  5.     Func.prototype = superClass.prototype ;  
  6.     subClass.prototype = new Func() ;  
  7.     subClass.prototype.constructor = subClass ;  
  8. } ;  
  9.   
  10. /*-----Example-----*/  
  11.   
  12.   
  13. /* Class Person */  
  14. function Person(name){  
  15.     this.name = name ;  
  16. } ;  
  17.   
  18. Person.prototype.getName = function(){  
  19.     return this.name ;  
  20. } ;  
  21.   
  22. /* Class Author */  
  23.   
  24. function Author(name,books){  
  25.     Person.call(this,name) ;  
  26.     this.books = books ;  
  27. } ;  
  28.   
  29. extend(Author,Person) ;  
  30.   
  31. Author.prototype.getBooks = function(){  
  32.     return this.books ;  
  33. } ;  
原创粉丝点击