1.构造继承法可以实现多继承吗?意思是说可以通过this.base可以多次赋值吗?

例21.14 构造继承法(疑问,是否可继承多个类).html

  1. <html> 
  2.     <head> 
  3.         <title>例21.14 构造继承法(疑问,是否可继承多个类)</title>     
  4.     </head> 
  5.     <body> 
  6.         <script> 
  7.         <!-- 
  8.             function dwn(s){ 
  9.                 document.write(s + "<br/>");     
  10.             } 
  11.              
  12.             //定义一个Triangle类型 
  13.             function Triangle(height){ 
  14.                 this.height = function(){ return height; };  
  15.             } 
  16.              
  17.              
  18.             //定义一个Quadrangle类型 
  19.             function Quadrangle(width){      
  20.                 this.width = function(){ return width; } 
  21.             } 
  22.              
  23.              
  24.             //定义一个T类型,它继承Triangle类型,Quadrangle类型 
  25.             function T(){ 
  26.                 this.base = Triangle
  27.                 this.base.apply(this,arguments); 
  28.                  
  29.                 this.base = Quadrangle
  30.                 this.base.apply(this,arguments);                 
  31.             } 
  32.              
  33.             var t = new T(2); 
  34.             dwn(t.height()); 
  35.             dwn(t.width()); 
  36.  
  37.         -->  
  38.         </script> 
  39.     </body> 
  40. </html> 

答案是可以的

2.在原型继承中使用构造继承法,看看构造继承法的属性在对象中是如何表现的:

  1. <script> 
  2.     function dwn(s){ 
  3.             document.write(s + "<br/>");     
  4.     } 
  5.      
  6.     function Point(name){ 
  7.         this.pointName = name
  8.     } 
  9.     Point.prototype = new Object(); 
  10.     function Point2D(){ 
  11.         this.point2DName = "Point2D"
  12.     } 
  13.     //Point2D继承Point 
  14.     Point2D.prototype = new Point("fromPoint2D"); 
  15.      
  16.     function ColorPoint2D(){ 
  17.             this.colorPoint2DName = "ColorPoint2D"
  18.             this.base = Point;//构造继承法 
  19.             this.base.call(this,"fromColorPoint2D"); 
  20.     } 
  21.     //ColorPoint2D继承Point2D 
  22.     ColorPoint2D.prototype = new Point2D(); 
  23.      
  24.     var colorPoint2D = new ColorPoint2D(); 
  25.     dwn("colorPoint2DName:" + colorPoint2D.colorPoint2DName); 
  26.     dwn("point2DName:" + colorPoint2D.point2DName); 
  27.     dwn("pointName:" + colorPoint2D.pointName); 
  28.     delete colorPoint2D.pointName; 
  29.     dwn("删除构造继承法继承来的pointName:" + colorPoint2D.pointName);//在ColorPoint2D通过构造继承法继承了pointName属性,而通过prototype继承了pointName属性 
  30.     delete ColorPoint2D.prototype.pointName; 
  31.     dwn("错误删除prototype.pointName:" + colorPoint2D.pointName); 
  32.     delete Point2D.prototype.pointName; 
  33.     dwn("正确删除prototype.pointName:" + colorPoint2D.pointName); 
  34. </script> 

构造继承法很像java中的类继承,就是在子类中多了一个普通属性;

同时上面也证明了原型继承可以继承静态属性

3.证明原型继承法可以继承静态方法

原型继承法可以继承静态方法.html

  1. <script> 
  2.     function dwn(s){ 
  3.             document.write(s + "<br/>");     
  4.     } 
  5.      
  6.     function Point(name){ 
  7.         this.pointName = name
  8.     } 
  9.     Point.prototype = new Object(); 
  10.     Point.prototype.printPoint = function(){ 
  11.         dwn("继承了Point的静态方法printPoint");  
  12.     } 
  13.     function Point2D(){ 
  14.         this.point2DName = "Point2D"
  15.     } 
  16.     //Point2D继承Point 
  17.     Point2D.prototype = new Point("fromPoint2D"); 
  18.     Point2D.prototype.printPoint2D = function(){ 
  19.         dwn("继承了point2D的静态方法printPoint2D"); 
  20.     } 
  21.      
  22.     function ColorPoint2D(){ 
  23.             this.colorPoint2DName = "ColorPoint2D"
  24.     } 
  25.     //ColorPoint2D继承Point2D 
  26.     ColorPoint2D.prototype = new Point2D(); 
  27.      
  28.      
  29.     var colorPoint2D = new ColorPoint2D(); 
  30.     dwn("colorPoint2DName:" + colorPoint2D.colorPoint2DName); 
  31.     dwn("point2DName:" + colorPoint2D.point2DName); 
  32.     dwn("pointName:" + colorPoint2D.pointName); 
  33.  
  34.      colorPoint2D.printPoint2D(); 
  35.      colorPoint2D.printPoint(); 
  36. </script> 

运行结果:

colorPoint2DName:ColorPoint2D
point2DName:Point2D
pointName:fromPoint2D
继承了point2D的静态方法printPoint2D
继承了Point的静态方法printPoint

