by Marjin Haverbeke
-
-
Save dekom/2048062 to your computer and use it in GitHub Desktop.
There are six basic types of values:
- numbers: All numbers in JavaScript are 64-bit. 1 sign bit, 11 decimal bits, and 52 whole number bits.
- strings: Standard string, either double or single quotes, with backslash () escapes.
- booleans: Standard true/false values with
&&, ||, !
operators. Extension!||
tests from left to right and returns the firsttrue
value.&&
returns the firstfalse
value or the right-most value (last expression to be evaluated) - objects: Variables ... zzz (var ... )
- functions:
alert
,show
,confirm
,prompt
,Number
,String
,Boolean
- undefined values:
null
andundefined
,null == undefined /* => true */
, butnull === undefined; /* => false */ null !== undefined /* => true */
function name(args ...) {
...
}
Empty return
statement returns undefined
.
Pure functions
= functional programming
Impure functions
= procedural or Object-oriented
Functions are first-class citizens. They can be passed around and reassigned. Thus, JavaScript functions also exhibit closure. And...one can create lambda functions (anonymous functions).
function alertIsPrint(value) {
var alert = print; /* assignment */
alert(value);
}
Of course, there is lexical scoping, where enclosed functions have access to variables and values of its parent function as well as the top-level environment. "Yo dawg, I heard you like functions, so we put a function inside your function..."
In JS, objects are meant to hold other values, that is to say that they're more containers than object-oriented objects.
To create object:
var name = {attribute_name: attribute_value, ...}
name.attribute_name /* => attribute_value */
delete name.attribute_name
name.attribute_name /* => undefined */
name.new_attribute_name = new_attribute_value /* Creates new attribute/value pair */
name{"attribute name with spaces"] = "attribute_value"
name["attribute name with spaces"] /* => "attribute_value" */
name["attribute_name"] /* same thing as name.attribute_name */
"string".length == "string"["length"]
To test existence of an attribute, use the in
operator:
"attribute_name" in name /* => true */
Common functions, Google them if you need specifics:
var arr = []
arr.push("one")
arr.pop()
arr.join(" ")
"string".split(delimiter)
"string".slice(i, j)
new Date(year, month, day, hour, minute, second, milliseconds) var date = new Date(...) date.getFullYear(), date.getMonth()... date.getTime() date.setMonth()...
"Whenever a function is called, a special variable named arguments is added to the environment in which the function body runs. This variable refers to an object that resembles an array. It has a property 0 for the first argument, 1 for the second, and so on for every argument the function was given. It also has a length property."
Here are references for JS: JS DOM and MDN Reference