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

推荐订阅源

B
Blog RSS Feed
博客园 - 叶小钗
F
Fortinet All Blogs
GbyAI
GbyAI
Martin Fowler
Martin Fowler
博客园 - 聂微东
I
InfoQ
B
Blog
IT之家
IT之家
美团技术团队
L
LangChain Blog
小众软件
小众软件
C
Check Point Blog
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
Last Week in AI
Last Week in AI
A
About on SuperTechFans
博客园 - Franky
P
Proofpoint News Feed
罗磊的独立博客
月光博客
月光博客
V
Visual Studio Blog
MyScale Blog
MyScale Blog

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