BINDINGS or variables
keyword: let, var, const:
BINDING NAMES:
A binding name may include dollar signs ($) or underscores (_) but no other
punctuation or special characters.
ENVIRONMENT:
The collection of bindings and their values that exist at a given time is called
the environment. When a program starts up, this environment is not empty. It
always contains bindings that are part of the language standard, and most of the
time, it also has bindings that provide ways to interact with the surrounding
system. For example, in a browser, there are functions to interact with the
currently loaded website and to read mouse and keyboard input.
FUNCTIONS:
A function is a piece of program wrapped in a value .
Control flow :
C类似: if whiel, do-while, for-loops, break, continue, switch-case
Functions as values
let launchMissiles = function() {
missileSystem.launch("now");
};
Declaration notation (没有顺序)
function square(x) {
return x * x;
}
Arrow functions
const square1 = (x) => { return x * x; };Optional Arguments
function minus(a, b) {
if (b === undefined) return -a;
else return a - b;
}
Rest parameters
function max(...numbers) {
let result = -Infinity;
for (let number of numbers) {
if (number > result) result = number;
}
return result;
}
console.log(max(4, 1, 9, -2));
// → 9
function power(base, exponent = 2) {
let result = 1;
for (let count = 0; count < exponent; count++) {
result *= base;
}
return result;
}
Data Structures: Objects and Arrays
let anObject = {left: 1, right: 2};
console.log(anObject.left);
// → 1
delete anObject.left;
console.log(anObject.left);
// → undefined
console.log("left" in anObject);
// → false
console.log("right" in anObject);
// → true
console.log(Object.keys({x: 0, y: 0, z: 2}));
// → ["x", "y", "z"]
let objectA = {a: 1, b: 2};
Object.assign(objectA, {b: 3, c: 4});
console.log(objectA);
// → {a: 1, b: 3, c: 4}
mutability:
对象的比较是对象id的比较,类似python对象 is 比较
Number , string, boolen, 内置数据类型不可变,
let kim = "Kim";
kim.age = 88;
console.log(kim.age);
// → undefined
DESTRUCTURE
function phi(table) {
return (table[3] * table[0] - table[2] * table[1]) /
Math.sqrt((table[2] + table[3]) *
(table[0] + table[1]) *
(table[1] + table[3]) *
(table[0] + table[2]));
}
function phi([n00, n01, n10, n11]) {
return (n11 * n00 - n10 * n01) /
Math.sqrt((n10 + n11) * (n00 + n01) *
(n01 + n11) * (n00 + n10));
}
JSON
JSON.stringify(object) , JSON.parse(string)