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.
Features
Prototype 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 functions
The $() function
The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the DOM of an HTML page, the usual function identifying an element is: <source lang="javascript">
document.getElementById("id_of_element").style.color = "#ffffff";
</source>
The $() function reduces the code to: <source lang="javascript">
$("id_of_element").setStyle({color: '#ffffff'});
</source>
The $() function can also receive an element as parameter and will return, as in the previous example, a prototype extended object. <source lang="javascript">
var element_js = document.getElementById("id_of_element"); // usual object reference returned
var element_pt = $(element_js); // prototype extended object reference
</source>
- Note: Like the underscore (
_), the$character is a legal "word character" in JavaScript identifiers, and has no other significance in the language. It was added to the language at the same time as support for regular expressions, so that the Perl-like matching variables could be emulated, such as$`and$'.
The $F() function
Building on the $() function: the $F() function returns the value of the requested form element. For a 'text' input, the function will return the data contained in the element. For a 'select' input element, the function will return the currently selected value.
<source lang="javascript">
$F("id_of_input_element")
</source>
The $$() function
The 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> tags with the class "pulsate", you would use the following:
<source lang="javascript">
$$("a.pulsate")
</source>
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:
<source lang="javascript">
$$("a.pulsate").each(Effect.Pulsate);
</source>
The Ajax object
In an effort to reduce the amount of code needed to run a cross-browser XMLHttpRequest function, Prototype provides the Ajax object to abstract the different browsers. It has two main methods: Ajax.Request() and Ajax.Updater().
There are two forms of the Ajax object. Ajax.Request returns the raw XML output from an AJAX call, while the Ajax.Updater will inject the return inside a specified DOM object.
The Ajax.Request below finds the values of two HTML value inputs, requests a page from the server using the values as POST values, then runs a custom function called showResponse() when complete:
<source lang="javascript"> var url = "http://www.example.com/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
}); </source>
Object-oriented programming
Prototype also adds support for more traditional object-oriented programming. The Class.create() method is used to create a new class. A class is then assigned a prototype which acts as a blueprint for instances of the class.
<source lang="javascript">
var FirstClass = Class.create( {
// The initialize method serves as a constructor
initialize: function () {
this.data = "Hello World";
}
}); </source>
Extending another class: <source lang="javascript"> 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 ...
});
</source>
The framework function Object.extend(dest, src) takes two objects as parameters and copies the properties of the second object to the first one simulating inheritance. The combined object is also returned as a result from the function. As in the example above, the first parameter usually creates the base object, while the second is an anonymous object used solely for defining additional properties. The entire sub-class declaration happens within the parentheses of the function call.
References
Bibliography
- {{{Expansion depth limit exceeded}}}{{Expansion depth limit exceeded|, {{{Expansion depth limit exceeded}}}
}} ({{Expansion depth limit exceeded|{{{Expansion depth limit exceeded}}}| }}). {{Expansion depth limit exceeded|
|{{Expansion depth limit exceeded|{{Expansion depth limit exceeded||p. }}{{{Expansion depth limit exceeded}}}
|{{Expansion depth limit exceeded|{{Expansion depth limit exceeded||pp. }}{{{Expansion depth limit exceeded}}}
|
}}
}}
}}. ISBN 978-0470384596. {{{Expansion depth limit exceeded}}}.
See also
- Ajax (programming)
- Comparison of JavaScript frameworks
- Script.aculo.us
- Archetype JavaScript Framework
- Mootools JavaScript Framework
External links
- Prototype Javascript Framework home page
- Official Prototype API documentation
- Archetype Javascript Framework runs on top of Prototype
- Prototype extensions library created by community members
| ||||||||||||||||||||||||||||||||||||||
ar:بروتوتيب (إطار عمل) de:Prototype (Framework) es:Prototype ko:Prototype 자바스크립트 프레임워크 it:Prototype JavaScript Framework ja:Prototype JavaScript Framework pl:Prototype JavaScript Framework ro:Prototype ru:Prototype uk:Prototype vi:Prototype (Framework JavaScript) zh:Prototype JavaScript Framework
If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...