创建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. 代码修改mysql字符

    为了不再显示的时候乱码,要修改数据库默认编码.以下以GBK编码页面为例进行说明: 1.修改MYSQL的配置文件:my.ini里面修改default-character-set=gbk2.代码运行时修改 ...

  2. CM5(Cloudera Manager 5) + CDH5(Cloudera's Distribution Including Apache Hadoop 5)的安装详细文档

    参考 :http://www.aboutyun.com/thread-9219-1-1.html Cloudera Manager5及CDH5在线(cloudera-manager-installer ...

  3. [转]JQuery判断浏览器类型版本1.9和2.0之后的区别

    转至:http://zhidao.baidu.com/link?url=Nzk2aSxBKRZKYg9Evqn8hLwMyXTI-4jza-zCAZq4Vd6hWCOHIvuBX6yj8hzDYDrf ...

  4. Redis_基本类型介绍和指令___1

    1.Key(键) 主要的方法: ->set key(设置) ->get key(得到) ->del key(删除) ->exitst key(存在) -> expires ...

  5. android httpClient 支持HTTPS的2种处理方式

    摘自: http://www.kankanews.com/ICkengine/archives/9634.shtml 项目中Android https或http请求地址重定向为HTTPS的地址,相信很 ...

  6. 【转】Java Thread.join()详解

    http://www.open-open.com/lib/view/open1371741636171.html 一.使用方式. join是Thread类的一个方法,启动线程后直接调用,例如: ? 1 ...

  7. 新建虚拟SAN

    在SCVMM中,在每台主机上新建虚拟SAN(所使用的FC适配器必须一起用NPIV,否则不可用),新建完成后,在其主机的Hyper-V管理器中也可以看到 每台主机上的虚拟SAN名称必须相同    之后就 ...

  8. RedHat Enterprise Linux 6.4使用Centos 6 的yum源 分类: 服务器搭建 Nginx 2015-07-14 14:11 5人阅读 评论(0) 收藏

    转载自:http://blog.sina.com.cn/s/blog_50f908410101cto6.html 思路:卸载redhat自带yum,然后下载centos的yum,安装后修改配置文件 1 ...

  9. 三期_day12_其它+jetty的使用

    1.大致总结 行程管理和留言反馈和前面的思路差点儿相同,这里就不多啰嗦了.经过十几天的写写停停.有了一个初步的进展了,再往下写也没有啥欲望了.还是研究下android和server,设计模式和网络这些 ...

  10. SQLite的SQL语法

    SQLite库能够解析大部分标准SQL语言.但它也省去了一些特性而且增加了一些自己的新特性.这篇文档就是试图描写叙述那些SQLite支持/不支持的SQL语法的.查看keyword列表. 例如以下语法表 ...