OOP in javascript-模仿類

來源:互聯網
上載者:User
  1. javascript中模仿類function Circle () {
       }

      上面的代碼就聲明了一個類,準確說是一個物件建構器(constructor),這是一個空類,沒有任何屬性和方法,沒有什麼意義,我們可以添加需要的屬性和方法。

  2. 屬性和方法function Circle (xPoint, yPoint, radius) {
        this.x = xPoint;  // The x component of the center of the circle.
        this.y = yPoint;  // The y component of the center of the circle.
        this.r = radius;  // The radius of the circle.
          this.getArea()=function(){Math.PI*this.r*this.r;};  //return area of the circle.

    circle包含3個屬性和一個方法,x,y表示座標,r表示半徑,getArea方法返回圓的面積。

  3. 執行個體化var circle = new Circle(1,2,5);//instantiate a Circle object.
    var circleArea = circle.getArea();//get the area of the circle;

    通過關鍵字來執行個體化一個類,對象點操作符來訪問對象方法和屬性。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.