创建Animal对象

<html>
<head>
<title>11</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<script type="text/javascript">
Animal = function(tail){
this.tail = tail || "动物的尾巴";
};
Animal.prototype={
happy:function(){
alert("摇动 > " + this.tail);
},
eat:function(){
alert("动物吃生的");
},
run:function(){
alert("动物四条腿跑");
},
fight:function(){
alert("动物往死里打");
}
};
Animal.prototype.constructor=Animal;
var a = new Animal("蓬松的尾巴");
a.happy();
var b = new Animal("长尾巴");
b.happy();
var init = function(){};
</script>
</head>
<body onload="init();">
</body>
</html>

创建Person对象,继承Animal

<html>
<head>
<title>11</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<script type="text/javascript">
Animal = function(tail){
this.tail = tail || "动物的尾巴";
};
Animal.prototype={
happy:function(){
alert("摇动 > " + this.tail);
},
eat:function(){
alert("动物吃生的");
},
run:function(){
alert("动物四条腿跑");
},
fight:function(){
alert("动物往死里打");
}
};
Animal.prototype.constructor=Animal; Person = function(name){
this.name = name;
};
Person.prototype=new Animal();
var p = new Person("大漠穷秋");
alert(p.tail);
alert(p.name);
p.happy();
p.eat();
p.run();
p.fight(); var init = function(){};
</script>
</head>
<body onload="init();">
</body>
</html>

删除Person的tail属性

<html>
<head>
<title>11</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<script type="text/javascript">
Animal = function(tail){
this.tail = tail || "动物的尾巴";
};
Animal.prototype={
happy:function(){
alert("摇动 > " + this.tail);
},
eat:function(){
alert("动物吃生的");
},
run:function(){
alert("动物四条腿跑");
},
fight:function(){
alert("动物往死里打");
}
};
Animal.prototype.constructor=Animal; Person = function(name){
this.name = name;
};
Person.prototype=new Animal();
delete Person.prototype.tail;
var p = new Person("大漠穷秋");
alert(p.tail); var init = function(){};
</script>
</head>
<body onload="init();">
</body>
</html>

重置constructor

<html>
<head>
<title>11</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<script type="text/javascript">
Animal = function(tail){
this.tail = tail || "动物的尾巴";
};
Animal.prototype={
happy:function(){
alert("摇动 > " + this.tail);
},
eat:function(){
alert("动物吃生的");
},
run:function(){
alert("动物四条腿跑");
},
fight:function(){
alert("动物往死里打");
}
};
Animal.prototype.constructor=Animal; Person = function(name){
this.name = name;
};
Person.prototype=new Animal();
delete Person.prototype.tail;
Person.prototype.constructor=Person; var p = new Person("大漠穷秋");
alert(p.constructor);
alert(p.constructor==Person); var init = function(){};
</script>
</head>
<body onload="init();">
</body>
</html>

对象冒充

<html>
<head>
<title>11</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<script type="text/javascript">
Animal = function(tail){
this.tail = tail || "动物的尾巴";
};
Animal.prototype={
happy:function(){
alert("摇动 > " + this.tail);
},
eat:function(){
alert("动物吃生的");
},
run:function(){
alert("动物四条腿跑");
},
fight:function(){
alert("动物往死里打");
}
};
Animal.prototype.constructor=Animal; Person = function(name){
Animal.call(this);
this.name = name;
delete this.tail;
}; var p = new Person("大漠穷秋");
alert(p.name);
alert(p.tail);
p.happy();
p.eat();
p.run();
p.fight(); var init = function(){};
</script>
</head>
<body onload="init();">
</body>
</html>

静态属性, undefined是正常的。

