Showing posts with label dojo. Show all posts
Showing posts with label dojo. Show all posts

Tuesday, 3 June 2014

Introduction to Object Oriented Javascript

Introduction
Many programming languages uses class based object oriented based paradigm, However, JavaScript uses prototype based paradigm. Let's get more deeper into that....

JavaScript was simply used as a scripting language which was mainly used by web-designers to do simple dynamic tricks on web pages. With time the JavaScript has evolve as one of the important full featured programming language to built rich Internet application rather than just doing simple tricks on the website.

                                    Object Oriented JavaScript
JavaScript uses class-less based object oriented paradigm which is also known as prototype based object oriented paradigm.
In prototype based object oriented paradigm, rather than defining set of class templates from which the objects are created, the objects are declared as they are needed.

                                                Advantage 
One of the major advantage of prototype based OOP paradigm is that the objects can be modified at runtime. This means that the definition structure of the object is not strict as the classes are not defined.

                                                  Example
Let us see an example of object oriented paradigm, A function serves as an object prototype. To create an instance of this object new keyword is used.



1
2
3
function Car() { }
var myCar = new Car();
console.log(myCar);

Here the function Car() serves as an object and myCar is an instance of Car().
The output of the above code is object{}

To see a complete example of the same Following is the code :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<html>
<head>
    <title>Learning Dojo</title>
    <script src="dojoroot/dojo/dojo.js">
</script>

</head>
<body>
<h1>Learning Dojo</h1>
<div>Object oriented javascript</div>
<ul id="list">
    <li>It is class-less based object oriented paradigm</li>
    <li>Allow to modify object at runtime</li>
    <li>class template need not to be defined</li>
</ul>
<script>
    function Car() { } //Here Car function serves as an object
    var myCar = new Car(); // myCar is an instance of Car object
    alert(myCar); // This will show the output as object in the alert-box
    
</script>
</body>
</html>


After running the above code in the browser we get the following output:


Saturday, 12 April 2014

Getting Started with dojo : Part 1

DOJO

Dojo is a lightweight javascript library which have the following features:
  • Lightweight 
  • Provide object oriented features
  • Help to build AJAX rich application
  • Large scale web application can be built
  • Provide widget library 
  • Saves development time
  • Prevent cross browser inconsistency
  • And many more
Follow the instruction to get started with dojo :
  1. Download stable version of dojo from dojotoolkit.org
  2. In your www directory of wamp server make a folder named learndojo, in the folder learndojo make another directory dojoroot.
  3. Unzip the downloaded dojo library in this dojoroot directory, directory structure should be like www/learndojo/dojoroot/dojo/dojo.js . The file dojo.js is present in the dojo folder of your unzipped directories.
  4. In the learndojo directory make a file and named it index.html which have the following code.
  1. <html>
  2. <head>
  3. <title>Learning Dojo</title>
  4. <!--Below line includes the dojo.js files from the directory dojoroot/dojo/ -->
  5. <script src="dojoroot/dojo/dojo.js">
  6. </script>
  7. </head>
  8. <body>
  9. <h1>Learning Dojo</h1>
  10. <div id="message">Following are some features of dojo</div>
  11. <ul id="list">
  12. <li>Rich Internet application, saving development time</li>
  13. <li class="highlight">Dojo have widget library and object oriented features which helps to build a AJAX powered web application</li>
  14. <li>Extremely light-weight</li>
  15. </ul>
  16. </script>
  17. <script>
  18. /*Here dojo.onLoad is a function which will create a div on this page gets loaded and will add the text Hello learner.... to the body of this page. */
  19. dojo.addOnLoad(function() {
  20. dojo.create(
  21. "div",
  22. {
  23. "innerHTML": "Hello, Learner. I hope you are enjoying dojo!"
  24. },
  25. dojo.body()
  26. );
  27. });
  28. </script>
  29. </body>
  30. </html>

In the above code, the we have included dojo.js in the <head> section and we are using dojo to create a div dynamically when the web page gets loaded.
The dojo.onLoad function creates a div and add the text to the body "Hello learner, I hope you are enjoying  dojo".
The same working code you can fork from git also.