MooTools - 程式結構



MooTools 是一種可用於設計面向物件模型的工具。在本章中,讓我們討論 MooTools 庫的一個簡單示例。

示例

我們將使用 Class 設計一個名為 Rectangle 的模型。為此,我們需要宣告屬性 — 寬度和高度。

檢視以下程式碼,並將其儲存到 sample.html 中。

<html>

   <head>
      <script type = "text/javascript" src = "MooTools-Core-1.6.0.js"></script>
      <script type = "text/javascript" src = "MooTools-More-1.6.0.js"></script>
      
      <script type = "text/javaScript">
         var Rectangle = new Class({
            //properties
            width: 0,
            height: 0,
            
            //methods
            initialize: function(widthVal, heightVal) {
               this.width = widthVal;
               this.height = heightVal;
            },
            details: function() {
               document.write("Welcome to MooTools demo program");
               document.write("Width: "+this.width+" Height: "+this.height);
            },
         });
         var rec = new Rectangle(5,4);
         rec.details();
      </script>
   </head>
   
   <body>
   </body>
   
</html>

你將收到以下輸出 −

輸出

廣告
© . All rights reserved.