创建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. 枚举在c与c++中定义的不同

    众所周知的,枚举是在运行期才决定枚举变量的值的,而不是像宏一样在预编译的时候就进行值得替换. 而且c标准规定: size(int) <= size(enum)<=系统所能表示的最大范围的值 ...

  2. HW2.22

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  3. Stream消息流 和 Stream Grouping 消息流组

  4. URL参数带中文,后台接收乱码解决方案

    1.前台中文参数用encodeURIComponent()进行编码,如: var textName= encodeURIComponent(name); 2.对整个URL用encodeURI()进行编 ...

  5. 【三支火把】---队列和栈的C程序实现

    这几天总结了C语言的队列,栈的实现方法,在此总结一下:一.栈 首先从栈开始,诚然,相信学习过数据结构的你,肯定应该知道栈是什么东西了,如果不知道也没事每一句话我就可以帮你总结--数据只在栈顶进行插入和 ...

  6. Redis: OOM command not allowed when used memory > ‘maxmemory

    Redis: OOM command not allowed when used memory > ‘maxmemory’ 解决方式: $ vim /etc/redis/6903.conf ma ...

  7. iOS从生成证书到打包上架-01(详细2016-10最新)

    今天项目上架成功,在此小结一下这个过程,希望对这个流程有疑惑的小伙伴少走弯路(大神请忽略此文) 关于证书是什么,请自行百度,Google,这里直接上流程. 1.首先打开苹果开发者网站,Apple De ...

  8. 分布式助手Zookeeper(一)

    分布式助手Zookeeper(一)博客分类: Zookeeper   Zookeeper最早是Hadoop的一个子项目,主要为Hadoop生态系统中一些列组件提供统一的分布式协作服务,在2010年10 ...

  9. string的操作

    除了顺序容器共有的操作之外,string类型还提供了一些额外的操作.这些操作中的大部分要么是提供string类和C风格字符数组之间的相互转换,要么是增加了允许我们用下标代替迭代器的版本. 构造stri ...

  10. jQuery动态实现title的修改 失效问题

    最近做了一个网站,一切都很顺利,在上线的时候,突然发现一个严重的问题,开始面对这个问题,完全不知所措(在goole.火狐.IE9及其以上都没得问题:IE8及其低版本都失效)只是浏览器弹出一个bug,但 ...