Wednesday, February 16, 2011

What happens when a Javascript constructor returns?


function T(){return T.a}
T.a = new T();
T.b = new T();
T.a == T.b

If you run this, the last statement will be true. Why?

The basic concept in a constructor's return statement is that if the return value is a primitive (null, number, boolean) then the result will be as expected. The "new" statement will return a reference to the newly created object. If the return is a reference, then that is what will be returned.

Thus when T.a = new T(); is called, T.a is undefined and the reference to the newly created object is returned. When T.b = new T(); is called T.a is defined and the reference to T.a is assigned to T.b. That is why the final statement T.a == T.b is true.

This is an interesting way to implement singletons in Javascript without having to write any special methods or use any other techniques.

You can also implement the Flyweight design pattern after majority of the program is written using a constructor.

Tuesday, February 8, 2011

Dojo Maven Integration

Project Structure:
/src
     /main
          /assembly/project-dojo.xml
          /javascript
                     /profiles/project.profile.js
                     /projectpackage1/**.js
                     /projectpackage2/**.js
pom.xml

project.profile.js
dependencies ={
    layers:  [
        {
        name: "../projectpackage1/projectpackage1",//output file
        dependencies: [
        "projectpackage1.hello"//list of all the files that need to be compressed into the given file.
        ]
        }
    ],
    prefixes: [
        [ "dijit", "../dijit" ],
        [ "dojox", "../dojox" ],
    ]
};


project-dojo.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: soc-dojo.xml,v 1.1 2010/04/06 19:27:59 rsingh Exp $ -->

<!-- Assembly descriptor for the CSP DOJO Custom Build ZIP -->
<assembly>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${basedir}/target/javascript/release/dojo</directory>
      <outputDirectory></outputDirectory>
      <includes>
        <include>projectpackage1/**</include>
        <include>projectpackage2/**</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>




pom.xml:
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelversion>4.0.0</modelversion>
 <groupid>project-group</groupid>
 <artifactid>project-name</artifactid>
 <packaging>pom</packaging>
 <name>SOC DOJO Custom Build Project</name>
 <description>

 <repositories>
  <repository>
   <id>topaz-repository</id>
   <name>Topaz Project Maven2 Repository</name>
   <url>http://maven.topazproject.org/maven2</url>
  </repository>
 </repositories>

 <build>

  <plugins>

   
   <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-dependency-plugin</artifactid>
    <executions>
     <execution>
      <id>unpack dojo</id>
      <phase>generate-sources</phase>
      <goals>
       <goal>unpack</goal>
      </goals>
      <configuration>
       <artifactitems>
        <artifactitem>
         <groupid>org.dojotoolkit</groupid>
         <artifactid>dojo</artifactid>
         <classifier>src</classifier>
         <version>1.5.0</version>
         <type>zip</type>
        </artifactitem>
       </artifactitems>
       <outputdirectory>${project.build.directory}/javascript</outputdirectory>
      </configuration>
     </execution>
    </executions>
   </plugin>

   
   <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-resources-plugin</artifactid>
    <version>2.3</version>
    <executions>
     <execution>
      <id>copy-js</id>
      <phase>generate-sources</phase>
      <goals>
       <goal>copy-resources</goal>
      </goals>
      <configuration>
       <outputdirectory>${project.build.directory}/javascript</outputdirectory>
       <resources>
        <resource>
         <directory>src/main/javascript</directory>
         <filtering>false</filtering>
        </resource>
       </resources>
      </configuration>
     </execution>
    </executions>
   </plugin>

   
   <plugin>
    <groupid>org.codehaus.mojo</groupid>
    <artifactid>exec-maven-plugin</artifactid>
    <version>1.1</version>
    <configuration>
     <executable>${java.home}/bin/java</executable>
     <workingdirectory>${project.build.directory}/javascript/util/buildscripts</workingdirectory>
     <arguments>
      <argument>-classpath</argument>
      <argument>../shrinksafe/js.jar</argument>
      <argument>-classpath</argument>
      <argument>../shrinksafe/shrinksafe.jar</argument>
      <argument>org.mozilla.javascript.tools.shell.Main</argument>
      
      <argument>build.js</argument>
      <argument>profileFile=${project.build.directory}/javascript/profiles/soc.profile.js</argument>
      <argument>action=release</argument>
      <argument>optimize=shrinksafe</argument>
      <argument>layerOptimize=shrinksafe</argument>
      <argument>internStrings=true</argument>
      <argument>cssOptimize=comments.keepLines</argument>
     </arguments>
    </configuration>
    <executions>
     <execution>
      <id>generate-release</id>
      <phase>generate-sources</phase>
      <goals>
       <goal>exec</goal>
      </goals>
     </execution>
    </executions>
   </plugin>

   
   <plugin>
    <artifactid>maven-assembly-plugin</artifactid>
    <configuration>
     <descriptors>
      <descriptor>src/main/assembly/soc-dojo.xml</descriptor>
     </descriptors>
    </configuration>
    <executions>
     <execution>
      <phase>package</phase>
      <goals>
       <goal>single</goal>
      </goals>
     </execution>
    </executions>
   </plugin>

   <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-install-plugin</artifactid>
    <executions>
     <execution>
      <id>install-custom-build</id>
      <phase>package</phase>
      <goals>
       <goal>install</goal>
      </goals>
     </execution>
    </executions>
   </plugin>

  </plugins>
 </build>

</description></project>