js
From QED
JavaScript in a nutshell
| “ | In five years I expect that dynamic languages (the successors inspired by today's languages, or today's languages evolved) overtake Java and C++ as the dominant implementation languages in most areas. Moore's law is on our side. | ” |
| —Guido Van Rossum, 2005[1] | ||
| “ | If you are doing any web development right now, you have to deal with Javascript whether you want it or not. | ” |
| — Blogger | ||
JavaScript is a scripting language with a syntax like C but it has more in common with functional languages.
It is the J in AJAX. It is the basis for all “thick” client models for building apps on the web.
- AJAX really just means “using the asynchronous calls in Javascript”.
- XMLHttpRequest is a misnomer - one can use JSON just as well.
Top 10 one-liners about JavaScript
"JavaScript is the most widely-deployed runtime environment in existence."
"JavaScript is the most successful scripting language ever."
"JavasSript is the most widely deployed language in the world."
"JavaScript: The World's Most Misunderstood Programming Language"[2]
.....—The Web
"Javascript [became] what nobody expected;
namely a programming language for web applications."—Lars Bak [3]
42,889,748 total downloads— NoScript Firefox add-on [4]
"The Internet breathes JavaScript"— www.ejscript.org
Javascript has something that neither Python nor Ruby possess - a real spec.
"If you want to scale back power consumption on mobile devices,
V8 can be a good choice."—Lars Bak, technical lead for the V8 design at Google
"Sure, obj-c is nice but writing your app three times is barbaric."— John Resig[5]
Top 10 moments in JavaScript history
- 1995 - JavaScript included in Netscape browser version 2.0B3
- 1996 - Microsoft's dialect, JScript, was included with IE3
- 1997 - ECMA-262 (ECMAScript)
- 1999 - Most recent ECMAScript (Third Edition) - JavaScript 1.5
- 2000 - DOM Level 2 published
| Engine | Dialect |
|---|---|
| Google Chrome V8 | JavaScript 1.5 |
| Firefox, SpiderMonkey, Rhino, ... | JavaScript 1.8 |
| Apple Safari | JavaScript 1.5 |
| MSIE | JScript 5.7 (see discrepancies) |
| .NET | JScript.NET 8.0 |
| Adobe Acrobat | JavaScript 1.5 |
- 2004 Google Mail [6]
- 2005 Google Maps (JavaScript and JSON)
- 2005 AJAX (W3C released draft spec for XMLHttpRequest in 2006)
- 2006 Google Web Toolkit; jQuery
- 2008 Dojo 1.0
Top 10 JavaScript demos
- Google
- Google Documents: What's New
- Google Maps
- Google Mail
- http://www.chromeexperiments.com — Bounce
- O3D e.g. Home Designer
- Simile, e.g.
- 2D animation
- Gravity
- 3D rotation demos in JavaScript:
- Fisheye
- jsMath
- FCKEditor
- Uize demos
- class=zebra [8]
- booklist.magic
- Slide Show Editor and Presenter
Top 10 one-liners in JavaScript
- javascript:resizeTo(800,600);
- javascript:alert(navigator.javaEnabled())
- javascript:alert(navigator.appName+navigator.appVersion)
- dojo.isBrowser, dojo.isFF, dojo.isIE, dojo.isSafari, etc
- javascript:void((function(){var q=document.getSelection?document:window;q=q.getSelection();if(q=="")q=prompt('Wikipedia:',''); if(q)location.href='http://en.wikipedia.org/w/wiki.phtml?search='+escape(q)})())
- s.replace(RE, t)
- See also: s.match(RE); RE.test(s)
- s.replace(/^./, String.toUpperCase)
- s.replace(/^./, function(x) { return x.toUpperCase(); } )
- Exercise: 'abc def'.replace(/\b./g, function(x) { return x.toUpperCase(); } )
- javascript:void(document.getElementById('javascriptEnabled').style.display='none');
- JavaScript is not enabled
- make visible
- Cf. dojo.style("javascriptEnabled", "display", "none")
- x = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(text.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + text + ')');
- — a safe and fast eval for JSON to JavaScript
- args=args(); alert(args.x)
// Parse the query string of a URL (or the incoming URL), returning a hash.
// Note that "&x=" yields "" whereas "&x" yields undefined.
function args(parms) {
if (!parms) parms = window.location.search.substring(1).split('&');
var x, ans={};
for (var i in parms) {
x = parms[i].split('=');
ans[x[0]] = x[1];
}
return ans;
}
P.S. "This page requires JavaScript"
<NOSCRIPT> <H3>This page requires JavaScript</H3> </NOSCRIPT>
Top 10 JavaScript language features
- C or C-like syntax
- expressions, statements, blocks
- () {} ; = , ?: return
- structured programming constructs: if, while, switch, for
- for (var i = 0; i < N; i++) { ... }
- function NAME(ARG, ...) { BODY }
- JavaScript 1.7 uses let for block-level scoping
- Arrays
- IO=0
- ["a", '"b"', [1,2] ]
- Objects are associative arrays
- Object literal notation => JSON
- { name: "John", sons: [ { name: "Junior" } ] }
- kwArgs
- Object literal notation => JSON
- Functions are first-class objects
- Every function is a Function object
- Functions can be defined in several ways:
- function f() {}
- f = function() {}
- f = new Function( [], "" )
- Constructors are special-purpose functions
- new F() calls function F with this initially bound to {}, and returns this.
- function Building(name) { this.name = name; }
- function Building(kwArgs) { this.name = kwArgs.name; }
- new F() calls function F with this initially bound to {}, and returns this.
- Methods
- Any function can be invoked as a method
- target.f()
- Any function can be invoked as a method
- Inner functions and closures
- Modules -
- Global = { f: function() {} }
- vs:
- Module = function() { return { f: function() {} }; }()
- Global = { f: function() {} }
- Modules -
- Regular expressions
- Delegation
- Use your selected library's approach to "classes" and inheritance
- for (x in obj) { ... } loops over all user-defined property names
- Support for asynchrony in browsers
- "script tag hack"
- Asynchronous callbacks (e.g. onclick)
- XMLHttpRequest
Top 10 peeves about JavaScript
(See JavaScript: The Good Parts for a list of the bad parts.)
- Global variables
- No block scope
- There is no built-in assertion mechanism
- ideal assertion mechanism cannot be implemented in JS 1.5
- ideal: assert( f() )
- compromise: assert( "f()" )
- ideal assertion mechanism cannot be implemented in JS 1.5
- There is no "hasOwnProperty" equivalent of "in" in "for (_ in _)"
- workaround: for(p in o) { if (o.hasOwnProperty(p) ... }
Top 10 JavaScript libraries and frameworks
- The "Prototype Javascript framework" is implemented as a single file, typically prototype.js
- Script.aculo.us is built on Prototype.
- Flotr Javascript Plotting Library is also built on Prototype.
- Google Web Toolkit (GWT) — software development in Java
- jQuery
- MochiKit
- MooTools
- YUI (Yahoo! UI Library)
- dojo
- Uize (pron. U-ize)
- JS.Class — a library that implements Ruby’s core object, module and class system
Top 10 JavaScript tools
- "Javascript: The Good Parts" by Douglas Crockford
- Firebug
- Works adequately in Chrome ..
- Firebug Lite for other browsers
- alert()
- throw()
- e.g. throw("Missing input")
- JSLint
- JSONLint
Top 10 news items
- FF3.1, IE8 and ECMAScript 5 all support JSON
- "iPhone 3.0 handles JavaScript 3x-10x faster than iPhone 2.1."
- Server-side JavaScript
- "Cross-Origin Resource Sharing" — W3C Working Draft 17 March 2009
- ECMAScript 5 is likely to be published towards the end of 2009
V8 (Google Chrome)
| “ | Not your mother's JavaScript | ” |
| — chromeexperiments.com [9] | ||
"Assorted benchmarks" from August, 2008.












