javascript嵌入式
If expressions are single units of JavaScript that the engine can evaluate, statements can contain one or more different expressions, and are executed by the engine to perform an operation.
如果表达式是引擎可以评估JavaScript的单个单元,则语句可以包含一个或多个不同的表达式,并由引擎执行以执行操作。
Programs are composed by multiple statements. Statements can span over multiple lines.
程序由多个语句组成。 语句可以跨越多行。
Just like with expressions, JavaScript has a whole different set of statements:
就像表达式一样,JavaScript具有一组完全不同的语句:
Let’s dive into the details.
让我们深入研究细节。
Statements can end with an optional semicolon ;
. Using it, you can have multiple statements on a single line. I normally don’t use semicolons, but you can use it whenever a statement ends.
语句可以以可选的分号结尾;
。 使用它,您可以在一行上包含多个语句。 我通常不使用分号,但是您可以在语句结束时使用它。
An expression on its own is also a statement:
表达式本身也是一个语句:
2
0.02
'something'
true
false
this //the current scope
undefined
i //where i is a variable or a constant
1 / 2
i++
i -= 2
i * 2
'A ' + 'string'
[] //array literal
{} //object literal
[1,2,3]
{a: 1, b: 2}
{a: {b: 1}}
a && b
a || b
!a
object.property //reference a property (or method) of an object
object[property]
object['property']
new object()
new a(1)
new MyRectangle('name', 2, {a: 4})
function() {}
function(a, b) { return a * b }
(a, b) => a * b
a => a * 2
() => { return 2 }
a.x(2)
window.resize()
With a declaration statement you assign a value to a variable name.
使用声明语句,您可以为变量名分配一个值。
Examples:
例子:
var i = 0
let j = 1
const k = 2
//declare an object value
const car = {
color: blue
}
Here are function declarations:
这是函数声明:
//declare a function
function fetchFromNetwork() {
//...
}
//or
const fetchFromNetwork = () => {
//...
}
Statements can be grouped, using a block:
可以使用一个语句对语句进行分组:
{
//this is a block
const a = 1;
const b = 2;
}
Using this syntax, you can have multiple statements whenever JavaScript expects a single statement.
使用此语法,只要JavaScript需要一个语句,就可以有多个语句。
Be aware that any of the conditional control flow statements check an expression and depending on it they execute a statement, or a block:
请注意,任何条件控制流语句都会检查表达式,并根据表达式执行一条语句或一个块:
if (condition === true) {
//execute this block
} else {
//execute this block
}
You can omit curly braces if you only have one statement:
如果只有一条语句,则可以省略花括号:
if (condition === true) /* statement */ else /* another statement */
I’ll go into all the different control flow structures in the next sections.
在下一节中,我将介绍所有不同的控制流结构。
Loops work similarly to the if
example above.
循环的工作方式与上面的if
示例相似。
Some loops check an expression, and repeat a statement execution it until that expression evaluates to true.
一些循环会检查一个表达式,并重复执行一条语句,直到该表达式的值为真。
Some other loops iterate over a list and execute a statement (or block) for each element of the list, until the list finishes.
其他一些循环遍历列表,并为列表的每个元素执行一条语句(或块),直到列表完成。
See my full JavaScript loops tutorial.
请参阅我的完整JavaScript循环教程 。
return
(return
)This statement returns a value from a function, ending the function execution.
该语句从函数返回值,从而结束函数执行。
throw
(throw
)Throws an exception (we’ll later see what is an exception)
引发异常(我们将在后面看到什么是异常)
try
catch
(try
and catch
)A try/catch block is used to catch exceptions. Again, we’ll see those applied later on.
try / catch块用于捕获异常。 再次,我们将在稍后看到应用的那些。
try {
} catch () {
}
use strict
(use strict
)This statement applies strict mode.
该语句适用严格模式 。
debugger
(debugger
)Adds a breakpoint which the debugger can use.
添加调试器可以使用的断点。
翻译自: https://flaviocopes.com/javascript-statements/
javascript嵌入式