《ext江湖》第8章继承-代码片段
创建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章继承-代码片段的更多相关文章
- Java 学习笔记 ------第六章 继承与多态
本章学习目标: 了解继承的目的 了解继承与多态的关系 知道如何重新定义方法 认识java.lang.object 简介垃圾回收机制 一.继承 继承是java面向对象编程技术的一块基石,因为它允许创建分 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (30) ------ 第六章 继承与建模高级应用之多对多关联
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第六章 继承与建模高级应用 现在,你应该对实体框架中基本的建模有了一定的了解,本章 ...
- 30+有用的CSS代码片段
在一篇文章中收集所有的CSS代码片段几乎是不可能的事情,但是我们这里列出了一些相对于其他的更有用的代码片段,不要被这些代码的长度所吓到,因为它们都很容易实现,并且具有良好的文档.除了那些解决常见的恼人 ...
- Javascript 语言精粹 代码片段合集
Javascript 语言精粹 代码片段合集 标签:Douglas-Crockford Javascript 最佳实践 原文链接 更好的阅读体验 使用一个method 方法定义新方法 Function ...
- 46 个非常有用的 PHP 代码片段
在编写代码的时候有个神奇的工具总是好的!下面这里收集了 40+ PHP 代码片段,可以帮助你开发 PHP 项目. 这些 PHP 片段对于 PHP 初学者也非常有帮助,非常容易学习,让我们开始学习吧- ...
- Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段(转)
摘自:http://blog.csdn.net/shakespeare001/article/details/7926783 Adapter是连接后端数据和前端显示的适配器接口,是数据Data和UI( ...
- 非常实用的PHP代码片段推荐
当使用PHP进行开发的时候,如果你自己收 藏 了一些非常有用的方法或者代码片段,那么将会给你的开发工作带来极大的便利.今天我们将介绍10个超级好用的PHP代码片段,希望大家能够喜欢! 1. 使用te ...
- 直接拿来用 九个超实用的PHP代码片段(二)
每位程序员和开发者都喜欢讨论他们最爱的代码片段,尤其是当PHP开发者花费数个小时为网页编码或创建应用时,他们更知道这些代码的重要性.为了节约编码时间,笔者收集了一些较为实用的代码片段,帮助开发者提高工 ...
- ASP.NET自定义控件组件开发 第二章 继承WebControl的自定义控件
原文:ASP.NET自定义控件组件开发 第二章 继承WebControl的自定义控件 第二章 继承于WebControl的自定义控件 到现在为止,我已经写了三篇关于自定义控件开发的文章,很感谢大家的支 ...
随机推荐
- WCF入门到精通(二)——契约
第一次接触WCF,如有写的不对的地方有望大家指出来,谢谢!! 本篇文章主要说下WCF中的契约的种类.契约的种类.如何定义契约等内容. 契约是一种双边或多边的协议,是利益相关方就某个问题达成的一种共识, ...
- ubuntu下git安装及连接github
1.安装 sudo apt-get install git git-core git-gui git-doc git-svn git-cvs gitweb gitk git-email git-dae ...
- va_start、va_end、va_list的使用
1:当无法列出传递函数的所有实参的类型和数目时,可用省略号指定参数表void foo(...);void foo(parm_list,...); 2:函数参数的传递原理函数参数是以数据结构:栈的形式存 ...
- CF29D - Ant on the Tree(DFS)
题目大意 给定一棵树,要求你按给定的叶子节点顺序对整棵树进行遍历,并且恰好经过2*n-1个点,输出任意一条符合要求的路径 题解 每次从叶子节点开始遍历到上一个叶子节点就OK了, 这个就是符合要求的路径 ...
- 【三支火把】---队列和栈的C程序实现
这几天总结了C语言的队列,栈的实现方法,在此总结一下:一.栈 首先从栈开始,诚然,相信学习过数据结构的你,肯定应该知道栈是什么东西了,如果不知道也没事每一句话我就可以帮你总结--数据只在栈顶进行插入和 ...
- weblogic目录结构
安装WEBLOGIC SERVER weblogic server 的目录结构 weblogic server的classpath变量 weblogic server使用命令行 通过管理控制台执行核心 ...
- CAS 之 集成RESTful API
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘測 LCT
入门级LCT: 仅仅有 Cut Link 2049: [Sdoi2008]Cave 洞穴勘測 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 3073 ...
- STM32F030 IO口外部中断应用
//==文件exit.h============================================================ #ifndef __EXIT_H #define __ ...
- 111_leetcode_Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...