Prototype JavaScript Framework
Encyclopedia
|
| Tutorials | Encyclopedia | Dictionary | Directory |
|
![]()
Prototype JavaScript Framework
The Prototype JavaScript Framework is a JavaScript framework created by Sam Stephenson which provides an Ajax framework and other utilities. It is implemented as a single file of JavaScript code, usually named prototype.js. Prototype is distributed standalone, but also as part of larger projects, such as Ruby on Rails, script.aculo.us and Rico.
FeaturesPrototype provides various functions for developing JavaScript applications. The features range from programming shortcuts to major functions for dealing with XMLHttpRequest. Prototype also provides library functions to support classes and class-based objects[1], something the JavaScript language does not have[2][3]. In JavaScript, object creation is prototype-based instead: an object creating function can have a prototype property, and any object assigned to that property will be used as a prototype for the objects created with that function. The Prototype framework is not to be confused with this language feature. Sample utility functionsThe $() functionThe dollar function, $(), is basically Prototype's shorthand for getElementById. To refer to an element in the DOM of an HTML page, the usual function identifying an element is:
document.getElementById("id_of_element")
The $() function reduces the code to:
$("id_of_element")
This function can be used as the getElementById() function. For example, you can set the CSS text color with this code:
$("id_of_element").style.color = "#ffffff";
Or, the "Prototype way":
$("id_of_element").setStyle({color: '#ffffff'});
The $F() functionBuilding on the
$F("id_of_input_element")
The $$() functionThe dollar dollar function is Prototype's CSS Selector Engine. It returns all matching elements, following the same rules as a selector in a CSS stylesheet. For example, if you want to get all
$$("a.pulsate")
This returns a collection of elements. If you are using the Script.aculo.us extension of the core Prototype library, you can apply the "pulsate" (blink) effect as follows:
$$("a.pulsate").each(Effect.Pulsate);
The Ajax objectIn an effort to reduce the amount of code needed to run a cross-browser
var url = "http://yourserver/path/server_script";
var myAjax = new Ajax.Request(url, {
parameters: {
value1: $F("name_of_id_1"),
value2: $F("name_of_id_2")
},
onSuccess: showResponse,
onFailure: showError
});
Object-oriented programmingPrototype also adds support for more traditional object-oriented programming. The
var FirstClass = Class.create( {
// The initialize method serves as a constructor
initialize: function () {
this.data = "Hello World";
}
});
Extending another class:
Ajax.Request= Class.create( Ajax.Base, {
//Overwrite the initialize method
initialize: function(url, options) {
this.transport = Ajax.getTransport();
this.setOptions(options);
this.request(url);
},
// ...more methods add ...
});
The framework function ReferencesSee alsoExternal links
de:Prototype (Framework) es:Prototype it:Prototype JavaScript Framework ru:Prototype vi:Prototype (Framework JavaScript)
Source: Wikipedia | The above article is available under the GNU FDL. | Edit this article
|
|
top
©2008-2009 TutorGig.com. All Rights Reserved. Privacy Statement