JavaScript中的面向对象----继承
来源:互联网 发布:如何生成淘宝口令 编辑:程序博客网 时间:2024/05/17 06:05
摘自:http://www.cnblogs.com/LongWay/archive/2008/10/19/1314705.html
1、对象冒充
- function ClassA(sColor){
- this.color = sColor;
- this.sayColor = function(){
- alert(this.color);
- }
- }
- function ClassB(sColor,sName){
- this.newMethod = ClassA;
- this.newMethod(sColor);
- delete this.newMethod;
- this.name = sName;
- this.sayName = function(){
- alert(this.name);
- }
- }
- var objB = new ClassB("color of objB","name of objB");
- objB.sayColor();
2、call方法
- function ClassA(sColor){
- this.color = sColor;
- this.sayColor = function(){
- alert(this.color);
- }
- }
- function ClassB(sColor,sName){
- // this.newMethod = ClassA;
- // this.newMethod(sColor);
- // delete this.newMethod;
- ClassA.call(this,sColor);
- this.name = sName;
- this.sayName = function(){
- alert(this.name);
- }
- }
- var objB = new ClassB("color of objB","name of objB");
- objB.sayColor();
3、apply方法
- function ClassA(sColor){
- this.color = sColor;
- this.sayColor = function(){
- alert(this.color);
- }
- }
- function ClassB(sColor,sName){
- // this.newMethod = ClassA;
- // this.newMethod(sColor);
- // delete this.newMethod;
- ClassA.apply(this,new Array(sColor));
- this.name = sName;
- this.sayName = function(){
- alert(this.name);
- }
- }
- var objB = new ClassB("color of objB","name of objB");
- objB.sayColor();
4、原型链方法
- function ClassA(){
- }
- ClassA.prototype.color = "color";
- ClassA.prototype.sayColor = function(){
- alert(this.color);
- }
- function ClassB(){
- }
- ClassB.prototype = new ClassA();
- ClassB.prototype.name = "name";
- ClassB.prototype.sayName = function(){
- alert(this.name);
- }
- var objB = new ClassB();
- objB.color = "color of objB";
- objB.name = "name of objB";
- objB.sayColor();
5、混合方式
- function ClassA(sColor){
- this.color = sColor;
- }
- ClassA.prototype.sayColor = function(){
- alert(this.color);
- }
- function ClassB(sColor,sName){
- ClassA.call(this,sColor);
- this.name = sName;
- }
- ClassB.prototype = new ClassA();
- ClassB.prototype.sayName = function(){
- alert(this.name);
- }
- var objB = new ClassB("color of objB","name of objB");
- objB.sayColor();
- JavaScript中的面向对象----继承
- javascript实现面向对象中的继承模板
- JavaScript面向对象------继承
- JavaScript面向对象---继承
- javaScript 面向对象-继承
- JavaScript面向对象------继承
- javascript面向对象-继承
- JavaScript面向对象-继承
- javaScript面向对象-继承
- JavaScript面向对象中的属性,对象创建,继承
- 面向对象中的继承
- 面向对象在javascript中的三大特征之继承
- Javascript面向对象编程:继承
- javascript面向对象编程-->继承
- JavaScript面向对象继承方法
- javascript面向对象二-“继承”
- javaScript 面向对象-继承(一)
- javaScript 面向对象-继承(二)
- Visual Studio.Net 快捷键表
- AJAX在VS2005中的简单应用
- 使用erlang实现的quicksort
- 远程连接数据库
- gcc command argument
- JavaScript中的面向对象----继承
- vc响应最大化最小化按钮的事件或消息
- 十个故事
- java 图片缩放
- 人生就像等公交
- FDD还是TDD?
- Ajax使用POST提交中文乱码问题(转)
- 智能指针auto_ptr使用注意事项
- HTTP协议———zz