Traditionally, the way variable declarations work has been one tricky part of programming in javascript. In most C-based languages, variables(more formally known as bindings, as a name is bound to a value inside a scope) are created at the spot where the declaration occurs. In JavaScript, however, this is not the case. Where your variables are actually created depends on how you declare them, and ECMAScript 6 offers options to make controlling scope easier. This chapter demonstrates why classic var declarations can be confusing, introduces block-level bindings in ECMAScript 6 and then offers some best practices for using them.

var Declarations and Hoisting 

  Variable declarations using var are treated as if they are at the top of the function(or in the global scope, if decalred outside of a function ) regardless of where the actual declartion occurs; this is called hoisting. For a demonstration of what hoisting does, consider the following function definition.

function getValue(condition){

	if(condition){
		var value = "blue";
		return value;
	}else {
		return null;
	}
}

    If you are unfamiliar with Javascript, you might expect the variable value to be created only if condition evaluates to true. In fact, the variable value is created regardless. Behind the scenes, the JavaScript engine changes the getValue function to look like this:

function getValue(condition){
  var value;   if(condition){
     value = "blue"; return value;   }else { return null;   } }

  

  The declaration of value is hoisted to the top and the initialization remains in the same spot. That means the variable  value is still accessilble from the else clause. If accessed from the else clause, the variable would just have a value of undefined because it has not been initialized in the else block.

  It often takes new JavaScript developers some time to get used to declaration hoistiong, and misunderstanding this unique behavior can end up causing bugs. For this reason, ECMAScript 6 introduces block-level scoping options to give developers more control overra variable's life cycle.

   在JS 编程中,变量声明起作用的方式一直是难处理的一部分。在大部分类C语言中,我们在哪里声明变量,变量就会在哪里创建。然而,在JS中,情况确不一样。

    

understanding ECMAscript 6 ---- block bindings的更多相关文章

  1. [译]Understanding ECMAScript 6 说明

    说明 JavaScript核心语言功能定义在ECMA-262中,此标准定义的语言是ECMAScript,浏览器中的JavaScript和Node.js环境是它的超级.当浏览器与Node.js想要通过额 ...

  2. [译]Understanding ECMAScript 6 内容目录

    说明 浏览器与Node.js兼容 这本书是写给谁的 概述 帮助与支持 基本知识 更好的Unicode支持 其他字符串变化 其他正则表达式变化 Object.is() 块绑定 解构赋值 数字 总结 函数 ...

  3. Understanding ECMAScript 6 阅读问题小记

    拖了一年说要看这本书,一直都没坚持下来,开个 bo 记录下觉得疑惑的问题,也算鞭策一下自己. 第一章 块级绑定 1. 第一章“块级绑定”下,说 const 变量如果绑定的是对象 Object,那么修改 ...

  4. My ECMAScript 7 wishlist

    With ECMAScript 6 now feature complete, any further changes to the core of JavaScript will happen in ...

  5. 《理解 ES6》阅读整理:块绑定(Block Binding)

    变量声明一直是JavaScript中一个需要技巧的部分.在大多数基于C的编程语言中,变量(更正式地说是绑定)在声明的时候创建,然而在JavaScript中并不是这样.在JavaScript中,变量在何 ...

  6. ECMAScript 6十大特性

    ES6入门 http://es6.ruanyifeng.com/ ES6排名前十的最佳特性列表 Default Parameters(默认参数) in ES6 Template Literals (模 ...

  7. ESLint 规则

    ESLint由 JavaScript 红宝书 作者 Nicholas C.Zakas 编写, 2013 年发布第一个版本. ESLint是一个以可扩展.每条规则独立的,被设计为完全可配置的lint工具 ...

  8. 【微信小程序项目实践总结】30分钟从陌生到熟悉 web app 、native app、hybrid app比较 30分钟ES6从陌生到熟悉 【原创】浅谈内存泄露 HTML5 五子棋 - JS/Canvas 游戏 meta 详解,html5 meta 标签日常设置 C#中回滚TransactionScope的使用方法和原理

    [微信小程序项目实践总结]30分钟从陌生到熟悉 前言 我们之前对小程序做了基本学习: 1. 微信小程序开发07-列表页面怎么做 2. 微信小程序开发06-一个业务页面的完成 3. 微信小程序开发05- ...

  9. JS模块

    本文主要内容翻译自<Exploring ES6-upgrade to the next version of javascript> 模块系统 定义模块接口,通过接口 隐藏模块的内部实现, ...

随机推荐

  1. IOS开发UISearchBar失去第一响应者身份后,取消按钮不执行点击事件的问题

    在iOS开发中,使用UISearchBar的时候,当搜索框失去焦点的时候,取消按钮是默认不能点击的,如图按钮的颜色是灰色的:  这是因为此时取消按钮的enabled属性被设置为NO了,那么当我们需要让 ...

  2. 把汉字转换为html实体编码

    背景:工作中需要把汉字转换为html实体编码实现方式:import org.apache.commons.lang.StringEscapeUtils;public static void main( ...

  3. WebForm 内置对象2

    Session: 与Cookies相比 相同点:每一台电脑访问服务器,都会是独立的一套session,key值都一样,但是内容都是不一样的 以上所有内容,都跟cookies一样 不同点: 1.Sess ...

  4. 基于SSM的分页

    现在基本每一个项目都有用到分页,SSM也是当前企业用到的比较频繁的框架,这里我就总结一下基于SSM的分页: 一.首先我们要准备一个分页的工具类 /** * 分页 */ public class Pag ...

  5. Mvc4中使用Spring.Net实现IOC和DI

    一个Hello类,一个IHello接口 1.引用dll 2.修改Global文件 (SpringMvcApplication) 3.控制器添加属性 4.WebConfig配置 5.Spring.xml ...

  6. 【实践】js实现随机不重复抽取数组中元素

    经过3个星期的时间终于用做完了学校的练习作品了,但是发现在用jq 做互动虽然很方便但却带来了不少的烦恼 所以在以后的日子里我要好好学 js 了! 然后呢在博主之前学java 里面 另我最头痛的就是做产 ...

  7. ASP DropDownList部分选项无法触发回传问题

    今天偶然碰到这个问题,一个通过后台绑定的DropDownList控件出现部分选项触发事件,部分选项不触发事件的问题: 原因是多个OPTION的Value值一致,导致ASP事件注册失败,只要在绑定过程中 ...

  8. 关于UIView的显示问题

    今天在倒腾PP助手SDK的接入,游戏框架使用的是cocos2d-x,因为不熟悉iOS的UIKit,所以费了点功夫终于让SDK的登录页面显示出来了,问题来了,在iOS设备landscape显示模式UI显 ...

  9. httpclient模拟post请求json封装表单数据

    好长时间不更博了,主要肚子里没什么好墨水,哈哈.废话不说上代码. public static String httpPostWithJSON(String url) throws Exception ...

  10. 同一界面放两个TTIWDBAdvWebGrid的问题(delphi IW TMS)

    同一个界面放了两个 TTIWDBAdvWebGrid 时,操作一个表,另一个的也跟着一起更改了,后查看网页生成的DIV,发现ID相同. 后查,有一个ID属性,更改后解决问题.