JavaScript DHTML/Dojo toolkit/Class Definition

Материал из Web эксперт
Перейти к: навигация, поиск

Extend class

 
<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/"
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      function FirstName() {
        var firstName = "first";
        this.getFirstName = function() {
          return firstName;
        }
      }
      function LastName() {
        var lastName = "last";
        this.getLastName = function() {
          return lastName;
        }
      }
      function NameOutput() {
        this.sayName = function() {
          alert(this.getFirstName() + " " + this.getLastName());
        }
      }
      dojo.extend(NameOutput, new FirstName(), new LastName());
      var name = new NameOutput();
      name.sayName();
    </script>
  </head>
  <body></body>
</html>



Use dojo to create class

 
<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/"
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      function FirstName() {
        var firstName = "A";
        this.getFirstName = function() {
          return firstName;
        }
      }
      function LastName() {
        var lastName = "F";
        this.getLastName = function() {
          return lastName;
        }
      }
      dojo.declare("com.wbex.AnotherClass", [ FirstName, LastName],
        { middleName : "W",
          getMiddleName : function() {
            return this.middleName;
           }
         }
       );
       var o = new com.wbex.AnotherClass();
       alert(o.getFirstName() + " " + o.getMiddleName() + " " + o.getLastName());
    </script>
  </head>
  <body></body>
</html>



Use package name in function definition

 
<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/"
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
  </head>
  <body>
    <script>
      function com() { }
      
      com.wbex = function() { };
      
      com.wbex.method1 = function() { return "method1 called"; }
      
      alert(dojo.getObject("com.wbex.method1")() + ", " +
        dojo.getObject("com.wbex.method2", true) + ", " +
        dojo.getObject("com.wbex.method2", false));
    </script>
  </body>
</html>