千天计划之第981天——half notes of chapter one
这是相当平凡的一天。
这章比较长,分两天写吧,又在23.00之前不能睡觉了。T-T
这章我们开始真正的编程,展开javascript语言命令,用所知碎片的名词与句子,组成散文的表达意义。
expressions and statements
A fragment of code that produces a value is called an expression.
只要写下来的,都可以是表达式。双括号里面包裹着表达式也是一个表达。
An expression between parentheses is also an expression, as is a binary operator applied to two expressions or a unary operator applied to one.
编程只是一些陈述。
If an expression corresponds to a sentence fragment, a JavaScript statement corresponds to a full sentence in a human language. A program is simply a list of statements.
The simplest kind of statement is an expression with a semicolon after it. This is a program
比如:
1;
!false;
当然,这个编程没什么用处。只有编程可影响这个世界时,才具有独立性的和等同于其他事物,比如在屏幕上展现内容。或者,它也可以改变机器的代理中心,以此来影响接下来的编程运行,我们把这个叫做side effects.
这本书里,每个编程的写作结束,都需要一个分号,否则都会出错。
variables
程序如何做到有记忆呢?
To catch and hold values, JavaScript provides a thing called a variable.
var caught = 5 * 5;
caught是我们命名的一个变量。
And that gives us our second kind of statement. The special word (keyword) var indicates that this sentence is going to define a variable.
我们用=来传递expression,之后这个变量就代表了这个expression.
The value of such an expression is the value the variable currently holds.
Here’s an example:
var ten = 10;
console.log(ten * ten);
// → 100
数字也可以为变量名的一部分,比如catch22,但是不能以开头数字,也不能包含标点符号,不过$和_是可以的。(美元和下划线)
=传递的expression不是一直待在变量里面的,它可以随时用=再转递一个expression。
看例子:
var mood = "light";
console.log(mood);
// → light
mood = "dark";
console.log(mood);
// → dark
把variables想象成章鱼的胡须,而不是一个盒子,因为它们不包含values——两个variables可以指代同一个value.
看个例子:
为了记住Luigi还欠你多少钱,你创造了一个variable,然后当他还你35美元时,你就give this variable a new value.
var LuigisDebt = 140;
LuigisDebt = LuigisDebt - 35;
console.log(LuigisDebt);
// → 105
一个var可能定义多个variables
看例子:
var one = 1, two = 2;
console.log(one + two);
// → 3
keywords and reserved words
像var这样有特殊意义的keywards不能命名为变量名。
以下是所有这样的词:
break case catch class const continue debuggerdefault delete do else enum export extends false
finally for function if implements import in
instanceof interface let new null package private
protected public return static super switch this
throw true try typeof var void while with yield
不用去记,但是要知道,当程序出现问题时,可能就是这个问题。
the environment
The collection of variables and their values that exist at a given time is called the environment.
例如
浏览器里包含了用于检查和相互影响当前网站的variables and functions,这些会读取鼠标和键盘输入的数据。
functions
A function is a piece of program wrapped in a value
例如,在浏览器环境里,变量名alert拥有这样一个功能
shows a little dialog box with a message. It is used like this:
alert("Good morning!");
An alert dialog :
执行一段代码,我们把这叫做invoking, calling, 或者applying. 你可以把一段具有functions的expression嵌入双括号里。
In the example, the alert function uses the string that we give it as the text to show in the dialog box.
functions的value被称作arguments。
the console.log function
我们用这个输出结果。大部分javascript系统,包括现代浏览器和node.js,provide a console.log function that writes out its arguments to some text output device.
大部分都默认隐藏起来,但你可以按F12(windows),或Command-Option-I(on mac)来唤起,要是不起作用,就找菜单栏里的web console,或开发者工具。
var x = 30;
console.log("the value of x is", x);
// → the value of x is 30
尽管变量名不包含标点符号,但很明显,console.log就有一个,因为这个不是简单的变量名,我们在第四章详细介绍。
return values
functions可能产生values。
例如:Math.max,取走所有数字,然后回赠最大值。
console.log(Math.max(2, 4));
// → 4
Here a call to Math.min, which is the opposite of Math.max.
console.log(Math.min(2, 4) + 100);
// → 102
prompt and confirm
浏览器总会弹出一个alert window, 要去你取消或者确认,那么, This returns a Boolean: true if the user clicks OK and false if the user clicks Cancel.
confirm("Shall we, then?");
promt function可以用来询问一个开放式问题,第一个argunemt是所描述的问题,第二个用户开始回答的地方。文本可以在这里输入,这个功能函数会把这个文本当作一个string处理。
prompt("Tell me everything you know.", "...");
尽管现代网页编程里用得不多,但是 they are useful for toy programs and experiments.
control flow
程序包含很多statements时,这些代码会从上到下一次执行。
看例子,第一个要求输入数字,第二个执行这个数字的平方:
var theNumber = Number(prompt("Pick a number", ""));
alert("Your number is the square root of " +
theNumber * theNumber);
这个function Number 把一个value转换成number, 因为prompt的结果是a string value, 而我们想要的结果是number。
here are similar functions called String and Boolean that convert values to those types.
这是一个相当平凡的一个例子:
conditional execution
我们在两种基于Boolean value的路线上选择是,可供选择的是conditional execution
Conditional execution is written with the if keyword in JavaScript.
简单来讲,我们就是要一些代码在if, only if 的情况下执行。
例如,之前的程序里,我们可能想只展示输出是数字的结果:
var theNumber = Number(prompt("Pick a number", ""));
if (!isNaN(theNumber))
alert("Your number is the square root of " +
theNumber * theNumber);
这种情况下,你输入cheese, 就不展示出任何东西。
The isNaN function is a standard JavaScript function that returns true only if the argument it is given is NaN
如果条件为其他情况时,可以用else这个key word
var theNumber = Number(prompt("Pick a number", ""));
if (!isNaN(theNumber))
alert("Your number is the square root of " +
theNumber * theNumber);
else
alert("Hey. Why didn't you give me a number?");
If we have more than two paths to choose from, multiple if/else pairs can be “chained” together. Here’s an example:
var num = Number(prompt("Pick a number", "0"));
if (num < 10)
alert("Small");
else if (num < 100)
alert("Medium");
else
alert("Large");
这个程序检查结果,如果是小于10,输出Small; 否则,如果小于100,输出Medium; ,否则,输出Large.
这看起来,像这样: