彻底被Extjs类的继承打败,大侠来save我吧
erichua
2008-08-23
代码如下
//父类定义 BaseClass = function(){ // //begin this.cry = function(){//this.不能丢 alert("this.cry ...."); } //end return { cry: function(){ alert("return crying...."); } }; } BaseClass.prototype.name = ""; BaseClass.prototype.say = function(){ alert("say parent"); } BaseClass.prototype.talk = function(){ alert("talk parent"); } //子类定义 SubClass = function(){ SubClass.superclass.constructor.call(this);//调用父类构造函数,也就是执行BaseClass里的begin--->end中间的代码。如果这行代码注释掉,则begin---->end中间的代码不会初始化,但是父类用prototype构造的属性和方法任然会初始化 //js里子类实例化时是不会去自己调用父类的构造函数的 this.walk = function(){//this.必须要加上,否则SubClass的实例对象没有办法引用到该walk方法 alert("i'm warking"); } } //继承,第3个参数{},覆盖或者扩展父类方法,该参数也可以不要 Ext.extend(SubClass, BaseClass, { say: function(){ alert("overide say"); }, jump: function(){ alert("jumping"); } }); var sb = new SubClass(); sb.say();//overide say sb.walk();//i'm warking sb.jump();//jumping sb.talk();//talk parent sb.cry();//this.crying .... var bc=new BaseClass(); bc.cry();//返回return crying.... 为什么同样为BaseClass里面this、 prototype、var 里面同样的函数为什么返回结果会都不一样。 |
|
jianfeng008cn
2008-08-23
http://jianfeng008cn.iteye.com/blog/195409
|
|
lugreen
2008-08-23
你在基类中定义了返回了一个对象{}
当直接创建基类对象时返回实际是基类构造器中的返回值 |
|
erichua
2008-08-24
# cry: function(){
# alert("return crying...."); # } 这个是函数呀。怎么是对象呢?? |
|
chanball
2008-08-26
erichua 写道 # cry: function(){
# alert("return crying...."); # } 这个是函数呀。怎么是对象呢?? js中的函数也是一个类! |
|
lugreen
2008-08-26
js中函数也是一个对象
|
|
erichua
2008-08-27
基本搞清楚了。
JavaScript里全部是对象,只是范围不同,因此定义在var this prototype中效果不一样。理解是理解了。但如何在一个application中很好的利用这些特性呢。有经验者一起讨论哦。 |