MooTools - 程序结构

MooTools 是一种可用于设计面向对象模型的工具。本章中我们来讨论 MooTools 库的一个简单示例。

示例

在这里我们将使用 Class 设计一个名为 Rectangle 的模型。为此,我们需要声明属性 — Width 和 Height。

查看以下代码,并将其保存到 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>

您将收到以下输出 −

输出