The Prototype JavaScript Framework is a
JavaScript
JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
framework
A framework is a generic term commonly referring to an essential supporting structure which other things are built on top of.
Framework may refer to:
Computing
* Application framework, used to implement the structure of an application for an op ...
created by Sam Stephenson in February 2005 as part of
Ajax
Ajax may refer to:
Greek mythology and tragedy
* Ajax the Great, a Greek mythological hero, son of King Telamon and Periboea
* Ajax the Lesser, a Greek mythological hero, son of Oileus, the king of Locris
* ''Ajax'' (play), by the ancient Greek ...
support in
Ruby on Rails
Ruby on Rails (simplified as Rails) is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and we ...
. 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. As of March 2021, according to w3techs, Prototype is used by 0.6% of all websites.
Features
Prototype provides various functions for developing JavaScript applications. The features range from programming shortcuts to major functions for dealing with
XMLHttpRequest
XMLHttpRequest (XHR) is an API in the form of an object whose methods transfer data between a web browser and a web server. The object is provided by the browser's JavaScript environment. Particularly, retrieval of data from XHR for the purpos ...
.
Prototype also provides library functions to support
classes and class-based objects. In JavaScript, object creation is
prototype
A prototype is an early sample, model, or release of a product built to test a concept or process. It is a term used in a variety of contexts, including semantics, design, electronics, and Software prototyping, software programming. A prototyp ...
-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
Document Object Model
The Document Object Model (DOM) is a cross-platform and language-independent interface that treats an XML or HTML document as a tree structure wherein each node is an object representing a part of the document. The DOM represents a document wi ...
(DOM) of an
HTML
The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
page, the usual function identifying an element is:
document.getElementById("id_of_element").style.color = "#ffffff";
The $() function reduces the code to:
$("id_of_element").setStyle();
The $() function can also receive an element as parameter and will return, as in the previous example, a prototype extended object.
var domElement = document.getElementById("id_of_element"); // Usual object reference returned
var prototypeEnhancedDomElement = $(domElement); // Prototype extended object reference
: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 expression
A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or ...
s, so that the
Perl
Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offici ...
-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.
$F("id_of_input_element")
The $$() function
The dollar dollar function is Prototype's ''
CSS
Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language such as HTML or XML (including XML dialects such as SVG, MathML or XHTML). CSS is a cornerstone techno ...
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>
elements with the class "pulsate", you would use the following:
$$("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 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 current values of two HTML form input elements, issues an HTTP POST request to the server with those element name/value pairs, and runs a custom function (called
showResponse
below) when the HTTP response is received from the server:
new Ajax.Request("http://localhost/server_script", );
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.
var FirstClass = Class.create( );
Extending another class:
Ajax.Request = Class.create( Ajax.Base, );
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.
Problems
Unlike other JavaScript libraries like
jQuery
jQuery is a JavaScript library designed to simplify HTML DOM tree traversal and manipulation, as well as event handling, CSS animation, and Ajax. It is free, open-source software using the permissive MIT License. As of Aug 2022, jQuery is used ...
, Prototype extends the DOM. There are plans to change this in the next major version of the library.
In April 2010, blogger Juriy 'kangax' Zaytsev (of Prototype Core) described at length the problems that can follow from
monkey patch Monkey patching is a technique used to dynamically update the behavior of a piece of code at run-time. A monkey patch (also spelled monkey-patch, MonkeyPatch) is a way to extend or modify the runtime code of dynamic languages (e.g. Smalltalk, JavaSc ...
ing new methods and properties into the objects defined by the W3C DOM.
These ideas echo thoughts published in March 2010 by Yahoo! developer Nicholas C. Zakas They have been summarized as follows
* Cross browser issues: host objects are not subject to rules, non-compliant IE DOM behavior, etc.
* Chance of name collisions
* Performance overhead
By 2008, specific issues with using DOM-extension methods in older versions of Prototype, combined with newer versions of current browsers, were already being documented.
Rather than adding new methods and properties to pre-existing 'host' DOM objects such as
Element
, like
element.hide()
, the solution to these issues is to provide wrapper objects around these host objects and implement the new methods on these.
jQuery
is such a wrapper object in the library of that name.
It is now widely expected that the majority of these ideas and issues will be addressed in the release of Prototype 2.0, but Prototype developers will have to learn to work with an altered syntax, and much existing Prototype code will become outdated.
See also
*
Ajax (programming)
Ajax (also AJAX ; short for " Asynchronous JavaScript and XML") is a set of web development techniques that uses various web technologies on the client-side to create asynchronous web applications. With Ajax, web applications can send and retri ...
*
Comparison of JavaScript frameworks
This is a comparison of web frameworks for front-end web development that are heavily reliant on JavaScript
JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World ...
*
Mootools
MooTools (My Object-Oriented Tools) is a lightweight, object-oriented JavaScript framework. It is released under the free, open-source MIT License.
Overview
MooTools provides the user with a number of options beyond native JavaScript. These inc ...
JavaScript Framework
*
jQuery
jQuery is a JavaScript library designed to simplify HTML DOM tree traversal and manipulation, as well as event handling, CSS animation, and Ajax. It is free, open-source software using the permissive MIT License. As of Aug 2022, jQuery is used ...
*
JavaScript framework
A web framework (WF) or web application framework (WAF) is a software framework that is designed to support the development of web applications including web services, web resources, and web APIs. Web frameworks provide a standard way to build an ...
*
JavaScript library A JavaScript library is a library of pre-written JavaScript code that allows for easier development of JavaScript-based applications, especially for AJAX and other web-centric technologies.
Libraries
With the expanded demands for JavaScript, an ea ...
References
Bibliography
*
External links
*
{{DEFAULTSORT:Prototype Javascript Framework
JavaScript libraries
Ajax (programming)
Software using the MIT license