1 #!/usr/local/bin/node
  2 
  3 /**
  4  * @fileoverview  This example will try to showcase some of the great features
  5  *  of the closure compiler.  Such things as:
  6  * <ul>
  7  *  <li>Encapsulisation</li>
  8  *  <li>Interfaces</li>
  9  *  <li>Type Safety</li>
 10  *  <li>Code Optimisation</li>
 11  *  <li>Compile Time Checkings</li>
 12  *  <li>Code Documentaion</li>
 13  *  <li>
 14  *    Provides a Scalable Framework for Enterprise JavaScript Development.
 15  *  </li>
 16  * </ul>
 17  */
 18 
 19 /*
 20  * Does not require an opts parameter as we are providing all the options in
 21  * the closure.json file in this directory;
 22  */
 23 require('nclosure').nclosure();
 24 
 25 /*
 26  * Now that the nclosure is initialised you can use any base.js functionality
 27  * such as goog.require / goog.provide
 28  *
 29  * Note: At least one namespace must be provided for compilation purposes
 30  */
 31 
 32 goog.provide('nclosure.examples.animals.Example');
 33 
 34 goog.require('goog.array');
 35 goog.require('nclosure.examples.animals.Cat');
 36 goog.require('nclosure.examples.animals.Dog');
 37 goog.require('nclosure.examples.animals.IAnimal');
 38 goog.require('nclosure.examples.animals.Monkey');
 39 goog.require('nclosure.examples.animals.Tiger');
 40 
 41 
 42 
 43 /**
 44  * @constructor
 45  */
 46 nclosure.examples.animals.Example = function() {
 47   /**
 48    * @private
 49    * @type {Array.<nclosure.examples.animals.IAnimal>}
 50    */
 51   this.animals_ = this.initRandomAnimals_();
 52   this.makeAnimalsTalk_();
 53   console.log('Bye!');
 54 };
 55 
 56 
 57 /**
 58  * @private
 59  * @return {Array.<nclosure.examples.animals.IAnimal>} A colleciton of
 60  *    random animals.
 61  */
 62 nclosure.examples.animals.Example.prototype.initRandomAnimals_ = function() {
 63   /** @type {Array.<nclosure.examples.animals.IAnimal>} */
 64   var animals = [];
 65   var types = [
 66     nclosure.examples.animals.Dog,
 67     nclosure.examples.animals.Cat,
 68     nclosure.examples.animals.Tiger,
 69     nclosure.examples.animals.Monkey
 70   ];
 71   var len = parseInt(10 + (Math.random() * 10), 10);
 72   console.log('Creating ' + len + ' random animals');
 73   for (var i = 0; i < len; i++) {
 74     var t = types[parseInt(Math.random() * types.length, 10)];
 75     animals.push(new t());
 76   }
 77   return animals;
 78 };
 79 
 80 
 81 /**
 82  * @private
 83  */
 84 nclosure.examples.animals.Example.prototype.makeAnimalsTalk_ = function() {
 85   /** @type {Array.<nclosure.examples.animals.IAnimal>} */
 86   goog.array.forEach(this.animals_, this.makeAnimalTalk_, this);
 87 
 88 };
 89 
 90 
 91 /**
 92  * @private
 93  * @param {nclosure.examples.animals.IAnimal} animal The animal to talkify.
 94  */
 95 nclosure.examples.animals.Example.prototype.makeAnimalTalk_ =
 96     function(animal) {
 97   animal.talk();
 98 };
 99 
100 
101 /*
102  * Start the demo
103  */
104 new nclosure.examples.animals.Example();
105