Anda di halaman 1dari 15

Not to be confused with Java (programming language). "Java Script" redirects here.

For the writing system, see Javanese script. For the use of JavaScript on Wikipedia, see Wikipedia:JavaScript.

JavaScript

Unofficial logo from JSConf EU 2011

Paradigm(s)

Multi-paradigm: scripting,object-oriented(prototypebased),imperative, functional[1]

Appeared in

1995

Designed by

Brendan Eich

Developer

Netscape Communications Corporation, Mozilla Foundation

Stable release

1.8.5[2] (March 22, 2011; 12 months ago)

Typing discipline

dynamic, weak, duck

Major implementations

KJS, Rhino,SpiderMonkey, V8,WebKit, Carakan, Chakra

Influenced by

C, Java, Perl, Python,Scheme, Self

Influenced

ActionScript,CoffeeScript, Dart, JScript .NET, ObjectiveJ, QML, TIScript

JavaScript at Wikibooks

JavaScript

Filename extension

.js

Internet media type

application/javascript text/javascript (obsolete)[3]

Uniform Type Identifier

com.netscape.javascript-source[4]

Type of format

Scripting language

This article is part of the JavaScript series.

JavaScript JavaScript syntax JavaScript topics


This box:

view

talk

edit

JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class [5] functions. It is a multi-paradigm language, supportingobject-oriented, imperative, [1][6] and functional programming styles. JavaScript was formalized in the ECMAScript language standard and is primarily used in the form of client-side JavaScript, implemented as part of a Web browser in order to provide enhanced user interfaces and dynamic websites. This enables programmatic access to computational objects within a host environment. JavaScript's use in applications outside Web pages for example in PDF documents, site-specific browsers, and desktop widgets is also significant. Newer and faster JavaScript VMs and frameworks built upon them (notably Node.js) have also increased the popularity of JavaScript for server-side web applications. JavaScript uses syntax influenced by that of C. JavaScript copies many names and naming conventions from Java, but the two languages are otherwise unrelated and have very different semantics. The key [7] design principles within JavaScript are taken from the Self and Scheme programming languages.
Contents
[hide]

1 History 2 Trademark 3 Features

o o o o o o

3.1 Imperative and structured 3.2 Dynamic 3.3 Functional 3.4 Prototype-based 3.5 Miscellaneous 3.6 Vendor-specific extensions

4 Syntax and semantics

o o

4.1 Simple examples 4.2 More advanced example

5 Use in web pages

o o o

5.1 Example script 5.2 Compatibility considerations 5.3 Accessibility

6 Security

6.1 Cross-site vulnerabilities

6.1.1 Misplaced trust in the client 6.1.2 Browser and plugin coding errors 6.1.3 Sandbox implementation errors

7 Uses outside web pages

o o o

7.1 Embedded scripting language 7.2 Scripting engine 7.3 Application platform

8 Development tools 9 Versions 10 Related languages and features

o o

10.1 Use as an intermediate language 10.2 JavaScript and Java

11 See also 12 References 13 Further reading 14 External links

[edit]History
Anyway I know only one programming language worse than C and that is Javascript. [...] I was convinced that we needed to build-in a programming language, but the developers, Tim first, were very much opposed. It had to remain completely declarative. Maybe, but the net result is that the programming-vacuum filled itself with the most horriblekluge in the history of computing: Javascript. Robert Cailliau[8]

JavaScript was originally developed by Brendan Eich of Netscape under the name Mocha, which was later renamed to LiveScript, and finally to JavaScript mainly because it was more influenced by the Java [9][10] programming language. LiveScript was the official name for the language when it first shipped in beta releases of Netscape Navigator 2.0 in September 1995, but it was renamed JavaScript in a joint [11] announcement with Sun Microsystems on December 4, 1995, when it was deployed in the Netscape [12] browser version 2.0B3. The change of name from LiveScript to JavaScript roughly coincided with Netscape adding support for Java technology in its Netscape Navigator web browser. The final choice of name caused confusion, giving the impression that the language was a spin-off of the Java programming language, and the choice has been characterized by many as a marketing ploy by Netscape to give JavaScript the cachet of what [13][14] was then the hot new web programming language. It has also been claimed that the language's name is the result of a co-marketing deal between Netscape and Sun, in exchange for Netscape bundling Sun's Java runtime with its then-dominant browser.

