- /* Extend Function */
- function extend(subClass,superClass){
- var Func = function(){} ;
- Func.prototype = superClass.prototype ;
- subClass.prototype = new Func() ;
- subClass.prototype.constructor = subClass ;
- } ;
- /*-----Example-----*/
- /* Class Person */
- function Person(name){
- this.name = name ;
- } ;
- Person.prototype.getName = function(){
- return this.name ;
- } ;
- /* Class Author */
- function Author(name,books){
- Person.call(this,name) ;
- this.books = books ;
- } ;
- extend(Author,Person) ;
- Author.prototype.getBooks = function(){
- return this.books ;
- } ;
JavaScript实现继承---extend函数
最新推荐文章于 2022-06-22 18:57:00 发布
本文介绍了一种使用JavaScript实现继承的方法,通过定义extend函数来使子类继承父类的属性和方法。示例中Person类作为父类,Author类作为子类进行继承演示。


656

被折叠的 条评论
为什么被折叠?



