14.07.2013 Views

Essentials of Javascript - Cultural View

Essentials of Javascript - Cultural View

Essentials of Javascript - Cultural View

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

JavaScript 4<br />

Prototype-based<br />

prototypes<br />

JavaScript uses prototypes instead <strong>of</strong> classes for inheritance. It is possible to simulate many class-based<br />

features with prototypes in JavaScript.<br />

functions as object constructors<br />

Functions double as object constructors along with their typical role. Prefixing a function call with new creates<br />

a new object and calls that function with its local this keyword bound to that object for that invocation. The<br />

constructor's prototype property determines the object used for the new object's internal prototype. JavaScript's<br />

built-in constructors, such as Array, also have prototypes that can be modified.<br />

functions as methods<br />

Unlike many object-oriented languages, there is no distinction between a function definition and a method<br />

definition. Rather, the distinction occurs during function calling; a function can be called as a method. When a<br />

function is called as a method <strong>of</strong> an object, the function's local this keyword is bound to that object for that<br />

invocation.<br />

Miscellaneous<br />

run-time environment<br />

JavaScript typically relies on a run-time environment (e.g. in a web browser) to provide objects and methods<br />

by which scripts can interact with "the outside world". In fact, it relies on the environment to provide the<br />

ability to include/import scripts (e.g. HTML elements). (This is not a language feature per se, but it is<br />

common in most JavaScript implementations.)<br />

variadic functions<br />

An indefinite number <strong>of</strong> parameters can be passed to a function. The function can access them through formal<br />

parameters and also through the local arguments object.<br />

array and object literals<br />

Like many scripting languages, arrays and objects (associative arrays in other languages) can each be created<br />

with a succinct shortcut syntax. In fact, these literals form the basis <strong>of</strong> the JSON data format.<br />

regular expressions<br />

JavaScript also supports regular expressions in a manner similar to Perl, which provide a concise and powerful<br />

syntax for text manipulation that is more sophisticated than the built-in string functions.<br />

Vendor-specific extensions<br />

JavaScript is <strong>of</strong>ficially managed by Mozilla Foundation, and new language features are added periodically. However,<br />

only some non-Mozilla JavaScript engines support these new features:<br />

• property getter and setter functions (also supported by WebKit, Opera, [25] ActionScript, and Rhino) [26]<br />

• conditional catch clauses<br />

• iterator protocol adopted from Python<br />

• shallow generators/coroutines also adopted from Python<br />

• array comprehensions and generator expressions also adopted from Python<br />

• proper block scope via new let keyword<br />

• array and object destructuring (limited form <strong>of</strong> pattern matching)<br />

• concise function expressions (function(args) expr)<br />

• ECMAScript for XML (E4X), an extension that adds native XML support to ECMAScript

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!