JavaScript in 10 minutes
------------------------
yet another Bennish-summary,
based on O'Reilly Pocket Reference
General
-------
* overall syntax is very similar to java and C:
- case-sensitive
- whitespace-insensitive
- statements terminate with semicolon
- blocks use curly braces
- allows both C and C++ style comments
* language is object-oriented.
* variables are UNTYPED. declare them with 'var':
var i = 6;
var name = 'george';
* scoping is weird.
- global vars are properties of special Global object
- local vars are properties of function's Argument object
- NO block-level scoping of variables:
{ var baz, foo = 2; {var bar = 5; } baz = foo + bar}
'bar' is visible everywhere in function! but not outside function.
Data Types
----------
A. Numbers
All numbers are 64-bit floats, end of story. You can write
them with our without decimal points: 4, 3.49, -34.9888888.
Hex numbers are legal: 0xB7
Overflows return special values, underflows return 0.
Divide by zero (and similar) return "NaN"; detect with isNaN().
Number object has nice methods. So does Math object.
B. Booleans
var foo = true;
var bar = false;
C. Strings
IMMUTABLE 16-bit unicode.
Can use single or double quotes, nested too: 'foo "bar" baz'
Standard escapes: \n, \r, \t, etc.
String class has builtin .length() method.
Concatenation: var newstring = "foo" + "bar";
Compare by string value: (foo == bar)
D. Objects
Data within objects ("properties") just appear as needed. No
need to declare class internals.
var foo = new BlahObject(3, 4); // constructor
foo.x = 19;
foo.somevar = "hello";
var q = foo["somevar"] // can be accessed via array notation too
var w = {x:19, somevar:"hello"} // curly braces initialization
E. Arrays
An object whose keys are strictly numbers.
var moo = new Array(15); // 15 elements
moo[4] = "whatup";
var other_array = [19, 23, "blah", 8]; // square bracket init.
F. "null" --> just like python's "None".
Functions
---------
Declare with 'function' keyword:
function foo(x, y) {
...
}
Just like Java and C.
Every function contains an implicit arguments[] array that lists
incoming args.
Operators
---------
All the usual Java/C operators, plus some extras:
typeof returns number,string,boolean,object,function
in if (color in object) ==> returns boolean
instanceof if (foo instanceof MyClass) ==> boolean
delete delete foo.color ==> removes property.
=== "strict" version of == , requires type match.
!==
Control
-------
Just like C :
if / else if / else
return
while (), do {} while ()
for (__; ___; ___)
switch/case/break/default
continue
try/catch/throw/finally
Not like C:
for (x in object) ==> assigns every property of object to x
Objects
-------
Define a object type by defining a constructor using 'this':
function Foo(x,y) {
this.x = x;
this.y = y;
}
Then add stuff to the constructor's 'prototype' property to expand
the class:
Foo.prototype.MyMethod = function(z) {
{
return (z + this.x + this.y);
}
To declare *static* class variables or methods, add them directly
to the constructor:
Foo.PI = 3.14159
Regexps
-------
Basically it's Perl regexp syntax: /regexp/
Invoke through various string methods:
foo = "hello world";
var result = foo.search(/llo/); // returns position of match, or -1
var result2 = foo.replace(/world/gi, "planet");
var matchlist = foo.match(/l/g); // returns array of results
Lots of features here, see book. There's also a python-style
RegExp class you can use.
WEB BROWSER USAGE and APIs
--------------------------
Embed via .
Or, point to source:
-- Event Handlers: always of form "onFoo()"
...more to come...