• JS 中面向对象的5钟写法


    JS 中面向对象的5钟写法

    Java代码
    //第1种写法  
    function Circle(r) {  
          this.r = r;  
    }  
    Circle.PI = 3.14159;  
    Circle.prototype.area = function() {  
      return Circle.PI * this.r * this.r;  
    }  
     
    var c = new Circle(1.0);     
    alert(c.area());  

    //第1种写法
    function Circle(r) {
          this.r = r;
    }
    Circle.PI = 3.14159;
    Circle.prototype.area = function() {
      return Circle.PI * this.r * this.r;
    }

    var c = new Circle(1.0);  
    alert(c.area());

    Java代码
    //第2种写法  
    var Circle = function() {  
       var obj = new Object();  
       obj.PI = 3.14159;  
         
       obj.area = function( r ) {  
           return this.PI * r * r;  
       }  
       return obj;  
    }  
     
    var c = new Circle();  
    alert( c.area( 1.0 ) ); 

    //第2种写法
    var Circle = function() {
       var obj = new Object();
       obj.PI = 3.14159;
      
       obj.area = function( r ) {
           return this.PI * r * r;
       }
       return obj;
    }

    var c = new Circle();
    alert( c.area( 1.0 ) );

    Java代码
    //第3种写法  
    var Circle = new Object();  
    Circle.PI = 3.14159;  
    Circle.Area = function( r ) {  
           return this.PI * r * r;  
    }  
     
    alert( Circle.Area( 1.0 ) ); 

    //第3种写法
    var Circle = new Object();
    Circle.PI = 3.14159;
    Circle.Area = function( r ) {
           return this.PI * r * r;
    }

    alert( Circle.Area( 1.0 ) );

    Java代码
    //第4种写法  
    var Circle={  
       "PI":3.14159,  
     "area":function(r){  
              return this.PI * r * r;  
            }  
    };  
    alert( Circle.area(1.0) ); 

    //第4种写法
    var Circle={
       "PI":3.14159,
     "area":function(r){
              return this.PI * r * r;
            }
    };
    alert( Circle.area(1.0) );

    Java代码
    //第5种写法  
    var Circle = new Function("this.PI = 3.14159;this.area = function( r ) {return r*r*this.PI;}");  
     
    alert( (new Circle()).area(1.0) ); 

  • 相关阅读:
    iOS 苹果开发证书失效的解决方案(Failed to locate or generate matching signing assets)
    iOS NSArray数组过滤
    App Store2016年最新审核规则
    iOS 根据字符串数目,自定义Label等控件的高度
    iOS 证书Bug The identity used to sign the executable is no longer valid 解决方案
    Entity FrameWork 增删查改的本质
    EF容器---代理类对象
    Entity FrameWork 延迟加载本质(二)
    Entity FrameWork 延迟加载的本质(一)
    Entity FrameWork 增删查改
  • 原文地址:https://www.cnblogs.com/lds85930/p/1541326.html
Copyright © 2020-2023  润新知