ZK's JavaScript Extension

From Documentation

To make it easier for JavaScript objects to represent widgets ZK has introduced a class concept to JavaScript. Here is a brief introduction on defining a class in JavaScript.

To define a new class in JavaScript, we use zk.$extends(Class, Map, Map).

com.foo.Location = zk.$extends(zk.Object, {
 x: 0,
 y: 0,
 distance: function (loc) {
  return Math.sqrt(Math.pow(this.x - loc.x, 2) + Math.pow(this.y - loc.y, 2));
 find: function (name) {
  if (name == 'ZK')
   return new com.foo.Location(10, 10);
  throw 'unknown: "+name;

The first argument of zk.$extends(Class, Map, Map) is the base class to extend from. In this case, we extend from Object, which is the root of the class hierarchy. The second argument consists of the (non-static) members of the class. In this case, we define two data members (x and y) and one method (distance).

The third argument is optional and if omitted means that the extended class will contain no static members. In the example we define a static method (find). For now this is all that is required for us to create our first component so let’s move onto talk about the component implementation.

Last Update : 2010/07/14

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.