javascript:apply函数在面向对象中的用法
1 <html> 2 <head> 3 <title>1</title> 4 </head> 5 <body> 6 <script language='javascript'> 7 function Point(x,y){ 8 this.x=x; 9 this.y=y; 10 this.fun1=function(){ 11 return this.x+'/'+this.y; 12 } 13 } 14 Point.prototype.valueOf=function(){ 15 return this.x+':'+this.y; 16 } 17 function Wu(){ 18 this.x=1; 19 this.y=2; 20 } 21 var p=new Point(26,28); 22 var obj=new Wu(); 23 document.write(p.valueOf()); 24 document.write('<br/>'); 25 document.write(p.fun1.apply(obj)); 26 //因为fun1是原先定义的,所以无需用prototype来加以引用;当然用 27 //prototype的,得用类名Point来引用;而apply的调用,就是valueOf函数的 28 //this指向obj;所以输出来的是1,2 29 alert(Point.prototype.valueOf.apply(obj)); 30 </script> 31 32 </body> 33 </html>