---
title: "提速javascript开发(二)"
author: "morfil"
date: "2007-03-31T17:57:17Z"
tags: []
slug: "javascript"
draft: false
lastmod: "2007-03-31T17:57:17Z"
id: 66941
url: "https://morfil.iteye.com/blog/66941"
---

本文引用：[www.iteye.com/topic/58698](http://www.iteye.com/topic/58698)

**例子**：

001. //得到当前对象的名称
002. Object.prototype.getClassName = function()
003. {
004. return this.toString().match(/function\\s\*(\\w+)/)\[1];
005. };
006. /\**
007. * 实现单一多层结构的继承
008. * @param arguments\[0] 指定的类名，非字符串型，不可以是null。
009. \*/
010. Object.prototype.inherit = function()
011. {
012. try{
013. if(!(arguments\[0] instanceof Object)) throw new Error("you can just inherit from a Object");
014. if(this\["parent"]) throw new Error("there have inherited class,you can't inherit class "+arguments\[0].getClassName());
015. }catch(e){
016. alert(e.message);
017. return;
018. }
019. var arg = new Array();
020. for(var i=0; i
021. arg\[i] = arguments\[i+1];
022. var temp = new arguments\[0](arg);
023. for(var i in temp){
024. if(!this\[i])
025. this\[i] = temp\[i];
026. }
027. this\["parent"] = temp;
028. }
029. //用来访问父类的public资源
030. Object.prototype.father = function(){
031. if(this.parent==null) return;
032. if(this.parent\[arguments\[0]]){
033. var temp = this.parent\[arguments\[0]].toString();
034. if(temp.indexOf("function")&lt;0&gt;
035. return this.parent\[arguments\[0]];
036. else{
037. var arg = new Array();
038. for(var i=0; i
039. arg\[i] = arguments\[i+1];
040. return this.parent\[arguments\[0]](arg);
041. }
042. }else{
043. this.parent.father(arguments);
044. }
045. }
046. //与javascript的关键字typeof区别在：o大写
047. Object.prototype.typeOf = function(){
048. var target = arguments\[0];
049. var result = this instanceof target;
050. if(!result){
051. var temp = this.parent;
052. while(temp!=null){
053. result = temp instanceof target;
054. if(!result)
055. temp = temp.parent;
056. else
057. break;
058. }
059. }
060. return result;
061. }
062. //将子类转换成父类，注意:如果子类覆盖了父类的方法，那么换转后的类，该方法的实现仍然是子类中的实现版
063. Object.prototype.toParent = function(){
064. var result = null;
065. var target = arguments\[0];
066. var flag = this instanceof target;
067. if(!flag){
068. var temp = this.parent;
069. while(temp!=null){
070. flag = temp instanceof target;
071. if(!flag)
072. temp = temp.parent;
073. else{
074. result = {};
075. for(var p in temp){
076. if(this\[p])
077. result\[p] = this\[p];
078. else
079. result\[p] = temp\[p];
080. }
081. break;
082. }
083. }
084. }else
085. result = this;
086. return result;
087. }
088. /\**
089. * 下面的继承关系是A inheritB,B inherit C，D是一个测试多态的方法
090. \*/
091. function A(){
092. this.inherit(B,"x");
093. this.inherit(C);
094. var locate1 = "1oh";
095. this.locate2 = "2oh";
096. //A类继承B中的methodB1()，并且在自己的method1中调用B的methodB1
097. this.method1 = function(){
098. alert("A:test继承——I am use B's variant: "+this.methodB1(true));
099. }
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、一个闭包就是当一个函数返回时，一个没有释放资源的栈区。

**闭包的写法**

01. function A(){
02. var answer1 = function(str){
03. alert("这里提供复杂功能1");
04. }
05. var answer2 = function(str){
06. alert("这里提供复杂功能2");
07. }
08. this.getAnswer = function(flag){
09. 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的灵活型。建议使用它，不要走极端：将其对象的一面在需要的时候充分运用，但没有必要任何情况下都这样做。所谓灵活：在不同的情况，可以有适合的方式可用。

（完毕）