JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functionalprogramming styles.
JavaScript is an implementation of 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 . JavaScript copies many names and naming conventions from Java, but the two languages are otherwise unrelated and have very different semantics. The key design principles within JavaScript are taken from the Self and Scheme programming languages.
Features
The following features are common to all conforming ECMAScript implementations, unless explicitly specified otherwise.Imperative and structured
JavaScript supports structured programming syntax in C (e.g.,if statements, while loops, switch statements, etc.). One partial exception is scoping: C-style 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 expressions and statements. One syntactic difference from C is automatic semicolon insertion, in which the semicolons that terminate statements can be omitted.Dynamic
- dynamic typing
- As in most scripting languages, types are associated with values, not with variables. For example, a variable
xcould 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.[23] - 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 = 10andobj['x'] = 10are equivalent, the dot notation being syntactic sugar. 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 afor...inloop. JavaScript has a small number of built-in objects such asFunctionandDate. - run-time evaluation
- JavaScript includes an
evalfunction that can execute statements provided as strings at run-time.
Functional
- first-class functions
- Functions are first-class; they are objects themselves. As such, they have properties and methods, such as
lengthandcall(); and they can be assigned to variables, passed as arguments,returned by other functions, and manipulated like any other object. Any reference to a function allows it to be invoked using the()operator. - nested functions
- "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.
Prototype-based
- 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
newcreates a new object and calls that function with its localthiskeyword bound to that object for that invocation. The constructor'sprototypeproperty determines the object used for the new object's internal prototype. JavaScript's built-in constructors, such asArray, 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 as a method. When a function is called as a method of an object, the function's local
thiskeyword is bound to that object for that invocation.
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
argumentsobject. - 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 JSON data 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.
Vendor-specific extensions
JavaScript is officially managed by Mozilla Foundation, and new language features are added periodically. However, only some non-Mozilla JavaScript engines support these new features:- property getter and setter functions (also supported by WebKit, Opera,ActionScript, and Rhino)
- conditional
catchclauses - 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 new
letkeyword - 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
Syntax and semantics
Main article: JavaScript syntax
As of 2011[update], 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 E4X (ECMA-357) support and experimental features considered for inclusion into future ECMAScript editions, are documented hereSimple examples
A simple recursive function:function factorial(n) { if (n == 0) { return 1; } return n * factorial(n - 1); }
function displayClosure() { var count = 0; return function() { return count++; }; } var inc = displayClosure(); inc(); // returns 0 inc(); // returns 1 inc(); // returns 2
function sum() { var x = 0; for (var i = 0; i < arguments.length; ++i) { x += arguments[i]; } return x; } sum(1, 2, 3); // returns 6
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; }, "lcm"/* can use strings here */: 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); } // 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() + "<br>"); });
LCMCalculator: a = 28, b = 56, gcd = 28, lcm = 56 LCMCalculator: a = 21, b = 56, gcd = 7, lcm = 168 LCMCalculator: a = 25, b = 55, gcd = 5, lcm = 275 LCMCalculator: a = 22, b = 58, gcd = 2, lcm = 638
source:Wikipedia
11.17
fanditkc

Posted in: 


0 comments:
Posting Komentar