javacript prototype 解密

来源:互联网 发布:车管家软件是什么 编辑:程序博客网 时间:2024/05/29 15:21
看了一下关于prototype的文章,但是,太书面化了,不易理解,我将prototype还原成类对象,和大家分享一下,如果不对,欢迎拍砖! 
Java代码  收藏代码
  1. ///原型对象  
  2. public class prototype  
  3. {  
  4.     //attribute  
  5.     public object constructor;    
  6.     //init  
  7.     public prototype(constructor)  
  8.     {  
  9.        self.constructor = constructor;  
  10.     }  
  11.   
  12.     //调用 prototype  
  13.     public object prototype()  
  14.     {  
  15.        return this.parent; // prototype          
  16.     }  
  17.   
  18.     //设置prototype值  
  19.     public void .=(key,value)  
  20.     {  
  21.        set(key,value);                                
  22.     }  
  23. }  
  24.   
  25. ///Function对象  
  26. public class  function : prototype  
  27. {  
  28.     //attribute  
  29.     public ArrayArguments arguments;  
  30.     public object prototype = prototype();  
  31.     //init  
  32.     public function()  
  33.     {  
  34.         //调用基类  
  35.         super(this);  
  36.         //建立初始化属性  
  37.         //处理 arguments  
  38.     }  
  39.     //call me  
  40.     public callee()  
  41.     {  
  42.         function();  
  43.     }     
  44.   
  45. }  


测试代码 1 
Java代码  收藏代码
  1. function abc()  
  2. {  
  3. };  
  4.   
  5. function cba()  
  6. {  
  7.     this.c = "cba.prototype";  
  8. }  
  9. cba.prototype.d="d";  
  10. //abc对象继承cba对象  
  11. abc.prototype = new cba();  
  12.   
  13. //实例化ABC对象  
  14. var obj = new abc();  
  15. abc.prototype.c = "abc.prototype";  
  16. obj.c = "self";  
  17.   
  18. //打印对象间继承关系  
  19. alert(obj.c);  
  20. alert(abc.prototype.c);  
  21. alert(abc.prototype.d);  
  22. alert(obj.d);  


测试代码 2 
Java代码  收藏代码
  1. function abc()  
  2. {  
  3. };  
  4.   
  5. var obj = new abc();  
  6. obj.c = "self";  
  7. //打印对象属性  
  8. alert(obj.c);  
  9. //打印继承prototype对象属性   
  10. alert(abc.prototype.c); //undefine  
  11. abc.prototype.c = "abc.prototype";  
  12. alert(obj.c); //self  
  13. alert(abc.prototype.c); //"abc.prototype";