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());
相关推荐
喝拿铁写前端2 小时前
别再让 AI 直接写页面了:一种更稳的中后台开发方式
前端·人工智能
A向前奔跑3 小时前
前端实现实现视频播放的方案和面试问题
前端·音视频
代码or搬砖3 小时前
String字符串
android·java·开发语言
十一.3664 小时前
131-133 定时器的应用
前端·javascript·html
leo__5204 小时前
基于两步成像算法的聚束模式SAR MATLAB实现
开发语言·算法·matlab
xhxxx4 小时前
你的 AI 为什么总答非所问?缺的不是智商,是“记忆系统”
前端·langchain·llm
Macbethad5 小时前
自动化测试技术报告
开发语言·lua
3824278275 小时前
python:输出JSON
前端·python·json
不会画画的画师5 小时前
Go开发指南:io/ioutil包应用和迁移指南
开发语言·后端·golang