# 零秒精通JavaScript是真的

https://github.com/AbstProcDo/Master-JavaScript-Instantly

1 个赞

2 个赞

10 个赞

1 个赞

# 一、构成程序的基本元素

## 1.数学表达式 Expression

JavaScript使用完全符合我们直觉的infix中序表达式：

``````// 加减乘除
> 173 + 373
546
> 1002 - 457
545
> 8 * 43
344
> 11 / 3
3.6666666666666665
> 11.0 / 4
2.75
> 10 / 2
5
> 3.7 + 10
13.7
//模除
> 11 % 3
2
//乘方
> 2 ** 11
2048
//组合
> (1 - ((5 / 2) * 4)) + 3
-6
``````

## 2.定义常量与变量

``````//定义常量
> const pi = 3.1415926 //const for constant 不变的值
undefined
//定义变量
> var radius = 11 //var for variable 变化的值
undefined
> pi * radius ** 2
380.1327046
> var area = pi * radius ** 2
undefined
> area
380.1327046
> var circumference = 2 * pi * radius
undefined
> circumference
69.1150372
``````

## 3.函数 Function

``````> function square(x) {
... return x ** 2;
... }
undefined
> square(11)
121
``````

``````function name(parameters) { return expression; }
``````

``````> square(11 + 13);
576
``````

``````> function sum_of_squares(x,y) {
... return square(x) + square(y);
... }
> sum_of_squares(3, 4)
25
``````

``````> function f(a) {
... return sum_of_squares(a+1, a*2);
... }
undefined
> f(7)
260
``````

## 4.条件判断表达式 Conditional Expressions and Predicates

``````predicate ?  //predicate 这个术语用得最妙
consequent-expression :
alternative-expression
``````

``````function abs(x) {
return x >= 0 ? x : -x;
}

> abs(-5);
5
``````

``````> expression1 && expression2
> expression1 || expression2
> ! expression
``````

``````x > 15 && x < 20
``````

``````> function greater_or_equal(x, y) {
... return x > y || x === y;
... }
> function greater_or_equal_alter(x, y) {
... return ! (x < y);
... }
``````

``````> function p() {return p();}

> function test(x, y) {
... return x === 0 ? 0 : y;
... }

> test(0, p())
Uncaught RangeError: Maximum call stack size exceeded
at p (REPL80:1:15)
at p (REPL80:1:22)
at p (REPL80:1:22)
at p (REPL80:1:22)
at p (REPL80:1:22)
at p (REPL80:1:22)
at p (REPL80:1:22)
at p (REPL80:1:22)
at p (REPL80:1:22)
at p (REPL80:1:22)
``````

## 5.迭代与递归

``````> function sqrt_iter(guess, x) {
...     return good_enough(guess, x)
...         ? guess
...         : sqrt_iter(improve(guess, x), x);
... }

> function improve(guess, x) {
...     return average(guess, x/guess);
... }

> function average(x, y) {
...     return (x + y) / 2;
... }

> function good_enough(guess, x){
...     return abs(square(guess) - x) < 0.001;
... }

> function sqrt(x) {
...     return sqrt_iter(1, x);
... }

> sqrt(9)
3.00009155413138
``````

``````(x/y**2 + 2y) / 3
function abs(x) {
return x >= 0 ? x : -x;
}

function cube(x) {
return x * x * x;
}

function good_enough(guess, x) {
return abs(cube(guess) - x) < 0.001;
}
function div3(x, y) {
return (x + y) / 3;
}
function improve(guess, x) {
return div3(x / (guess * guess), 2 * guess);
}
function cube_root(guess, x) {
return good_enough(guess, x)
? guess
: cube_root(improve(guess, x), x);
}
//对仗真是工整呀。
cube_root(3, 27);
``````

## 6.函数作为抽象的思维工具 Functions as Black-Box Abstractions

``````function square(x) {
return x * x;
}

function square(x) {
return math_exp(double(math_log(x)));
}
function double(x) {
return x + x;
}
``````

``````function square(x) {
return x * x;
}
function square() {
return y* y;
}
``````

``````function abs(x) {
return x >= 0 ? x : -x;
}

function square(x) {
return x * x;
}

function average(x,y) {
return (x + y) / 2;
}

function sqrt(x) {
function good_enough(guess, x) {
return abs(square(guess) - x) < 0.001;
}
function improve(guess, x) {
return average(guess, x / guess);
}
function sqrt_iter(guess, x) {
return good_enough(guess, x)
? guess
: sqrt_iter(improve(guess, x), x);
}
return sqrt_iter(1, x);
}

sqrt(5);
``````

## 7.收尾总结

2 个赞

1.数字是最原始的数据，运算符是最原始的函数。
2.在此基础上，编程语言引入“命名变量”程序踏步迈入抽象化表达门径；
3.作为“变量”的复杂形式，给动态的程序命名的函数，将程序的表达力垂直拔起。
4.“条件判断”赋予程序以“智能”，
5.而“迭代与递归”实现从“人扛马拉”到自动化的奋然一跃。
6.最后，编程语言Javascript作为语言的一种，是思维的利器，将函数作为“思维抽象体”thinking-abstraction，给解决问题的思维过程添砖加瓦。

2 个赞

2 个赞