extends in javascript

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


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

function Author(name,books){
    Person.call(this,name);
    this.books = books;
}
Author.prototype.getBooks = function(){
    return this.books;
}

extend(Author,Person);


var p = new Person("张三");
var a = new Author("刘畅");

console.log(p.getName());
console.log(a.getName());
相关推荐
xieliyu.6 小时前
Java算法精讲:双指针(二)
java·开发语言·算法
云水一下6 小时前
TypeScript 从零基础到精通(五):高级类型与泛型
前端·javascript·typescript
何以解忧,唯有..6 小时前
Python包管理工具pip:从入门到精通
开发语言·python·pip
counterxing6 小时前
vibe coding 之后,我更不想打字了
前端·agent·ai编程
雪的季节7 小时前
RabbitMQ详解
开发语言
云水一下7 小时前
TypeScript 从零基础到精通(六):类型声明与模块化
javascript·typescript
copyer_xyf7 小时前
Python 模块与包的导入导出
前端·后端·python
研☆香7 小时前
es6新特性功能介绍(四)
前端·ecmascript·es6
微扬嘴角7 小时前
React篇1--JSX语法规则、组件、组件实例的3大特性
前端·react.js·前端框架
ice8130331817 小时前
【Python】Matplotlib折线图绘制
开发语言·python·matplotlib