ITeye博客

提速javascript开发(二)

morfil · 2007-03-31 17:57 · JavaScript · Markdown

本文引用:www.iteye.com/topic/58698

例子:

  1. //得到当前对象的名称
  2. Object.prototype.getClassName = function()
  3. {
  4. return this.toString().match(/function\s*(\w+)/)[1];
  5. };
  6. /**
  7. * 实现单一多层结构的继承
  8. * @param arguments[0] 指定的类名,非字符串型,不可以是null。
  9. */
  10. Object.prototype.inherit = function()
  11. {
  12. try{
  13. if(!(arguments[0] instanceof Object)) throw new Error("you can just inherit from a Object");
  14. if(this["parent"]) throw new Error("there have inherited class,you can't inherit class "+arguments[0].getClassName());
  15. }catch(e){
  16. alert(e.message);
  17. return;
  18. }
  19. var arg = new Array();
  20. for(var i=0; i
  21. arg[i] = arguments[i+1];
  22. var temp = new arguments[0](arg);
  23. for(var i in temp){
  24. if(!this[i])
  25. this[i] = temp[i];
  26. }
  27. this["parent"] = temp;
  28. }
  29. //用来访问父类的public资源
  30. Object.prototype.father = function(){
  31. if(this.parent==null) return;
  32. if(this.parent[arguments[0]]){
  33. var temp = this.parent[arguments[0]].toString();
  34. if(temp.indexOf("function")<0>
  35. return this.parent[arguments[0]];
  36. else{
  37. var arg = new Array();
  38. for(var i=0; i
  39. arg[i] = arguments[i+1];
  40. return this.parent[arguments[0]](arg);
  41. }
  42. }else{
  43. this.parent.father(arguments);
  44. }
  45. }
  46. //与javascript的关键字typeof区别在:o大写
  47. Object.prototype.typeOf = function(){
  48. var target = arguments[0];
  49. var result = this instanceof target;
  50. if(!result){
  51. var temp = this.parent;
  52. while(temp!=null){
  53. result = temp instanceof target;
  54. if(!result)
  55. temp = temp.parent;
  56. else
  57. break;
  58. }
  59. }
  60. return result;
  61. }
  62. //将子类转换成父类,注意:如果子类覆盖了父类的方法,那么换转后的类,该方法的实现仍然是子类中的实现版
  63. Object.prototype.toParent = function(){
  64. var result = null;
  65. var target = arguments[0];
  66. var flag = this instanceof target;
  67. if(!flag){
  68. var temp = this.parent;
  69. while(temp!=null){
  70. flag = temp instanceof target;
  71. if(!flag)
  72. temp = temp.parent;
  73. else{
  74. result = {};
  75. for(var p in temp){
  76. if(this[p])
  77. result[p] = this[p];
  78. else
  79. result[p] = temp[p];
  80. }
  81. break;
  82. }
  83. }
  84. }else
  85. result = this;
  86. return result;
  87. }
  88. /**
  89. * 下面的继承关系是A inheritB,B inherit C,D是一个测试多态的方法
  90. */
  91. function A(){
  92. this.inherit(B,"x");
  93. this.inherit(C);
  94. var locate1 = "1oh";
  95. this.locate2 = "2oh";
  96. //A类继承B中的methodB1(),并且在自己的method1中调用B的methodB1
  97. this.method1 = function(){
  98. alert("A:test继承——I am use B's variant: "+this.methodB1(true));
  99. }
  100. //A类覆盖B中的method2(),并且在自己的method2中调用B的method2
  101. this.method2 = function(){
  102. var str = "A:test覆盖method2,result is:"+this.father("method2","ok");
  103. alert(str);
  104. }
  105. //A类覆盖了类C中的method3
  106. this.method3 = function(str){
  107. return "excuting class A's method";
  108. }
  109. }
  110. function B(){
  111. this.inherit(C,"y");
  112. var locateB2 = "";
  113. var locateB1 = arguments[0];
  114. this.methodB1 = function(flag){
  115. if(flag)
  116. return locateB1;
  117. else
  118. return "A converted B is success";
  119. }
  120. this.method2 = function(){
  121. var str =arguments[0]+" B's "+locateB1+" "+this.method1();
  122. return str;
  123. }
  124. //B类覆盖了类C中的method3
  125. this.method3 = function(str){
  126. return "excuting class B's method";
  127. }
  128. }
  129. function C(){
  130. var locate1 = arguments[0];
  131. this.method1 = function(){
  132. return "C's "+locate1;
  133. }
  134. this.method3 = function(){
  135. return locate1;
  136. }
  137. }
  138. /**
  139. * 在这个方法中,只会对C类的method3方法,如果参数是C的子类也会处理这个参数的method3方法。
  140. */
  141. function D(){
  142. var target = arguments[0];
  143. if(!target.typeOf(C)){
  144. alert("upper my scope");
  145. return;
  146. }
  147. alert("test多态:"+target.method3());
  148. }
  149. var a = new A;
  150. a.method1();//在其中调用了从B中继承过来的methodB1()
  151. a.method2();//A类覆盖B中的method2(),并且在自己的method2中调用B的method2
  152. var temp = a.toParent(B);
  153. alert(temp.methodB1(false)+": "+temp.method3());//转换成父类后,对象执行的方法仍然是子类版的。
  154. var b = new B("b^-^");
  155. D(a);
  156. D(b);
  157. //现在业务需要,扩展出一个E
  158. function E(){
  159. this.inherit(C,"change is so much!");
  160. }
  161. D(new E());//扩展性与通用性的表现

【注意事项】:

1) 需要用这种继承的写法,请将下面的方法当作javascript 的api,不要覆盖。

方法名称

方法作用

参数说明

getClassName

获得对象的名称。

无

inherit

实现对象之间的单一多层的继承

1:被继承的父类名,非字符串

2:父类初始化需要的参数

father

如果子类覆盖了父类的某个方法,用它来回调父类的这个方法

1:回调的方法名称,字符串格式

2:回调方法需要的参数

typeOf

判断子类的类型,与typeof是不一样的。

类的名称,非字符串形式。

toParent

将子类转换成父类

父类的名字,非字符串形式。

2) 在子类的第一行执行继承。如果要覆盖父类的某个方法,并且要用父类的这个方法,那么请在子类的方法中回调父类的那个方法。

 

3.3、活用闭包

所谓闭包,通俗的讲,使用函数的范围,已经不在函数的定义范围。

优点:进一步提高代码的封装性。

缺点:ie6的垃圾收集机制存在着bug,当进行ui编码时会出现一些问题。

闭包的特点

1、作为一个函数变量的一个引用 - 当函数返回时,其处于激活状态。
2、一个闭包就是当一个函数返回时,一个没有释放资源的栈区。

闭包的写法

  1. function A(){
  2. var answer1 = function(str){
  3. alert("这里提供复杂功能1");
  4. }
  5. var answer2 = function(str){
  6. alert("这里提供复杂功能2");
  7. }
  8. this.getAnswer = function(flag){
  9. if(flag)
  10. return answer1;
  11. else
  12. return answer2;
  13. }
  14. }
  15. var control = new A();
  16. var a1 = control.getAnswer(true);
  17. var a2 = control.getAnswer(false);
  18. a1();//弹出文字:这里提供复杂功能1
  19. a2();//弹出文字:这里提供复杂功能2
  20. /**
  21. * 1)通过闭包,对象A将自己的资源提供给了外部。
  22. * 2)并且外部的代码对a1之类的变量做出了改变,不会影响到A对象内部
  23. * 下面对A类稍作修改,我们看看效果
  24. */
  25. function A(){
  26. var answer1 = function(str){
  27. alert("这里提供复杂功能1");
  28. }
  29. var answer2 = function(str){
  30. alert("这里提供复杂功能2");
  31. }
  32. this.getAnswer = function(flag){
  33. if(flag)
  34. return answer1;
  35. else
  36. return answer2;
  37. }
  38. this.answer = function(){
  39. answer1();
  40. }
  41. }
  42. var control = new A();
  43. var a1 = control.getAnswer(true);
  44. a1();
  45. a1 = "1";
  46. control.answer();//弹出文字:这里提供复杂功能1

3.4、函数式编程

它的基本特点是:将函数(或对象)赋值给一个变量。前面我们已经采用了这种写法。

优点:代码可读性好。适合web页ui方面的开发

错误的写法

function A(){        alert(B());        var B = function(){            return "ok";        }    }    A();  

正确的写法

function A(){        var B = function(){            return "ok";        }        alert(B());     }    A();  

附:javascript是非常灵活的:

1)基于对象的:有对象与类的概念,也有独立于对象的函数概念

2)解释型:请把握住运行时与时间轴这个概念:它能自动转化变量的类型与这个特点紧密相连;一段代码是当作函数,还是一个类,也与这个有关。

2)弱类型:会将变量自动转化为当前合适的类型或者基本数据类型,这个特点基于它是解释型的语言。

这些充分体现了javascript的灵活型。建议使用它,不要走极端:将其对象的一面在需要的时候充分运用,但没有必要任何情况下都这样做。所谓灵活:在不同的情况,可以有适合的方式可用。

(完毕)

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。