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

推荐订阅源

D
DataBreaches.Net
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
B
Blog
博客园 - Franky
I
InfoQ
A
About on SuperTechFans
博客园_首页
L
LangChain Blog
量子位
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
美团技术团队
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
G
Google Developers Blog
Last Week in AI
Last Week in AI

博客园 - 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,不能调用被覆盖的基类中的同名方法。