Block statement

The most basic statement is a block statement that is used to group statements. The block is delimited by a pair of curly brackets:

{
  statement_1;
  statement_2;
  .
  .
  .
  statement_n;
}

  

Example

Block statements are commonly used with control flow statements (e.g. ifforwhile).

while (x < 10) {
  x++;
}

  

Here, { x++; } is the block statement.

Important: JavaScript does not have block scope prior to ECMAScript 6. Variables introduced within a block are scoped to the containing function or script, and the effects of setting them persist beyond the block itself. In other words, block statements do not introduce a scope. Although "standalone" blocks are valid syntax, you do not want to use standalone blocks in JavaScript, because they don‘t do what you think they do, if you think they do anything like such blocks in C or Java. For example:

var x = 1;
{
  var x = 2;
}
console.log(x); // outputs 2

  

This outputs 2 because the var x statement within the block is in the same scope as thevar x statement before the block. In C or Java, the equivalent code would have outputted 1.

Starting with ECMAScript 6, the let variable declaration is block scoped. See the letreference page for more information.

时间: 2025-01-16 02:11:58

Block statement的相关文章

Javascript的坑(一)---------- block statement scope

在ECMAScript 6之前,Javascript是没有block statement scope的..... 这就导致了诡异的现象,比如下面的代码 var x = 1; { var y = 2; } console.log(y); // outputs 2 简直神奇..... 现在有了ECMAScript 6,代码就可以这样写 var x = 1; { let y = 2; } console.log(y); // ReferenceError: y is not defined 嗯,这样就

javascript block

概览 语句块 (或其他语言中的 复合语句) 用来组织零个或多条语句. 用一对花括号界定语句块. 语法 { statement_1; statement_2; ... statement_n; } statement_1, statement_2, statement_n 语句都包裹在语句块中. 说明 语句块通常在流程控制语句 (如 if, for, while)中使用.如: while (x < 10) { x++; } 注意语句块不是以分号结尾. 其他语言中通常将语句块称为 复合语句. 语句块

使用Antlr实现简单的DSL

为什么要使用DSL DSL是领域专用语言,常见的DSL有SQL,CSS,Shell等等,这些DSL语言有别于其他通用语言如:C++,Java,C#,DSL常在特殊的场景或领域中使用.如下图: 领域专用语言通常是被领域专家使用,领域专家一般不熟悉通用编程语言,但是他们一般对业务非常了解,程序员一般对通用语言比较熟悉,但是在做行业软件的时候对业务部了解.这就需要协作的过程,一种方式是领域专家通过文档或者教授的方式把业务逻辑传递给程序员让程序员翻译成业务逻辑,而另一种方法,程序员为领域专家定制DSL,

通过OCCI连接oracle(C++)

OCCI介绍 OCCI:Oracle C++调用接口(OCCI),即Oracle的C++API,允许你使用面向对象的特性.本地类.C++语言的方法来访问Oracle数据库. OCCI优势 基于标准C++和面向对象的设计: 效率较高: 适合开发C/S模式的程序,软件中间层: OCCI特性 完整支持SQL/PLSQL 为不断增长的用户和请求提供了弹性选项 为使用用户自定义类型,如C中的类,提供了无缝接口 支持所有的Oracle数据类型以及LOB types 可以访问数据库元数据 OCCI头文件及动态

Java中条件语句和if-else的嵌套原则

if(condition)Statement 在此时的条件语句中的条件是需要用括号把它括起来.   其实,Java中的条件语句和C/C++中的是一样的.而Java常常希望在某个条件为真的时候执行多条语句.此时,我们就会引入一个概念,那就是"块模块(block statement)",具体格式如下,仅供参考: { statement1 statement2 ... } 就拿下面的例子,我们来试试上面的这个格式吧! if(score>=90) system.out.println(&

Variable scope

When you declare a variable outside of any function, it is called a global variable, because it is available to any other code in the current document. When you declare a variable within a function, it is called a local variable, because it is availa

7、控制流程

7.1 块作用域 块:指由一对花括号括起来的若干简单的Java语句. 块确定了变量的作用域. 块分为:静态的和非静态的 执行的顺序为: 一个块可以嵌套在另一个块中.但是不能在嵌套的两个块中声明同名的变量.在 示例: 步骤1:在Demo010项目中的com.zjk.type包内创建Block类 源码: package com.zjk.type; /** * *@类名 Block *@日期 2015年12月5日下午1:42:11 *@作者 zjkorder *@版本 v1.0 *@描述???? *

Web 前端代码规范

Web 前端代码规范 最后更新时间:2017-06-25 原始文章链接:https://github.com/bxm0927/web-code-standards 此项目用于记录规范的.高可维护性的前端代码,这是通过分析 Github 众多前端代码库,总结出来的前端代码书写规范. 目录 前端普适性规范 HTML 规范 CSS 规范 JS 规范 License public domain, Just take it. Thanks @Ruan YiFeng: https://github.com/

[Javascript AST] 2. Write a simple ESLint rule

What we want to do is checking if user write nested if statements which actually can combine to one: // BAD if (a) { console.log("a"); } else { if (b) { console.log("b"); } } // GOOD if (a) { console.log("a"); } else if (b) {