TDD测试驱动的javascript开发------javascript的继承讲述.doc

TDD测试驱动的javascript开发------javascript的继承讲述.doc

  1. 1、本文档共12页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
TDD测试驱动的javascript开发------ javascript的继承 说起面向对象,人们就会想到继承,常见的继承分为2种:接口继承和实现继承。接口继承只继承方法签名,实现继承则继承实际的方法。 由于函数没有签名,在ECMAScript中无法实现接口继承,只支持实现继承。 1. 原型链 1.1 原型链将作为实现继承的主要方法,基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法。 构造函数---原型---实例 之间的关系: 每一个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。 function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = function() { return this.property; }; function SubType() { this.subproperty = false; } SubType.prototype = new SuperType(); //通过原型链实现继承 SubType.prototype.getSubValue = function() { return this.subproperty; }; var subInstance = new SubType(); var superInstance = new SuperType(); TestCase(test extends,{ test superInstance property should be true : function() { assertEquals(true,superInstance.property); }, test superInstance getSuperValue() should be return true : function() { assertEquals(true,superInstance.getSuperValue()); }, test subInstance property should be false : function() { assertEquals(false,subInstance.subproperty); }, test subInstance could visit super method : function() { assertEquals(true,subInstance.getSuperValue()); //SubType继承SuperType,并调用父类的方法 } }); 注:要区分开父类和子类的属性名称,否则子类的属性将会覆盖父类的同名属性值:看如下代码: function SubType() { this.property = false; } SubType.prototype.getSubValue = function() { return this.property; }; function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = function() { return this.property; }; SubType.prototype = new SuperType(); //通过原型链实现继承 var subInstance = new SubType(); var superInstance = new SuperType(); TestCase(test extends,{ test superInstance property should be true : function() { assertEquals(true,superInstance.property); //父类的property值为true }, test superInstance getSuperValue() should be return true : function() { assertEquals(true,superInstance.getSuperValue()); //superInstance调用方法 }, test subInstance property should be false : function() { assertEquals(false,subInstance.property); //子类的prope

文档评论(0)

jiayou10 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:8133070117000003

1亿VIP精品文档

相关文档