千天计划之第972天——复习
The best cure for the body is a quiet mind.
配图不是我学校的油菜.....
时间不等人。
昨晚22.45躺下,23.02丢掉手机,23.35入睡。(当然这些都只是大概的样子,我睡时听着歌,定了30分钟的自动播放。)
早上被3.05的闹钟吵醒了,但是没起来,想再睡会,然后没听到4.29的闹钟,5.29醒来的,洗漱、打开电脑,转瞬已是5.41.
如果今天再无进度的话,这个计划算是废了。
开始复习,一直在想,是看自己的学习笔记呢,还是直接看原文?
哪个方便看哪个吧,都看看更好。
千天计划之第983天——notes of eloquent javascript||introduction
那我觉得应该以目的为导向,这样复习的效果更好。
目的为导向就是去解决问题,比如第一道题。
那么我就列出感觉会用到的知识点吧:
1. strings
这个代表文本,text, 写作单/双引号之内。几乎任何东西都可以放进去,但有些比较难,比如引号放在引号里。换行符号也不能放进去。string只能保持一行。
引号中的/之后有不同意义,尽管还是引号内的一部分,比如引号中的/n代表引号中的换行,引号中的/t代表引号中的tab charater。
2. unary operators
console.log代表输出,即看看我们代码运算的结果。
3. while and do loops
设想一个程序要打印出从0到12的所有数字。
一种写作方法是这样的:
console.log(0);
console.log(2);
console.log(4);
console.log(6);
console.log(8);
console.log(10);
console.log(12);
这样也可以,但违反了编程的本质:减少工作量。如果我们的数字大于1000,那怎么办?很明显,前面的方法就不奏效了,我们需要一种以重复执行某些代码的方式,这种形式的control flow就叫做一个loop,即循环。
looping control flow允许我们返回到之前的某个程序执行点,然后在当前状态下重复执行后面的程序,如果再配合上variables,我们就可以做点有意思的事儿:
var number = 0;
while (number <= 12) {
console.log(number);
number = number + 2;
}
// → 0
// → 2
// … etcetera
以while为关键词开头的语句就是一个循环,loop,下接一个用元括号包裹着的表达式,非常像if语句。循环直到产生为真的value才结束。
在这个循环里,我们既可以打印下来当前已经产生的数字,又可以再加两个变量。无论何时我们需要在loop里面执行多语句代码,只需要把它们包裹在卷括号里面就可以,{},叫做一个block。
nunber验证了variable追踪循环的次数,即循环重复一次,number增加1,然后在重复的每次开始的当儿,它都会与12比较,来决定是否进行下去,任务是否完成。
好,我们现在可以写个小程序了。计算一下2的10次方的结果:我们用到两个变量, 一个来记录我们的每次运算结果,一个来记录我们计算了多少次。这个循环会测试下第二个变量是否达到10,然后同时同步更新两个变量的值。
var result = 1;
var counter = 0;
while (counter < 10) {
result = result * 2;
counter = counter + 1;
}
console.log(result);
// → 1024
在这里,counter也可以从1开始算起,然后到小于或等于10结束,但最好从0开始,我们会在第四章详细介绍。
do loop 和while loop相似,不同点在于,do loop 总是至少执行代码一次,然后开始测试是否终止,所以这个表示测试的代码也就出现在loop之后。
do {
var yourName = prompt("Who are you?");
} while (!yourName);
console.log(yourName);
这个程序会要求你输入名字,然后会一直一直询问,直到得到的不是个无效值。这里面,!会把所有的有效值转换为条件为真,然后循环结束。
4. for loops
前面的while语句例子总有许多这样的模式:
首先,counter变量为了记录循环次数
然后,,while循环中测试表达式检验counter是否达到临界点
最后,countet增加计量一次
因为这种模式很普遍,所以javascript等语言提供了一直简单的且可理解性更强的形式,for loop
for (var number = 0; number <= 12; number = number + 2)
console.log(number);
// → 0
// → 2
// … etcetera
代码减少了,是因为都把它们放到一个组里面去了。
for后面必须包含括号,其中,第一个是定义变量,第二个是检验循环是否进行的表达式,最后一个是实时更新number的状态。大多数情况下,这种形式都比while简单。
这里用for语句计算2的20次方:
var result = 1;
for (var counter = 0; counter < 10; counter = counter + 1)
result = result * 2;
console.log(result);
// → 1024
这两个是循环,为了精简代码和减少劳动,肯定是会用到的,switch应该用不到,break可能会用到。
5. breaking out of a loop
循环出错不是循环终止的唯一方式。
break语句可以立即跳出循环使其终止。
看例子:
for (var current = 20; ; current++) {
if (current % 7 == 0)
break;
}
console.log(current);
// → 21
它找到第一个能被七整除的不小于20的数字。
用%可以很容易地测试出一个数能不能被另一个数整除,如果能,则余数为零。
for循环里无检查是否该结束的部分,所以除非break语句执行,否则循环永不终止。
如果你遗漏了break,或者无意间把程序产生的结果都写错成为真,你的程序就会无限循环,这可真糟。
不过若真是这样,页面会过几秒后自动显示,是否终止,要是你点了否,那唯一的人解决办法就只能是关闭整个页面,或者重启浏览器。
continue语句只跳出循环,继续下一个,像是你咬到舌头了,感觉停,然后接着吃饭;而break语句则是像碰着地雷了,完蛋,游戏结束,不再循环。
6. updating variables succinctly(简洁地)
循环中,需要计数工具,否则谁知道你来来回回算来几次啊,所以有
counter = counter + 1;
javascript有更简单的:
counter += 1;
相似的还有:
result *= 2 to double result or counter -= 1 to count downward.
这样程序就更简单。
for (var number = 0; number <= 12; number += 2)
console.log(number);
对于counter += 1 and counter -= 1,
用这个:
counter++ and counter--.
7. dispatching on a value with switch
代码共同点:
if (variable == "value1") action1();
else if (variable == "value2") action2();
else if (variable == "value3") action3();
else defaultAction();
这叫做,switch语句,用来直接解决dispatch,不过还有另一种:
switch (prompt("What is the weather like?")) {
case "rainy":
console.log("Remember to bring an umbrella.");
break;
case "sunny":
console.log("Dress lightly.");
case "cloudy":
console.log("Go outside.");
break;
default:
console.log("Unknown weather type!");
break;
}
你看哪个更好看啊?
警告:break语句不能少,别人就掉小黑屋里了。
8. capitalization
变量名不包含,空格,但是命名有很多技巧:
fuzzylittleturtle
fuzzy_little_turtle
FuzzyLittleTurtle
fuzzyLittleTurtle
不过建议使用最后一个,可读性强,看着还顺眼,而且很好上手。
9. comments
这就是用来把艰涩晦深的代码变成人话,而且电脑还不搭理它们。
这多好。
记牢了,写作有两种方式:
第一,先写//,然后写人话
比如,
var accountBalance = calculateBalance(account);
// It's a green hollow where a river sings
accountBalance.adjust();
// Madly catching white tatters in the grass.
var report = new Report();
// Where the sun on the proud mountain rings:
addToReport(accountBalance, report);
// It's a little valley, foaming like light in a glass.
第二,/*和*/之间加人话。
比如,
/*
I first found this number scrawled on the back of one of
my notebooks a few years ago. Since then, it has often
dropped by, showing up in phone numbers and the serial
numbers of products that I've bought. It obviously likes
me, so I've decided to keep it.
*/
var myNumber = 11213;
各有各的用处,哪个方便用哪个。
已经六点了,不过还是看笔记复习快,不然为什么要做笔记呢?
其实看第一篇笔记是全英文的,我也没奶住性子好好看==||。
那就明天开始尝试做题吧。
No, you’re not entitled to your opinion
By Patrick Stokes
Senior Lecturer in Philosophy, Deakin University
Every year, I try to do at least two things with my students at least once. First, I make a point of addressing them as “philosophers” – a bit cheesy, but hopefully it encourages active learning.
Secondly, I say something like this: “I’m sure you’ve heard the expression ‘everyone is entitled to their opinion.’ Perhaps you’ve even said it yourself, maybe to head off an argument or bring one to a close. Well, as soon as you walk into this room, it’s no longer true. You are not entitled to your opinion. You are only entitled to what you can argue for.”
A bit harsh? Perhaps, but philosophy teachers owe it to our students to teach them how to construct and defend an argument – and to recognize when a belief has become indefensible.
The problem with “I’m entitled to my opinion” is that, all too often, it’s used to shelter beliefs that should have been abandoned. It becomes shorthand for “I can say or think whatever I like” – and by extension, continuing to argue is somehow disrespectful. And this attitude feeds, I suggest, into the false equivalence between experts and non-experts that is an increasingly pernicious feature of our public discourse.
Firstly, what’s an opinion?
Plato distinguished between opinion or common belief (doxa) and certain knowledge, and that’s still a workable distinction today: unlike “1+1=2” or “there are no square circles,” an opinion has a degree of subjectivity and uncertainty to it. But “opinion” ranges from tastes or preferences, through views about questions that concern most people such as prudence or politics, to views grounded in technical expertise, such as legal or scientific opinions.
You can’t really argue about the first kind of opinion. I’d be silly to insist that you’re wrong to think strawberry ice cream is better than chocolate. The problem is that sometimes we implicitly seem to take opinions of the second and even the third sort to be unarguable in the way questions of taste are. Perhaps that’s one reason (no doubt there are others) why enthusiastic amateurs think they’re entitled to disagree with climate scientists and immunologists and have their views “respected.”
…
https://theconversation.com/no-youre-not-entitled-to-your-opinion-9978