Dojo Dijit and HTML5 in Real world applications/University/2. Advanced Java script I

From PMISwiki
Jump to: navigation, search

This is the second lesson on the HTML5, CSS3 and Advanced JavaScript course. PowerPoint presentation used at the lecture

2. Advanced Java script I

The first part of the lecture is based on the objects and arrays examples.

The second part is based on the First class functions demo. A simplified version of dojo/dijit layout is found in the Basic_structure.php.

Objects and arrays

JavaScript is a class-less programming language. However, yet Object Oriented Programming (OOP) is widely used in JavaScript. The following provide an insight to the way to make OOP in JavaScript.

Objects vs. Arrays

JavaScript is object oriented. All variables are objects. Even Arrays are objects. Objects may be defined in three different ways:

<script type="text/javascript">
var Array1 = new Array(1,2,3);
var Array2 = [1, 1, 1, 1];
var carArray = [];
      carArray['name'] = 'carArray';
      carArray['Lfront'] = 1;
      carArray['Rfront'] = 1;
      carArray[3] = 1;
      carArray[4] = 1;
      carArray[5] = function (){return carArray['Lfront'] + carArray['Rfront'] + 
                                                            carArray[3] + 

It is only possible to make a "associative" array using the last example. Some times it difficult the see a difference between an array and an object. JavaScript arrays are meant to be numeric. if you need to hold key/value pairs then it is better practice to use Objects instead. See: How Weird are JavaScript Arrays?

<script type="text/javascript">
    var carObject = {
        name:   'carObject',
        Lfront: 1,
        Rfront: 1,
        Lback:  1,
        Rback:  1,
        CountWeels: function (){return this.Lfront + this.Rfront + this.Lback + this.Lback}

Prototype and differential inheritance

Differential inheritance works as in other object oriented programming languages. Prototype is special: Changing the prototype change all inherited objects: Existing objects and new!

<script type="text/javascript">
    var Obj = function () {   = 'obj';
      this.title  = 'Welcome to advanced javascript';
      this.text_1 = 'First section will be about advanced java script.';
      this.text_2 = 'The second section will be about Dojo.';
    var obj_1 = new Obj(); // differential inheritance = 'Tom';
    Obj.prototype.printname = function (){
        return 'My name is: ' +;

The working example may be found here


This section is based on the First class functions demo. If you would like to start easy a simplified version is also available. We will focus on the following elements:

First class functions

Finally we will discuss the first class function example below:

<script type="text/javascript">
var postData = function(){
    var a = Number( dojo.byId("a").value );
    var b = Number( dojo.byId("b").value );
    var c = Number( dojo.byId("c").value );
        url: "data/object.json",
        handleAs: "json",
        load: function(data,ioargs){
            var x;
            for (x in data){
                dojo.byId("content").innerHTML += data[x]['name'] + ", ";
                if (data[x]['name']=='Luis Villasante'){
                      if([x]['function']) ==
                                                        '[object Function]')
                          data[x]['function']("Hello world!");
                } else {
                    dojo.byId("functions").innerHTML += " "+ data[x]['name'] +
                    ': ' + calculator (a, b, c, data[x]['function'])+'<br>';
dojo.ready(postData); // At start up: get data from server..
function calculator (a, b, c, func){
   return func(a,b,c);
function reset (){
    // Reset HTML elements
    dojo.byId("content").innerHTML   ="";
    dojo.byId("functions").innerHTML ="";

Home work

  • Get the examples to work on your local server.
  • Add more calculators to the "First class functions" example.


Document History

Version Date Author Status Purpose of update
1 14 Feb 2012 PSA Draft Not released.
2 23 Mar 2012 PSA Released Home work added