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 value, and the code works fine.

var someFunction = function(){

  // block code here

};

somefunction();

However, the following won‘t work:

function(){

  // block code here

}();        // error

The code causes a syntax error, because JavaScript sees the function keyword as the begining of a function declaration, and function declarations cannot be followed by parentheses. Function expressions, however, can be followed by parentheses. To turn the function declaration into a function expression, you need only surround it with the parentheses like this:

(function(){

  //block code here

})();

These private scopes can be used anywhere variables are needed temporarily, as in this example:

1 function outputNumbers(count){
2     (function(){
3         for (var i=0; i<count; i++){
4             alert(i);
5         }
6     }) ();
7
8     alert(i);           // causes an error
9 }
时间: 2024-10-15 23:44:59

Closure - Mimicking block scope的相关文章

[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() {

解题.for; block scope; while; &quot;2 to the &quot; + i + &quot; power is &quot; + 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

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 statemen

c++primer学习笔记(2)-Variables(names initialization scope declare)

Initializers: 1.被初始化的变量在create那一瞬间得到值:double price = 109.99, discount = price * 0.6; 这样的定义是合理的. Three ways of initialization: 1. int units = 0; 2. int units = {0}; 3. int units{0}; 4. int units(0); 其中第2和第3为list initialization, 该初始化有一个特点:就是该初始化不允许有可能导

IOS开发之---初识Block

正文 Block简介 我们可以把Block当做Objective-C的匿名函数.Block允许开发者在两个对象之间将任意的语句当做数据进行传递,往往这要比引用定义在别处的函数直观.另外,block的实现具有封闭性(closure),而又能够很容易获取上下文的相关状态信息. Block的创建 实际上,block使用了与函数相同的机制:可以像声明函数一样,来声明一个bock变量:可以利用定义一个函数的方法来定义一个block:也可以将block当做一个函数来调用. 1 // main.m 2 #im

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

You Don&#39;t Know JS: Scope &amp; Closures (第3章: 函数 vs 块作用域)

第二章,作用域由一系列的bubbles组成.每一个都代表了一个container或bucket,装着被声明的identifiers(variables, functions).这些bubbles相互嵌套.这种嵌套是在开发阶段写完的. 什么制造了一个新的bubble? 只是函数吗?其他的JS结构可以创建bubbles of scope吗? Function Vs. Block Scope Scope From Functions 探索函数作用域和它的暗示implications. 函数作用域内的所

You Don&#39;t Know JS: Scope &amp; Closures (第4章: Hoisting)

Chapter4: Hoisting 变量附加到哪个层次的scope,由它们在哪里和如何声明(let, var)来决定. Function scope/Block scope都有相同的法则:任何变量在一个scope内声明,则这个变量附加到这个作用域上. 但有一个细节问题:当声明declarations出现在一个作用域中的不同的位置的时候,scope附加如何与declarations协作? Chicken or The Egg? temptation: a strong desire to hav

coffeescript 1.8.0 documents

CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way. The golden rule