Introduction to JavaScript ;-)

If you are new to JavaScript this note may help you better understand the ActiveWidgets source code. I just want to mention here three things where JavaScript is very different from the common programming languages like C++ or VB.

First thing is the prototype-based inheritance. You can probably find the details in any JavaScript book, but here is the short summary:
- an object instance is created using the 'new' keyword and a constructor function.
- The constructor function may accept arguments and initialize some properties of the object. If the arguments are not necessary you can skip the brackets, i.e. var obj = new Constructor; instead of var obj = new Constructor();
- The constructor function has a property called 'prototype' which is the reference to some other object instance. This 'prototype' object works as a template to the new objects created using the constructor function. The new object 'inherits' all properties and methods of the prototype object. It does not mean that the properties or methods are physically copied. Only the reference to the prototype object exists. However when asked for a particular property or executing a method the JavaScript engine will look up through the prototypes chain until a property or method with the requested name is found. This relationship is dynamic - changes made in the prototype object are immediately reflected in all dependent objects. This dynamic lookup feature only applies to 'read property' and 'execute method' operations. The 'write' operation (assigning value to a property) always works on the exact object instance. If the property does not exist – it will be created on the fly.

Another important feature of JavaScript is how the functions are implemented. Any function can be treated simply as a different type of object. You can create functions using the same constructor syntax:

var myFunction = new Function("return null");

which is the same as
var myFunction = function(){return null};

and same as
function myFunction(){
return null;

Because functions are 'just objects' you can do all sorts of things with them which you would do with 'normal' objects. You can pass them as arguments to other functions, you can assign them to object properties so these properties become methods, you can compare them and convert to strings. And of course functions can have properties (and methods) too.

And finally a couple of words about arrays and array indices. JavaScript 'objects' are associative arrays of properties and methods (which is the same as 'collections' in VB world). JavaScript 'arrays' are also the same associative arrays. The only difference between an array and an object in JavaScript is that an array will maintain the 'length' property for you (which is the max numeric index used for 'write' operation + 1). So array[1] is the same as array["1"] and the same as object[1]. And object["method"]() is just another syntax for object.method().

To be continued...
Alex (ActiveWidgets)
April 14,

June 23,
Well I think you have forgotten the most important part (at least something that I have learned from your code) - Inheritance with the magic word "prototype". This is something that makes programming using Javascript finally cleaner and better structured.
June 24,

This topic is archived.

See also:

Back to support forum