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());
相关推荐
HashTang3 分钟前
我用 Cloudflare Workers + GitHub Actions 做了个 2.5 刀/月的 AI 日报,代码开源了
前端·ai编程·aiops
老王以为10 分钟前
前端重生之 - 前端视角下的 Python
前端·后端·python
2601_9491942613 分钟前
Python爬虫完整代码拿走不谢
开发语言·爬虫·python
饭后一颗花生米20 分钟前
2026 AI加持下前端学习路线:从入门到进阶,高效突破核心竞争力
前端·人工智能·学习
五号厂房22 分钟前
TypeScript 类型导入详解:import type 与 import {type}
前端
c***892031 分钟前
python爬虫——爬取全年天气数据并做可视化分析
开发语言·爬虫·python
果然_34 分钟前
为什么你的 PR 总是多出一堆奇怪的 commit?90% 的人都踩过这个 Git 坑
前端·git
xpyjs38 分钟前
零依赖、链式调用、可变对象:重新设计 JavaScript 颜色处理体验
前端
WayneYang40 分钟前
Node.js 全栈知识点详细整理(含代码示例 + 前端结合实战)
前端·node.js
土拨鼠爱coding42 分钟前
Chrome插件 - DIY Theme
前端·chrome