Understanding the Definition and Philosophy of Ajax

an article added by: Sonja Lande at 05312007


In: Root » Computers and technology » AJAX » Understanding the Definition and Philosophy of Ajax

French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic

The focus of this article is to provide solutions to some common, general problems and questions that are bound to arise before or during development of Asynchronous JavaScript and XML (Ajax) and Representational State Transfer (REST) applications. These common questions are not always technical in nature, often leaning more toward theory or philosophy of development. The problem with these kinds of questions is that once you begin to think about them, you keep going in a circle and end up where you started. The trick to figuring out the answers is not to keep going in a circle, but to stick with the assumptions and make a decision.

Understanding the Definition and Philosophy of Ajax

Jesse James Garrett at Adaptive Path coined the original definition1 of Ajax. Quoting the original definition, Ajax incorporates the following features:

• Standards-based presentation using Extensible HyperText Markup Language (XHTML) and Cascading Style Sheets (CSS)

• Dynamic display and interaction using the Document Object Model (DOM)

• Data interchange and manipulation using Extensible Markup Language (XML) and Extensible Stylesheet Language Transformations (XSLT)

• Asynchronous data retrieval using XMLHttpRequest

• JavaScript to bind everything together In a nutshell, Ajax is a style of Web development that requires aWeb browser. The user interface of the Web browser is dynamically modified using a programming language that retrieves data only when necessary, rather than the traditional approach of refreshing the whole page every time a request is made. I want to highlight the terms dynamically and only when necessary, because those terms are the essence of Ajax. Ajax and JavaScript are examples of duck typing2 and latent-type programming.

Duck-typed programming is about writing code where the definition of the classes is not known ahead of time, but you know the object has some specific behavior. Reuse is made possible by cloning and assembling the objects dynamically at runtime. Classical object-oriented programming is about defining the behavior of the type before execution. The following source code is for an example Dynamic HTML (DHTML) and JavaScript application that illustrates the essence of duck-typed programming. Source: /website/ROOT/gettingstarted/PrototypeBased.html

   <html>
   <head>
   <title>Prototype-based  Programming</title>
   <script language="JavaScript"  type="text/javascript">
   function Variation1() {
   document.getElementById(  "output").innerHTML = "Ran Variation 1";
   }
   function Variation2() {
   document.getElementById(  "output").innerHTML = "Ran Variation 2";
   }
   var obj = new Object();
   function RunVariation() {
   obj.runIt();
   }
   </script>
   </head>
   <body>
   <input type="button"  value="Variation 1"
   onclick="obj.runIt  = Variation1; RunVariation()" />
   <input type="button"  value="Variation 2"
   onclick="obj.runIt  = Variation2; RunVariation()" /><br/>
   <div id="output">Nothing  yet</div>
   </body>
   </html>

In the example, the bold code segments illustrate the duck-typed programming constructs. When the Web browser loads the code, it will be parsed from top to bottom. When the code has been parsed, the following types and object instances will be active:

• Definition of the functions Variation1, Variation2, and RunVariation

• Instantiation and definition of the variable obj, which references a plain vanilla Object instance

• Definition of two buttons (Variation 1 and Variation 2) that execute some JavaScript when clicked

• Definition of an HTML div element that has the identifier output Calling the function RunVariation generates an exception, because obj is a plain vanilla object instance and has no method implementation for runIt. A classical programming language such as Java, C#, or C++ is not able to compile the JavaScript code, because the function RunVariation executes a method on a type that is not defined to possess the method. When an object method is called, as in the source code, it is called latent typing. Latent typing is the identification of the type associated with a variable during the runtime of the application. In the case of the source code example, that means the exact behavior of obj is not known until the application is executed. Hence, RunVariation may or may not work. In the example code, when the input buttons are pressed, the property obj.runIt is assigned to either Variation1 or Variation2. After the property has been assigned, the input buttons call the function RunVariation, which in turn calls the property obj.runIt. As the property has an assigned value, the function Variation1 or Variation2 is called. The assigning of the property to a function is the essence of duck-typed programming. This raises the question, if a programming language employs latent programming techniques, does that imply duck-typed programming? And if it does not, what are the differences? If a programming language supports latent typing, it does not imply duck-typed programming. But if a programming language supports duck-typed programming, it must support latent typing. C++ is an excellent example of a language that supports latent types but does not support duck typing. The following source code illustrates latent typing:

   class LatentTypeCaller< T> {
   public void CallIt( T t) {
   t.LatentDefinedMethod();
   }
   }

In the example code, T is a type that belongs to a C++ template. In the implementation of CallIt, the method t.LatentDefinedMethod is called. From the source code, the type of T is not apparent, but whatever it is, the method LatentDefinedMethod must be supported. C++ does not support duck typing, because T cannot have the method LatentDefinedMethod assigned dynamically. With the inclusion of template type functionality in .NET 2.0 and in Java 5.0 called generics, you might be tempted to believe that generics support latent typing.

