zoukankan      html  css  js  c++  java
  • javascript 关于类得5种写法

    定义Circle类,拥有成员变量r,常量PI和计算面积的成员函数area()
    Javascript代码

    //第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()); 

    Javascript代码

    //第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 ) ); 

    Javascript代码

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

    alert( Circle.Area( 1.0 ) ); 

    Javascript代码

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

    }; 
    alert( Circle.area(1.0) ); 

    Javascript代码

    //第5种写法 
    var Circle = new Function("this.PI = 3.14159;this.area = function( r ) {return r*r*this.PI;}"); 
    alert( (new Circle()).area(1.0) );
  • 相关阅读:
    高斯消元法求逆矩阵
    二分法
    Boost库
    sorts
    排序算法_HeapSort
    粒子系统之概述_20140101
    新年祝福!_20140101
    思维_逆向的魅力2_20131229
    思维_逆向的魅力_20131229
    编码能力的提升?_20131228
  • 原文地址:https://www.cnblogs.com/cr7/p/2036533.html
Copyright © 2011-2022 走看看