[ES6] 05. The leg keyword -- 3. Block Scope

In ES6, IIFE is not necessary:

// IIFE写法
(function () {
    var tmp = ...;
    ...
}());

// 块级作用域写法
{
    let tmp = ...;
    ...
}

另外,ES6也规定,函数本身的作用域,在其所在的块级作用域之内

function f() { console.log(‘I am outside!‘); }
(function () {
  if(false) {
    // 重复声明一次函数f
    function f() { console.log(‘I am inside!‘); }
  }

  f();
}());

上面代码在ES5中运行,会得到“I am inside!”,但是在ES6中运行,会得到“I am outside!”

(function () {
    if(true) {
        // 重复声明一次函数f
        function f() { console.log(‘I am inside!‘); }
        f();
    }

}());
function f() { console.log(‘I am outside!‘); }

上面代码在ES6中运行,会得到“I am inside!”

时间: 2024-09-29 22:06:35

[ES6] 05. The leg keyword -- 3. Block Scope的相关文章

Closure - Mimicking block scope

The basic syntax of an anoymous function used as a block scope (often called a private scope) is as follows: (function(){ // block code here }) (); A variable is just a representation of another value, so the variable can be replaced with the actual

[ES6] 04. The let keyword -- 2 Fiald case

Fiald case 1: let can work in it's block { let a = 10; var b = 1; } a // ReferenceError: a is not defined. b //1 Case 2: Let has no "Hosting" Problem function do_something() { console.log(foo); // ReferenceError let foo = 2; } function do_someth

[ES6] 03. The let keyword -- 1

var message = "Hi"; { var message = "Bye"; } console.log(message); //Bye The message inside the block still has impact on the outside. If you add function around the inside message: var message = "Hi"; function greeting(){ va

解题.for; block scope; while; "2 to the " + i + " power is " + result

1 package com.java7; 2 3 public class WhileDemo { 4 public static void main(String[] args) { 5 int e; 6 int result; 7 for(int i = 0; i < 10; i++){ 8 result = 1; 9 e = i; 10 while(e > 0){ 11 result *= 2; 12 e--; 13 } 14 15 System.out.println("2

ES6 Syntax and Feature Overview(待续)

View on GitHub Note: A commonly accepted practice is to use const except in cases of loops and reassignment. However, in this resource I'll be using let in place of var for all ES6 examples. Variable: x Object: obj Array: arr Function: func Parameter

[ES6] 22. Const

'const' keyword is for creating a read only variable, something you can never change once created. 'const' likes 'let' keyword alos has block scope. describe("using const", function(){ it("will make a variable read-only", function(){ c

JS篇 ES6新特性

注意: 1. Node环境下,--harmony参数启用ES6新特性,许多新特性只有在strict mode下才生效,因此使用"use strict"或者--use_strict,否则harmony没有被启用: 1. 块级作用域(Block scope) // Block scope function f1(){ console.log("Outside."); } (function(){ if(false){ // f1定义在if {}之内 function f

ES6/ES2015常用知识点和概念

越来越多的开源库开始使用ES2015来构建代码了,大家知道ES6=ES2015,ES6在2015年被ECMAScript标准化组织approve,各大浏览器厂商要完全支持ES6的强大功能还须一些时日,对于喜爱新尝试的同学难道只有干等吗?幸运的是有了babel,traceur等transpiler的帮助,我们根本不用等待浏览器原生支持ES6才开始使用新技术了!其实babel做的事情很简单,他就是把es6的代码转换成浏览器认识的ES5代码.简单举一个例子,比如ES6中引入的语言原生支持模块的关键字i

JavaScript的Function Scope and Lexical Scope

原贴地址:http://pierrespring.com/2010/05/11/function-scope-and-lexical-scoping/ 个人觉得写得不错,简单地搬运,如有错误,请指正. Wikipedia defines Scope as follows: Tipically, scope is used to define the extent of information hiding--that is, the visibility or accessibility of