JavaScript very quickly gained widespread success as a client-side scripting language for web pages. As a consequence, Microsoft named its implementationJScript to avoid trademark issues. JScript added new date methods to fix the Y2K-problematic methods in JavaScript, which were based on [15] Java'sjava.util.Date class. JScript was included in Internet Explorer 3.0, released in August 1996. In November 1996, Netscape announced that it had submitted JavaScript to Ecma International for consideration as an industry standard, and subsequent work resulted in the standardized version [16] named ECMAScript. JavaScript has become one of the most popular programming languages on the web. Initially, however, many professional programmers denigrated the language because its target audience was web authors [17] and other such "amateurs", among other reasons. The advent of Ajax returned JavaScript to the spotlight and brought more professional programming attention. The result was a proliferation of comprehensive frameworks and libraries, improved JavaScript programming practices, and increased usage of JavaScript outside of web browsers, as seen by the proliferation of server-side JavaScript platforms. In January 2009, the CommonJS project was founded with the goal of specifying a common standard [18] library mainly for JavaScript development outside the browser. [edit]Trademark "JavaScript" is a trademark of Oracle Corporation. It is used under license for technology invented and [20] implemented by Netscape Communications and current entities such as the Mozilla Foundation. [edit]Features The following features are common to all conforming ECMAScript implementations, unless explicitly specified otherwise. [edit]Imperative
[19]

and structured

JavaScript supports much of the structured programming syntax from C (e.g., if statements, while loops, switch statements, etc.). One partial exception is scoping: Cstyle block-level scoping is not supported (instead, JavaScript has function-level scoping). JavaScript 1.7, however, supports block-level scoping with the let keyword. Like C, JavaScript makes a distinction between expressionsand statements. One syntactic difference from C is automatic semicolon insertion, in [21] which the semicolons that terminate statements can be omitted. [edit]Dynamic dynamic typing As in most scripting languages, types are associated with values, not with variables. For example, a variable x could be bound to a number, then later rebound to a string. JavaScript supports various ways to test the type of an object, including duck typing. object based JavaScript is almost entirely object-based. JavaScript objects are associative arrays, augmented with prototypes (see below). Object property names are string keys: obj.x = 10 andobj['x'] = 10 are equivalent, the dot notation being syntactic sugar.
[22]

Properties and their values can be added, changed, or deleted at run-time. Most properties of an object (and those on its prototype inheritance chain) can be enumerated using a for...in loop. JavaScript has a small number of built-in objects such as Function and Date. run-time evaluation JavaScript includes an eval function that can execute statements provided as strings at runtime. [edit]Functional first-class functions Functions are first-class; they are objects themselves. As such, they have properties and methods, such as length and call();
[23]

and they can be assigned to variables, passed as


[24]

arguments,return-ed by other functions, and manipulated like any other object. reference to a function allows it to be invoked using the () operator. nested functions
[25]

Any

"Inner" or "nested" functions are functions defined within another function. They are created each time the outer function is invoked. In addition to that, the scope of the outer function, including any constants, local variables and argument values, become part of the internal state of each inner function object, even after execution of the outer function concludes. closures JavaScript allows nested functions to be created, with the lexical scope in force at their definition, and has a () operator to invoke them now or later. This combination of code that can be executed outside the scope in which it is defined, with its own scope to use during that execution, is called a closure in computer science. prototypes JavaScript uses prototypes instead of classes for inheritance. It is possible to simulate many class-based features with prototypes in JavaScript. functions as object constructors Functions double as object constructors along with their typical role. Prefixing a function call with new creates a new object and calls that function with its local this keyword bound to that object for that invocation. The constructor's prototype property determines the object used for the new object's internal prototype. JavaScript's built-in constructors, such as Array, also have prototypes that can be modified. functions as methods Unlike many object-oriented languages, there is no distinction between a function definition and a method definition. Rather, the distinction occurs during function calling; a function can be called
[27] [26]

[edit]Prototype-based

as a method. When a function is called as a method of an object, the function's local this keyword is bound to that object for that invocation. [edit]Miscellaneous run-time environment JavaScript typically relies on a run-time environment (e.g. in a web browser) to provide objects and methods by which scripts can interact with "the outside world". In fact, it relies on the environment to provide the ability to include/import scripts (e.g. HTML <script> elements). (This is not a language feature per se, but it is common in most JavaScript implementations.) variadic functions An indefinite number of parameters can be passed to a function. The function can access them through formal parameters and also through the local arguments object. array and object literals Like many scripting languages, arrays and objects (associative arrays in other languages) can each be created with a succinct shortcut syntax. In fact, these literals form the basis of the JSONdata format. regular expressions JavaScript also supports regular expressions in a manner similar to Perl, which provide a concise and powerful syntax for text manipulation that is more sophisticated than the built-in string functions. [edit]Vendor-specific