4.证明Date中的方法是不可枚举方法

证明Date中的方法是不可枚举方法.html

  1. <script> 
  2.     function dwn(s){ 
  3.         document.write(s + "<br/>");     
  4.     } 
  5.     function EachFieldMethod(){ 
  6.          this.print = function(s){ dwn(s);}  
  7.          this.name = "EachFieldMethod"
  8.     } 
  9.     var EachFieldMethod = new EachFieldMethod(); 
  10.     dwn("可枚举的方法"); 
  11.     for(var each in EachFieldMethod){ 
  12.         dwn(each);   
  13.     }  
  14.      
  15.     dwn("不可枚举的方法"); 
  16.     var date = new Date(); 
  17.     for(var each2 in date){ 
  18.         dwn(each);   
  19.     }  
  20. </script>  

运行结果如下:

可枚举的方法
print
name
不可枚举的方法

Date中的一个属性或者方法都没有枚举出来

 

 

5.重载的toString()方法是否是非枚举类方法?

重载的toString()方法是否是非枚举类方法.html

  1. <html> 
  2.     <head> 
  3.         <title>重载的toString()方法是否是非枚举类方法</title>  
  4.     </head> 
  5.     <body> 
  6.         <script> 
  7.         <!-- 
  8.             Function.prototype.extends = function(obj){ 
  9.                 for(var each in obj){ 
  10.                     this.prototype[each] = obj[each]; 
  11.                 }    
  12.  
  13.             } 
  14.             //定义一个Point类型 
  15.             function Point(dimension){ 
  16.                 this.dimension = dimension; 
  17.                  
  18.                 this.toString = function(){ 
  19.                     return "point";  
  20.                 } 
  21.           } 
  22.            
  23.           /* 
  24.             Point.prototype.toString = function(){ 
  25.                 return "point";  
  26.             } 
  27.             */ 
  28.             function Point2D(){} 
  29.             Point2D.extends(new Point(2)); 
  30.             var point2D = new Point2D(); 
  31.             document.write(point2D.dimension + "<br/>"); 
  32.             document.write(point2D); 
  33.         -->  
  34.         </script> 
  35.     </body> 
  36. </html> 

运行结果:

2
point

上面这个程序证明重载的toString()方法是枚举方法,拷贝构造法通过反射机制拷贝基类对象的所有可枚举数据和方法来模拟“继承”

 6.事件中this的指向问题

  1. <html> 
  2. <head> 
  3.    <title>Example-21.35 this代词的异步问题</title> 
  4. </head> 
  5. <body> 
  6. <button id="btn" name="btn"/> 
  7. <script> 
  8. <!-- 
  9.   //构造一个Object对象 
  10.   var obj = {}; 
  11.   //定义obj对象的foo方法 
  12.   obj.foo = function(){ 
  13.     alert(this == obj); 
  14.     alert(this == btn); 
  15.     alert(this == window); 
  16.   } 
  17.   btn.onclick = obj.foo; 
  18.     //这个问题还比较好理解,可以认为是btn.onclick()做实际调用 
  19.         //而赋值只是将函数引用给了btn.onclick代理(false,true,false) 
  20.   btn.attachEvent("click",obj.foo);//(false,true,false) 
  21.         //而这个也不对,而且this的值是window,怎么说也是一种遗憾 
  22.   btn.attachEvent("click",function(){ obj.foo.call(btn)});//(false,true,false) 
  23.     //还得做这样的修正 
  24.   setTimeout(obj.foo,100);//(false,false,true) 
  25.     //setTime也是一样的,this的值是window 
  26. --> 
  27. </script> 
  28. </body> 
  29. </html> 

上面是opera的运行结果,而firefox和chrome都只能运行第一个,那么IE呢

改写成以下的形式:

例21.35 this代词的异步问题(test).html

  1. <html> 
  2. <head> 
  3.    <title>Example-21.35 this代词的异步问题</title> 
  4. </head> 
  5. <body> 
  6. <button id="btn" name="btn"/> 
  7. <script> 
  8. <!-- 
  9.   //构造一个Object对象 
  10.   var obj = function(){}; 
  11.   //定义obj对象的foo方法 
  12.   obj.foo = function(){ 
  13.     alert(this == obj); 
  14.     alert(this == btn); 
  15.     alert(this == window); 
  16.   } 
  17.   btn.onclick = obj.foo; 
  18.     //这个问题还比较好理解,可以认为是btn.onclick()做实际调用 
  19.         //而赋值只是将函数引用给了btn.onclick代理(false,true,false) 
  20.  // btn.attachEvent("click",obj.foo);//(false,true,false) 
  21.         //而这个也不对,而且this的值是window,怎么说也是一种遗憾 
  22.   //btn.attachEvent("click",function(){ obj.foo.call(btn)});//(false,true,false) 
  23.     //还得做这样的修正 
  24.   //setTimeout(obj.foo,100);//(false,false,true) 
  25.     //setTime也是一样的,this的值是window 
  26. --> 
  27. </script> 
  28. </body> 
  29. </html> 

结果会是怎么样的?

11