<html>
<head>
<title>11</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<script type="text/javascript">
Animal = function(tail){
this.tail = tail || "动物的尾巴";
Animal.instanceCounter++;
};
Animal.prototype={
happy:function(){
alert("摇动 > " + this.tail);
},
eat:function(){
alert("动物吃生的");
},
run:function(){
alert("动物四条腿跑");
},
fight:function(){
alert("动物往死里打");
}
};
Animal.prototype.constructor=Animal; Person = function(name){
Person.superclass.call(this);
this.name = name;
};
Person.superclass = Animal; var p1 = new Person("大漠穷秋");
alert(Person.instanceCounter); var init = function(){};
</script>
</head>
<body onload="init();">
</body>
</html>
<html>
<head>
<title>11</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<script type="text/javascript">
Animal = function(tail){
this.tail = tail || "动物的尾巴";
Animal.instanceCounter++;
};
Animal.instanceCounter=0;
Animal.prototype={
happy:function(){
alert("摇动 > " + this.tail);
},
eat:function(){
alert("动物吃生的");
},
run:function(){
alert("动物四条腿跑");
},
fight:function(){
alert("动物往死里打");
}
};
Animal.prototype.constructor=Animal; Person = function(name){
Person.superclass.call(this);
this.name = name;
for(var p in Animal){
//不能拷贝父类的prototype上的属性
Person[p] = Animal[p];
}
};
Person.superclass = Animal; var p1 = new Person("大漠穷秋");
var p2 = new Person("小秋");
alert(Person.instanceCounter); //不能拷贝父类的prototype上的属性
p1.happy(); var init = function(){};
</script>
</head>
<body onload="init();">
</body>
</html>

原型继承:可以把父类prototype上的属性全部继承下来,而且利用内建的原型查找机制,子类的运行效率会比较高。但是,原型继承不能“继承”父类的静态属性。

对象冒充:可以继承通过this赋值的属性,配合上for...in循环的处理还可以“继承”父类的静态属性。但是,不能继承父类中通过prototype设置的属性。

对象冒充和原型继承综合运用

<html>
<head>
<title>11</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<script type="text/javascript">
Animal = function(tail){
this.tail = tail || "动物的尾巴";
Animal.instanceCounter++;
};
Animal.instanceCounter=0;
Animal.prototype={
happy:function(){
alert("摇动 > " + this.tail);
},
eat:function(){
alert("动物吃生的");
},
run:function(){
alert("动物四条腿跑");
},
fight:function(){
alert("动物往死里打");
}
};
Animal.prototype.constructor=Animal; Person = function(name){
//对象冒充,并删除不需要的属性
Person.superclass.call(this);
delete this.tail; this.name = name;
//拷贝父类的静态属性
for(var p in Animal){
Person[p] = Animal[p];
}
};
Person.superclass = Animal; //原型继承并删除不需要的方法
var F = function(){};
F.prototype=Animal.prototype;
delete F.prototype.fight;
Person.prototype = new F();
Person.prototype.constructor=Person; var p1 = new Person("大漠穷秋");
alert(Person.instanceCounter);
alert(p1.tail);
alert(p1.name);
p1.eat();
p1.fight(); var init = function(){};
</script>
</head>
<body onload="init();">
</body>
</html>

覆盖prototype上的方法

<html>
<head>
<title>11</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<script type="text/javascript">
Animal = function(tail){
this.tail = tail || "动物的尾巴";
Animal.instanceCounter++;
};
Animal.instanceCounter=0;
Animal.prototype={
happy:function(){
alert("摇动 > " + this.tail);
},
eat:function(){
alert("动物吃生的");
},
run:function(){
alert("动物四条腿跑");
},
fight:function(){
alert("动物往死里打");
}
};
Animal.prototype.constructor=Animal; Person = function(name){
//对象冒充,并删除不需要的属性
Person.superclass.call(this);
delete this.tail; this.name = name;
//拷贝父类的静态属性
for(var p in Animal){
Person[p] = Animal[p];
}
};
Person.superclass = Animal; //原型继承并删除不需要的方法
var F = function(){};
F.prototype=Animal.prototype;
delete F.prototype.fight;
F.prototype.eat = function(){
alert("人类吃熟的");
}; /**
需要覆盖多个方法时使用Ext的apply
Ext.apply(F.ptototype, {
eat:function(){
alert("人类吃熟的");
}
});
**/
Person.prototype = new F();
Person.prototype.constructor=Person; var p1 = new Person("大漠穷秋");
p1.eat(); var init = function(){};
</script>
</head>
<body onload="init();">
</body>
</html>

--

