详解extextend的使用方法

详解extextend的使用方法

ID:9485417

大小:125.50 KB

页数:14页

时间:2018-05-01

详解extextend的使用方法_第1页
详解extextend的使用方法_第2页
详解extextend的使用方法_第3页
详解extextend的使用方法_第4页
详解extextend的使用方法_第5页
资源描述:

《详解extextend的使用方法》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、extend (Objectsubclass,Objectsuperclass,[Objectoverrides]:Object 第一个参数:子类 第二个参数:父类 第三个参数:要覆盖的属性。 这里需要强调一下,子类继承下来的是父类中通过superclass.prototype方式定义的属性(包括用此方法定义的函数)。使用方式使用示例假设有个 function 名为 SuperClass ,要实现一个子类,名为 MyClass 。下面的两种方式都可以实现这个功能。MyClass = Ext . extend ( SuperCl

2、ass , { /**/ });Ext . extend ( MyClass , SuperClass , { /**/ }); 下面来个具体示例:var a = function ( id ){    this . id = id ;}a . prototype = {    tostring : function (){        return this . id ;    }};           b = function ( id ){    b . superclass . constructor . call

3、 ( this , id );}Ext . extend ( b , a , {    tostring : function (){        return String . format ( "b:{0}" , this . id );    }});// 测试一下var obj1 = new a ( "obj1" );alert ( obj1 . tostring ());var obj2 = new b ( "obj2" );alert ( obj2 . tostring ());或者下面的代码,可以得到同样的效果

4、:var a = function ( id ){    this . id = id ;}a . prototype = {    tostring : function (){       return this . id ;    }}; b = Ext . extend ( a , {    tostring : function (){       return String . format ( "b:{0}" , this . id );    }});// 测试一下var obj1 = new a ( "obj

5、1" );alert ( obj1 . tostring ());var obj2 = new b ( "obj2" );alert ( obj2 . tostring ()); 一个错误例子下面看个示例:BaseClass = function () {    this . f1 = function () {        alert ( "f1inbase" );    }     this . f2 = function () {        alert ( "f2inbase" );    }} ChildClas

6、s = function () {  ChildClass . superclass . constructor . call ( this );}        Ext . extend ( ChildClass , BaseClass , {    f1 : function () {        alert ( "f1inchild" );    },     f3 : function () {        alert ( "f3inchild" );    }}); var b = new ChildClass 

7、();b . f1 ();b . f2 ();b . f3 (); 可以去执行一下,可以发现 f1 的执行结果仍然是 "f1inbase" 。并没有真正的达到 override 的效果。 Ext.extendputsthepropertiesspecifiedinthe3rdargumentintothe subclass's prototype 也就是说:第三个参数里面的函数被放置在了子类的 prototype 中。而在 ChildClass.superclass.constructor.call(this); 这句上, B

8、aseClass 的 f1 成了 ChildClass 的变量,而不是 ChildClass.prototype 。通过对 JavaScript 的原型继承的了解,可以知道,实例变量的优先级是高于 prototype 的,所以上面的这个代码是达不到 override 的功能的

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。