In: Categories » Computers and technology » AJAX » Understanding the Definition and Philosophy of Ajax
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 notice
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.
Useful tools and features
related articles
Given the recent interest in Ajax, you’d be forgiven for thinking it was a new technology. In fact, the XMLHttpRequest object has been around for years. In technical terms, asynchronous JavaScript interaction with the server is nothing new. All of the other elements of the Ajax model have also been around for quite some time: CSS, (X)HTML, and DOM Scripting. Yet in 2005, interest in this methodology soared. Could it really be that simply giving this approach a snappy name like Ajax was responsible for the sudde...
2. Understanding the Definition and Philosophy of Web Services and SOA
Understanding the Definition and Philosophy of Web Services and SOA Wikipedia offers the following definition of Web services:4 The W3C defines aWeb service as a software system designed to support interoperable machine-to-machine interaction over a network. This definition encompasses many different systems, but in common usage the term refers to those services that use SOAPformatted XML envelopes and have their interfaces described by WSDL. For ex...
3. Understanding the Definition and Philosophy of REST
Understanding the Definition and Philosophy of REST REST is a controversial topic among Web service enthusiasts, because it’s considered to stand for the opposite of what Web services and SOA are trying to achieve. The problem with this thinking is that REST is not in contradiction with the abstract definition of SOA and Web services. REST is in contradiction with technologies such as SOAP, WSDL, and WS-* specifications. The following offers a quick definition of REST:...
4. 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...
5. 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...
6. 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 ...
7. 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...
8. 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...
9. 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...