The code as written in C++ is not possible in either .NET or Java, as the compilers would complain about unconstrained types. To get rid of the compiler errors in C# or Java, you must constrain T to a type that supports the method LatentDefinedMethod. A common argument against duck-typed programming and latent typing is that you don’t know what the code will do until you execute it. In contrast, the C++, .NET, and Java programming environments, which require explicit definition or static typing of types, make for stable and robust code. At least, that is the argument promoted by individuals who support static typing. Static typing ensures that a program compiles and fits together, but it does not guarantee that the program does what is expected. Consider the following code, which illustrates how static typing can be fooled:

   class Math {
   public long add( long value1, long value2) {
   return value1 - value2;
   }
   }

legal disclaimer

Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.

related articles

1. The Easiest Way to Get Started with Ajax and REST
The Easiest Way to Get Started with Ajax and REST Problem You want to know the best way to get started with writing Ajax and REST. Solution When developing an Ajax and REST application, you must decide on the tools and frameworks you’ll use. The choice is simple: Use whatever you’re using today, and write some Ajax applications. You don’t need to change the tools you’re using today. Whether you’re using ASP.NET, JavaServer Pages (JSP), PHP, Ruby, or Python, you...

2. Testing a Dynamic Contract with Ajax
Coding the Contract Using Test-Driven Development Techniques Coding the contract using agile and test-driven development techniques requires writing a number of tests and implementing aMock URL layer. Problem You want to code the contract using these development techniques. Solution To demonstrate, let’s define a use case, implement the use case as a contract, write a test case(s) to implement the contract, implement the contract in the Mock URL, and finally...

3. Testing the Client Side Logic
Problem You want to effectively test your application’s client-side logic. Theory Testing GUI code tends not to be a productive task because of the complications that arise. The main complication is how to test the correctness of a user interface. Imagine a situation where clicking a button causes a table to be filled with data. Now imagine that when a check box is checked and the button is clicked again, a different table is filled with content. The fact that clicking the same button results in two ...

4. Understanding JavaScript and Types
Understanding JavaScript and Types Problem You want to work around the fact that JavaScript does not have types declared for its variables. Theory JavaScript code does not have any variables with a declared type. The lack of typed variables is apparent when you declare functions. That said, not having typed variable declarations does not mean JavaScript has no types or no type safety. Let’s start out with the simple declaration of a function, as illustrated by the following ex...

5. Coding Using Conventions and Not Configurations
Coding Using Conventions and Not Configurations Problem You want to make your JavaScript constructs more efficient by applying the Rails “convention over configuration” principle to them. Theory You may already be familiar with the programming platform Ruby on Rails, which is used to build Web applications. The focus of this recipe is not Ruby on Rails, but one aspect of Ruby on Rails namely, convention over configuration (see http://en.wikipedia.org/wiki/ Ruby_on_Rails for m...

6. Advantage of parameterless functions in JavaScript
Using Parameterless Functions Problem You want to take advantage of parameterless functions in JavaScript. Theory JavaScript functions for the most part have parameters. You may think that the previous sentence states the obvious after all, without parameters, what data could be passed to a function? JavaScript has the ability to declare functions that have no parameters, even though the caller of the function has passed parameters to the function. For example, let’s look at...

7. JavaScripot Functions
Treating Functions Like Objects Problem You want to take advantage of the fact that functions are objects (remember, everything is an object in JavaScript). Theory Many people think that a function is some keyword used in JavaScript. A function is also an object that can be manipulated. Knowing that a function is an object makes it very interesting from the perspective of writing JavaScript code, because the code can treat the function like another other object. This mean...

8. Implementing an Error and Exception Handling Strategy
Implementing an Error and Exception Handling Strategy Problem You want to implement a clean error and exception handling strategy in your applications, to make them run more smoothly. Theory Of course, you might argue that one error is a dialog box and the other is generated in the JavaScript console. The fact that one browser uses a dialog box to show an error and the other does not is a browser issue, not an error issue. A concise way of classifying the two errors is to ...

9. Understanding the Behavior of Variables When Implementing Recursion
Understanding the Behavior of Variables When Implementing Recursion Problem You want to implement recursion in JavaScript, and you also want to understand how variables will behave under those circumstances. Theory In JavaScript, you do not need to declare the variable type, or even declare the variable. For example, the following code works perfectly: if( counter == 1) { buffer = "counter is 1"; } document.getEle...

10. Using Functions to Initialize and Make Decisions JavaScript
Using Functions to Initialize and Make Decisions Problem You want to use functions to initialize and make decisions. Theory Usually when you write a piece of code where a change of logic needs to take place based on a context, you use a decision structure. For example, say you are implementing a light switch using a program. You turn on the light if the light is off, and you turn off the light if the light is on. The behavior of the program is determined by the conditions. One example behavior t...