Up & Going -1 (Into Programming)

從今天開始讀《You Don't Know JS》系列些阅,下面是一些讀后總結(jié)拗胜,為保持原滋原味歧匈,不翻譯了。以后本系列也應(yīng)該會延續(xù)本文風(fēng)格王带。

Code

A program, often referred to as source code or just code, is a set of special instructions to tell the computer what tasks to perform.

Statements

In a computer language, a group of words, numbers, and operators that performs a specific task is a statement. In JavaScript, a statement might look as follows:

a = b * 2;

Expressions

Statements are made up of one or more expressions. An expression is any reference to a variable or value, or a set of variable(s) and value(s) combined with operators.

For example:

a = b * 2;

This statement has four expressions in it:

  • 2 is a literal value expression
  • bis a variable expression, which means to retrieve its current value
  • b * 2 is an arithmetic expression, which means to do the multiplication
  • a = b * 2 is an assignment expression, which means to assign the result of theb * 2expression to the variable a.
    what is expression statemen and call expression statement ?
b * 2;
alert( a );

Executing a Program

Statements like a = b * 2 are helpful for developers when reading and writing, but are not actually in a form the computer can directly understand. So a special utility on the computer (either an interpreter or a compiler) is used to translate the code you write into commands a computer can understand.

interpreter

For some computer languages, this translation of commands is typically done from top to bottom, line by line, every time the program is run, which is usually called * interpreting* the code.

compiler

For other languages, the translation is done ahead of time, called compiling the code, so when the program runs later, what's running is actually the already compiled computer instructions ready to go.

It's typically asserted that JavaScript is interpreted, because your JavaScript source code is processed each time it's run. But that's not entirely accurate. ** The JavaScript engine actually compiles the program on the fly and then immediately runs the compiled code. **

Operators

Operators are how we perform actions on variables and values.
Here are some of the most common operators in JavaScript:

  • Assignment: = as in a = 2.
  • Math: + (addition), - (subtraction), * (multiplication), and / (division), as in a * 3.
  • Compound Assignment: +=, -=, *=, and /= are compound operators that combine a math operation with assignment, as in a += 2 (same asa = a + 2).
  • Increment/Decrement: ++ (increment),--(decrement);
  • Object Property Access: .as in console.log().
  • Equality: == (loose-equals), === (strict-equals), != (loose not-equals),!== (strict not-equals)
  • Comparison: < (less than), >(greater than), <=(less than or loose-equals), >=(greater than or loose-equals)
  • Logical:&& (and), ||(or), as in a || b that selects eitheraor b.

Values & Types

types

These different representations for values are called types in programming terminology. JavaScript has built-in types for each of these so called primitive values:
number,string,boolean(true or false).

Converting Between Types
coercion

If you have a number but need to print it on the screen, you need to convert the value to a string, and in JavaScript this conversion is called "coercion." (explicit coercion ** and ** implicit coercion)

Variables

Static typing

In some programming languages, you declare a variable (container) to hold a specific type of value, such as number or string. Static typing, otherwise known as type enforcement, is typically cited as a benefit for program correctness by preventing unintended value conversions.

Weak typing

Other languages emphasize types for values instead of variables. Weak typing, otherwise known as dynamic typing, allows a variable to hold any type of value at any time. It's typically cited as a benefit for program flexibility by allowing a single variable to represent a value no matter what type form that value may take at any given moment in the program's logic flow.
JavaScript uses the latter approach, dynamic typing.

Blocks

In code we often need to group a series of statements together, which we often call a block. In JavaScript, a block is defined by wrapping one or more statements inside a curly-brace pair { .. }.

Conditionals

There are quite a few ways we can express conditionals (aka decisions) in our programs.The most common one is the if statement.

Loops

A loop includes the test condition as well as a block (typically as { .. }). Each time the loop block executes, that's called an iteration. the while loop and the do..while loop forms illustrate the concept of repeating a block of statements until a condition no longer evaluates to true.

Functions

your program will almost certainly want to break up the code's tasks into reusable pieces, instead of repeatedly repeating yourself repetitiously (pun intended!). The way to do this is to define a function.

Scope

Programming has a term for this concept: *scope *(technically called lexical scope). In JavaScript, each function gets its own scope. Scope is basically a collection of variables as well as the rules for how those variables are accessed by name. Only code inside that function can access that function's scoped variables.

Review

  • You need operators to perform actions on values.
  • You need values and types to perform different kinds of actions like math on numbers or output with strings.
  • You need variables to store data (aka state) during your program's execution.
  • You need conditionals like if statements to make decisions.
  • You need loops to repeat tasks until a condition stops being true.
  • You need functions to organize your code into logical and reusable chunks.

references

Mastering The Developer Tools Console
You Don't Know JS: Up & Going
Expressions and Operators

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末淑蔚,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子愕撰,更是在濱河造成了極大的恐慌刹衫,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件搞挣,死亡現(xiàn)場離奇詭異带迟,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)囱桨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進(jìn)店門仓犬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人舍肠,你說我怎么就攤上這事搀继。” “怎么了翠语?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵叽躯,是天一觀的道長。 經(jīng)常有香客問我啡专,道長险毁,這世上最難降的妖魔是什么制圈? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任们童,我火速辦了婚禮,結(jié)果婚禮上鲸鹦,老公的妹妹穿的比我還像新娘慧库。我一直安慰自己,他們只是感情好馋嗜,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布齐板。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪甘磨。 梳的紋絲不亂的頭發(fā)上橡羞,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天,我揣著相機(jī)與錄音济舆,去河邊找鬼卿泽。 笑死,一個胖子當(dāng)著我的面吹牛滋觉,可吹牛的內(nèi)容都是我干的签夭。 我是一名探鬼主播,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼椎侠,長吁一口氣:“原來是場噩夢啊……” “哼第租!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起我纪,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤慎宾,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后浅悉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體璧诵,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年仇冯,在試婚紗的時候發(fā)現(xiàn)自己被綠了之宿。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡苛坚,死狀恐怖比被,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情泼舱,我是刑警寧澤等缀,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站娇昙,受9級特大地震影響尺迂,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜冒掌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一噪裕、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧股毫,春花似錦膳音、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽苍凛。三九已至,卻和暖如春兵志,著一層夾襖步出監(jiān)牢的瞬間醇蝴,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工想罕, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留哑蔫,地道東北人。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓弧呐,卻偏偏與公主長得像闸迷,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子俘枫,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容