JS OO程式学习笔记

来源:互联网 发布:百安居建材 知乎 编辑:程序博客网 时间:2024/04/30 19:35
  1. **  
  2.  * @ fileoverview Javascript OO JS面向对象  
  3. */  
  4. /**  
  5.  * @overview function 形式 赋值需要用this  
  6. */  
  7. function a(){   
  8.     //属性   
  9.    this.name=null;   
  10.    //方法   
  11.    this.setName=function(name){   
  12.       this.name=name;   
  13.    };   
  14.    //方法   
  15.    this.getName=function(){   
  16.       return this.name;   
  17.    };   
  18. }   
  19. /**  
  20.  * Json 变量形式,注意json的格式  
  21. */  
  22. var b={   
  23.     //属性   
  24.    name:"greengnn",   
  25.    //方法   
  26.    run:function(){   
  27.       alert("start run");   
  28.    },   
  29.    //方法   
  30.    sayHello:function(){   
  31.       alert("hello world");   
  32.    }   
  33. }   
  34.   
  35. /**  
  36.  * function方式调用测试  
  37. */  
  38.    var _p=new a();   
  39.    _p.setName("greengnn");   
  40.    alert(_p.getName());   
  41.    alert(_p.name);   
  42. /**  
  43.  * Json方式调用测试  
  44. */  
  45.    b.name;   
  46.    b.run();   
  47.    b.sayHello();  
原创粉丝点击