extensions

JavaScript is officially managed by Mozilla Foundation, and new language features are added periodically. However, only some nonMozilla JavaScript engines support these new features: property getter and setter functions (also [28] supported by WebKit, Opera, ActionScript, [29] and Rhino) conditional catch clauses iterator protocol adopted from Python shallow generators-coroutines also adopted from Python array comprehensions and generator expressions also adopted from Python proper block scope via the let keyword array and object destructuring (limited form of pattern matching)

concise function expressions (function(args) expr) ECMAScript for XML (E4X), an extension that adds native XML support to ECMAScript

[edit]Syntax

and semantics

Main article: JavaScript syntax As of 2011, the latest version of the language is JavaScript 1.8.5. It is a superset of ECMAScript (ECMA-262) Edition 3. Extensions to the language, including partial ECMAScript for XML (E4X) (ECMA-357) support and experimental features considered for inclusion into future ECMAScript editions, are [30] documented here. [edit]Simple

examples

A simple alert box: alert("alert alert") A simple recursive function: function factorial(n) { if (n === 0) { return 1; } return n * factorial(n - 1); } Anonymous function (or lambda) syntax and closure example: function displayClosure() { var count = 0; return function () { return ++count; }; } var inc = displayClosure(); inc(); // returns 1 inc(); // returns 2 inc(); // returns 3 Variadic function demonstration (arguments is a special variable). function sum() { var i,

x = 0; for (i = 0; i < arguments.length; ++i) { x += arguments[i]; } return x; } sum(1, 2, 3); // returns 6 [edit]More advanced example This sample code showcases various JavaScript features. /* Finds the lowest common multiple of two numbers */ function LCMCalculator(x, y) { // constructor function var checkInt = function (x) { // inner function if (x % 1 !== 0) { throw new TypeError(x + " is not an integer"); // throw an exception } return x; }; this.a = checkInt(x); // ^ semicolons are optional this.b = checkInt(y); } // The prototype of object instances created by a constructor is // that constructor's "prototype" property. LCMCalculator.prototype = { // object literal constructor: LCMCalculator, // when reassigning a prototype, set the constructor property appropriately gcd: function () { // method that calculates the greatest common divisor // Euclidean algorithm: var a = Math.abs(this.a), b = Math.abs(this.b), t;

if (a < b) { // swap variables t = b; b = a; a = t; } while (b !== 0) { t = b; b = a % b; a = t; } // Only need to calculate GCD once, so "redefine" this method. // (Actually not redefinition - it's defined on the instance itself, // so that this.gcd refers to this "redefinition" instead of LCMCalculator.prototype.gcd.) // Also, 'gcd' === "gcd", this['gcd'] === this.gcd this['gcd'] = function () { return a; }; return a; }, // Object property names can be specified by strings delimited by double (") or single (') quotes. "lcm" : function () { // Variable names don't collide with object properties, e.g. |lcm| is not |this.lcm|. // not using |this.a * this.b| to avoid FP precision issues var lcm = this.a / this.gcd() * this.b; // Only need to calculate lcm once, so "redefine" this method. this.lcm = function () { return lcm; }; return lcm; }, toString: function () {

return "LCMCalculator: a = " + this.a + ", b = " + this.b; } }; //define generic output function; this implementation only works for web browsers function output(x) { document.write(x + "<br>"); } // Note: Array's map() and forEach() are defined in JavaScript 1.6. // They are used here to demonstrate JavaScript's inherent functional nature. [[25, 55], [21, 56], [22, 58], [28, 56]].map(function (pair) { // array literal + mapping function return new LCMCalculator(pair[0], pair[1]); }).sort(function (a, b) { // sort with this comparative function return a.lcm() - b.lcm(); }).forEach(function (obj) { output(obj + ", gcd = " + obj.gcd() + ", lcm = " + obj.lcm()); }); The following output should be displayed in the browser window. LCMCalculator: a 28, lcm = 56<br> LCMCalculator: a 7, lcm = 168<br> LCMCalculator: a 5, lcm = 275<br> LCMCalculator: a 2, lcm = 638<br> [edit]Use = 28, b = 56, gcd = = 21, b = 56, gcd = = 25, b = 55, gcd = = 22, b = 58, gcd =

