Up & Going -1 (Into Programming)

从今天开始读《You Don't Know JS》系列,下面是一些读后总结,为保持原滋原味,不翻译了。以后本系列也应该会延续本文风格。

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

你可能感兴趣的:(Up & Going -1 (Into Programming))