《ext江湖》第8章继承-代码片段的更多相关文章

  1. Java 学习笔记 ------第六章 继承与多态

    本章学习目标: 了解继承的目的 了解继承与多态的关系 知道如何重新定义方法 认识java.lang.object 简介垃圾回收机制 一.继承 继承是java面向对象编程技术的一块基石,因为它允许创建分 ...

  2. 《Entity Framework 6 Recipes》中文翻译系列 (30) ------ 第六章 继承与建模高级应用之多对多关联

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第六章  继承与建模高级应用 现在,你应该对实体框架中基本的建模有了一定的了解,本章 ...

  3. 30+有用的CSS代码片段

    在一篇文章中收集所有的CSS代码片段几乎是不可能的事情,但是我们这里列出了一些相对于其他的更有用的代码片段,不要被这些代码的长度所吓到,因为它们都很容易实现,并且具有良好的文档.除了那些解决常见的恼人 ...

  4. Javascript 语言精粹 代码片段合集

    Javascript 语言精粹 代码片段合集 标签:Douglas-Crockford Javascript 最佳实践 原文链接 更好的阅读体验 使用一个method 方法定义新方法 Function ...

  5. 46 个非常有用的 PHP 代码片段

    在编写代码的时候有个神奇的工具总是好的!下面这里收集了 40+ PHP 代码片段,可以帮助你开发 PHP 项目. 这些 PHP 片段对于 PHP 初学者也非常有帮助,非常容易学习,让我们开始学习吧- ...

  6. Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段(转)

    摘自:http://blog.csdn.net/shakespeare001/article/details/7926783 Adapter是连接后端数据和前端显示的适配器接口,是数据Data和UI( ...

  7. 非常实用的PHP代码片段推荐

    当使用PHP进行开发的时候,如果你自己收 藏 了一些非常有用的方法或者代码片段,那么将会给你的开发工作带来极大的便利.今天我们将介绍10个超级好用的PHP代码片段,希望大家能够喜欢! 1.  使用te ...

  8. 直接拿来用 九个超实用的PHP代码片段(二)

    每位程序员和开发者都喜欢讨论他们最爱的代码片段,尤其是当PHP开发者花费数个小时为网页编码或创建应用时,他们更知道这些代码的重要性.为了节约编码时间,笔者收集了一些较为实用的代码片段,帮助开发者提高工 ...

  9. ASP.NET自定义控件组件开发 第二章 继承WebControl的自定义控件

    原文:ASP.NET自定义控件组件开发 第二章 继承WebControl的自定义控件 第二章 继承于WebControl的自定义控件 到现在为止,我已经写了三篇关于自定义控件开发的文章,很感谢大家的支 ...

随机推荐

  1. 如何禁止掉SharePoint页面个性化(网站操作-编辑页面)

    使用SharePoint Designer打开,或者创建一个新的Master Page,找到SPWebPartManager控件,如下所示,修改它的属性“Personalization-Enabled ...

  2. 问题-[Access]“无法打开工作组信息文件中的表 'MSysAccounts'”的问题的解决方法

    问题现象:ado.net oledb方式访问Access数据库文件时报错“无法打开工作组信息文件中的表 'MSysAccounts'”的问题的解决方法  问题处理:1.数据库名称不能命名为:Syste ...

  3. python 探索(四) Python CookBook 系统管理

    看CookBook就像看小说,挑感兴趣的先学习. 所以继<文本>之后,开始<系统管理>. 同样,请善用目录. 发现一个cookbook:好地址 生成随机密码 from rand ...

  4. SGU107——987654321 problem

    For given number N you must output amount of N-digit numbers, such, that last digits of their square ...

  5. linux中配置maven环境

    一 .  下载maven http://maven.apache.org/download.cgi 二.   将maven解压到你的工具文件夹下 如我是解压到:  /home/urc/tool下 三. ...

  6. PowerDesigner实用方法小结(1)

    PowerDesigner使用方法小结 PowerDesigner多用来进行数据库模型设计,具有SQL语句自动生成等功能.当然,也有不少缺点,比如团队分享. 一.设置PowerDesigner模型视图 ...

  7. Ubuntu 12.04 安装搜狗输入法

    安装指南 Ubuntu / Ubuntu Kylin 14.04 LTS 版本 只需双击下载的 deb 软件包,即可直接安装搜狗输入法. Ubuntu 12.04 LTS 版本 由于 Ubuntu 1 ...

  8. 深入浅出Android动态载入jar包技术

    在实际项目中.因为某些业务频繁变更而导致频繁升级client的弊病会造成较差的用户体验,而这也恰是Web App的优势,于是便衍生了一种思路.将核心的易于变更的业务封装在jar包里然后通过网络下载下来 ...

  9. PreferenceActivity 自动保存属性

    package com.example.preference; import android.content.Context; import android.os.Bundle; import and ...

  10. 【转】coco2d-x 纹理研究

    1.通常情况下用PVR格式的文件来进行图片显示的时候,在运行速度和内存消耗方面都要比PNG格式要快和小.一般情况下PVR消耗的内存比PNG消耗的内存小25%左右.PVR格式可以用ZWoptex导出.P ...