in web pages

See also: JavaScript engine and Ajax (programming)

The most common use of JavaScript is to write functions that are embedded in or included from HTML pages and that interact with the Document Object Model (DOM) of the page. Some simple examples of this usage are: Opening or popping up a new window with programmatic control over the size, position, and attributes of the new window (e.g. whether the menus, toolbars, etc., are visible). Validating input values of a web form to make sure that they are acceptable before being submitted to the server. Changing images as the mouse cursor moves over them: This effect is often used to draw the user's attention to important links displayed as graphical elements. Transmitting information about the user's reading habits and browsing activities to various websites. Web pages frequently do this for web analytics, ad tracking, personalization or other [citation needed] purposes.

Because JavaScript code can run locally in a user's browser (rather than on a remote server), the browser can respond to user actions quickly, making an application more responsive. Furthermore, JavaScript code can detect user actions which HTML alone cannot, such as individual keystrokes. Applications such as Gmail take advantage of this: much of the user-interface logic is written in JavaScript, and JavaScript dispatches requests for information (such as the content of an e-mail message) to the server. The wider trend of Ajax programming similarly exploits this strength. A JavaScript engine (also known as JavaScript interpreter or JavaScript implementation) is an interpreter that interprets JavaScript source code and executes the script accordingly. The first JavaScript engine was created by Brendan Eich at Netscape Communications Corporation,

for the Netscape Navigator web browser. The engine, code-named SpiderMonkey, is implemented in C. It has since been updated (in JavaScript 1.5) to conform to ECMA-262 Edition 3. The Rhino engine, created primarily by Norris Boyd (formerly of Netscape; now at Google) is a JavaScript implementation in Java. Rhino, like SpiderMonkey, is ECMA-262 Edition 3 compliant. A web browser is by far the most common host environment for JavaScript. Web browsers typically use the public API to create "host objects" responsible for reflecting the Document Object Model(DOM) into JavaScript. The web server is another common application of the engine. A JavaScript webserver would expose host objects representing an HTTP request and response objects, which a JavaScript program could then manipulate to dynamically generate web pages. Because JavaScript is the only language that the most popular browsers share support for, it has become a target language for many frameworks in other languages, even though JavaScript was [10] never intended to be such a language. Despite the performance limitations inherent to its dynamic nature, the increasing speed of JavaScript engines has made the language a surprisingly feasible compilation target. [edit]Example

script

Below is a minimal example of a standardsconforming web page containing JavaScript (using HTML 4.01 syntax) and the DOM: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.d td"> <html> <head><title>simple page</title></head> <body> <h1 id="header">This is JavaScript</h1>

<script type="text/javascript"> document.write('Hello World!'); var h1 = document.getElementById("header"); // holds a reference to the <h1> tag h1 = document.getElementByTagName("h1")[0 ]; // accessing the same <h1> element </script> <noscript>

Your browser either does not support JavaScript, or has JavaScript turned off. </noscript> </body> </html> [edit]Compatibility

considerations

Main article: Web interoperability Because JavaScript runs in widely varying environments, an important part of testing and debugging is to test and verify that the JavaScript works across multiple browsers. The DOM interfaces for manipulating web pages are not part of the ECMAScript standard, or of JavaScript itself. Officially, the DOM interfaces are defined by a separate standardization effort by theW3C; in practice, browser implementations differ from the standards and from each other, and not all browsers execute JavaScript. To deal with these differences, JavaScript authors can attempt to write standards-compliant code which will also be executed correctly by most browsers; failing that, they can write code that checks for the presence of certain browser features and behaves differently if they are not [31] available. In some cases, two browsers may both implement a feature but with different behavior, and authors may find it practical to

detect what browser is running and change their [32][33] script's behavior to match. Programmers may also use libraries or toolkits which take browser differences into account. Furthermore, scripts may not work for some users. For example, a user may: use an old or rare browser with incomplete or unusual DOM support, use a PDA or mobile phone browser which cannot execute JavaScript, have JavaScript execution disabled as a security precaution, use a speech browser due to, for example, a visual disability.

To support these users, web authors can try to create pages which degrade gracefully on user agents (browsers) which do not support the page's JavaScript. In particular, the page should remain usable albeit without the extra features that the JavaScript would have added. An alternative approach that many find preferable is to first author content using basic technologies that work in all browsers, then enhance the content for us

Anda mungkin juga menyukai