惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
C
Check Point Blog
博客园_首页
B
Blog
D
Docker
U
Unit 42
量子位
I
InfoQ
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
GbyAI
GbyAI
L
LangChain Blog
云风的 BLOG
云风的 BLOG
博客园 - Franky
美团技术团队
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
Vercel News
Vercel News
Recent Announcements
Recent Announcements
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Google DeepMind News
Google DeepMind News

博客园 - DingJun

Javascript 继承方法3 Javascript 继承方法2 Call web service from excel Cannot load type (加载页面出错) 几个主流的浏览器引擎及判定 防止数据库日志文件增长 配置发布数据库服务器时碰到错误18483 一些有关。NET界面处理与多线程的文章 不可恢复的生成错误 在.Net安装项目中如何判断操作系统的版本 修改dataConfiguration.config文件 SQL Server 使用外部连接 在.NET下利用目录服务操纵本机用户和用户组 System.windows.forms.datagrid控件使用技巧 读取配置文件中的自定义节 区域设置与格式化(1) 默认的 IIS MIME 类型关联 在.NET中使用XPath查找指定元素时遇到的麻烦(以dataConfiguration.config为例) 使用Data access block
Javascript 继承方法1
DingJun · 2007-09-14 · via 博客园 - DingJun

几天学习后总结一下Javascript关于继承的几种方法,基本都是关于原型法的,先看最简单的。

定义基类Person:

function Person(first, last)
{
   
this.first = first;
   
this.last = last;
}


Person.prototype.toString
= function()
{
   
return this.first + " " + this.last;
}
;

定义子类Employee:

function Employee(first, last, id)
{
   
this.id = id;
}


Employee.prototype
= new Person();
Employee.prototype.constructor
= Employee;

Employee.prototype.toString
= function()
{
   
return this.first + " " + this.last + ": " + this.id;
}
;

通过改变Employee.prototype的原型让Employee继承于Person类:Employee.prototype = new Person();

再定义子类Manager:

function Manager(first, last, id, department) {
   
this.department = department;
}


Manager.prototype
= new Employee();
Manager.prototype.constructor
= Manager;

Manager.prototype.toString
= function()
{
    return this.first + " " + this.last + ": " + this.id
 + ": " + this.department;
}
;

用同样的方法让Manager类继承于Employee类。
缺点:
1,不能调用基类的构造函数以便初始化。
2,不能调用被覆盖的基类中的同名方法。