<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>Javascript子类与继承</title> </head> <body> <script type="text/javascript"> //定义一个基类 function Rectangle(w,h) { this.width = w; this.height=h; } Rectangle.prototype.area = function(){return this.width * this.height;}; //定义基类的一个方法 Rectangle.prototype.toString = function(){ return "[" + this.width + "," + this.height + "]";}; //继承Rectangle,定义子类的构造函数 function PositonRectangle(x,y,w,h) { Rectangle.call(this,w,h);//调用基类方法,使用call方法,将基类的this替换为当前的this this.x = x; this.y = y; } //继承基类的方法 PositonRectangle.prototype = new Rectangle();//这样赋值后,原来Rectangle的prototype也被当成PositonRectangle.prototype的直接属性了. //prototype这个相当于类属性,只有类才能访问(这里是函数名称.prototype这样访问),当使用new示例化出的每一个对象都保留了对该对象类的prototype的引用 //是的每个对象都可以访问prototype中的属性 //是的PositonRectangle.prototype.prototype 没有,因此下面的语句 输出为 undefined //alert(PositonRectangle.prototype.prototype);//test //alert(typeof PositonRectangle.area);//输出 undefined //alert(typeof (new PositonRectangle(2,2,2,2)).area);//输出function,即,prototype中的属性,只有使用new示例话了一个对象后,才会产生,否则为undefined //删除基类的 width和height,因为子类中已经有了通过 子类 构造函数中 Rectangle.call(this,w,h); 这句实现 delete PositonRectangle.prototype.width; delete PositonRectangle.prototype.height; //修改子类的constructor属性,因为子类是通过构造函数中 Rectangle.call(this,w,h); 这句实现,使得子类的constructor变成了基类的constructor(Rectangle) PositonRectangle.prototype.constructor = PositonRectangle; //现在可以添加子类方法了 PositonRectangle.prototype.contains = function(x,y) { return(x > this.x && x < this.x + this.width && y > this.y && y < this.y + this.height); } //覆盖基类的toString方法 PositonRectangle.prototype.toString = function() { return "(" + this.x + "," + this.y + ")" + //子类的字段 Rectangle.prototype.toString.apply(this);// 特殊方式调用基类方法,必须使用这种方式调用,无法使用子类方法 } //使用子类,定义一个子类对象,调用子类方法和从基类中继承的方法 var r = new PositonRectangle(2,2,2,2); document.writeln(r); document.writeln(r.contains(3,3)); document.writeln(r.area()); //使用子类的字段 document.writeln(r.x + "," + r.y + "," + r.width + "," + r.height ); document.writeln(r instanceof PositonRectangle && r instanceof Rectangle && r instanceof Object); //true </script> </body> </html>
相关推荐
选定基类后,就可以创建它的子类了。...子类还可添加超类中没有的新属性和方法,也可以覆盖超类中的属性和方法。 4.2.1 继承的方式 和其他功能一样,ECMAScript中实现继承的方式不止一种。这是因为JavaScr
在JavaScript 里,被继承的函数称为超类型(父类,基类也行,其他语言叫法),继承的函数称为子类型(子类,派生类)。继承也有之前问题,比如字面量重写原型会中断关系,使用引用类型的原型,并且子类型还无法给超类型...
深入学习javascript继承之前,先了解下面的几个概念: 父类:被继承的类 子类:由继承得来的类 超类:也就是父类 抽象类:一般不用来实例化的类,它的用途是用来给其他类继承. 基类:提供给其他类可以继承的类 派生类:由基类...
在这个例子中,形状(Shape)是椭圆形(Ellipse)和多边形(Polygon)的基类(base class)(所有类都由它继承而来)。椭圆具有一个属性foci,说明椭圆具有的焦点的个数。圆形(Circle)继承了椭圆形,因此圆形是...
1. 创建基类 首先考虑Polygon类。哪些属性和方法是必需...注意,Polygon类不够详细精确,还不能使用,方法getArea()返回0,因为它只是一个占位符,以便子类覆盖。 2. 创建子类 现在考虑创建Triangle类。三角形具有三条
代码如下: [removed] //创建基类 function Person(name, age) { this.name = name; this.age = age; } //通过原型方式给基类添加函数(这样可以服用此函数) Person.prototype.showName = function () { alert(...
简单扩展用于继承和子类化的简单“扩展”助手。 改编自 Backbone.js 的和 CoffeeScript。 这与类似,只是它没有任何依赖项。用法将其分配给您的基类的.extend : function Shape ( ) { ... }Shape . extend = ...
1.确定基类 2.创建子类 3.测试代码 4.一波完整的代码 5.引发的思考 6.补充知识:极晚绑定
从大多数基类开始,默认构造函数由继承链调用。 可以通过以下调用访问超级构造函数: this . __super . constructor ( parameterA ) ; 另外, this.__super可用于访问重写的super方法: doLogic: function ( ) ...
泛型技巧系列:避免基类及接口约束 New Article 不该用Generics实现Abstract Factory的理由 C#2.0-泛型 C#2.0-extern C#2.0-可空类型 C#2.0-分部类 C#2.0-迭代器 C#2.0 的新增功能学习 泛型的序列化问题 .NET 2.0 ...
安装使用npm: $ npm install chasma --save 与二重奏: import * as chasma from 'littlstar/chasma' ;用法chasma提供了可以继承的Screen类。 屏幕可能有孩子。 屏幕也可以有一个父级。 屏幕可以侦听和调度任意事件...
3.1.3 子类结构 57 3.2 渲染框架 57 3.2.1 框架结构 58 3.2.2 摄像机类(CCCamera) 59 3.2.3 导演类(CCDirector) 59 3.2.4 场景类(CCScene) 62 3.2.5 图层类(CCLayer) 64 3.2.6 精灵类(CCSprite) 68 3.2.7 ...