understanding ECMAscript 6 ---- block bindings
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的更多相关文章
- [译]Understanding ECMAScript 6 说明
说明 JavaScript核心语言功能定义在ECMA-262中,此标准定义的语言是ECMAScript,浏览器中的JavaScript和Node.js环境是它的超级.当浏览器与Node.js想要通过额 ...
- [译]Understanding ECMAScript 6 内容目录
说明 浏览器与Node.js兼容 这本书是写给谁的 概述 帮助与支持 基本知识 更好的Unicode支持 其他字符串变化 其他正则表达式变化 Object.is() 块绑定 解构赋值 数字 总结 函数 ...
- Understanding ECMAScript 6 阅读问题小记
拖了一年说要看这本书,一直都没坚持下来,开个 bo 记录下觉得疑惑的问题,也算鞭策一下自己. 第一章 块级绑定 1. 第一章“块级绑定”下,说 const 变量如果绑定的是对象 Object,那么修改 ...
- My ECMAScript 7 wishlist
With ECMAScript 6 now feature complete, any further changes to the core of JavaScript will happen in ...
- 《理解 ES6》阅读整理:块绑定(Block Binding)
变量声明一直是JavaScript中一个需要技巧的部分.在大多数基于C的编程语言中,变量(更正式地说是绑定)在声明的时候创建,然而在JavaScript中并不是这样.在JavaScript中,变量在何 ...
- ECMAScript 6十大特性
ES6入门 http://es6.ruanyifeng.com/ ES6排名前十的最佳特性列表 Default Parameters(默认参数) in ES6 Template Literals (模 ...
- ESLint 规则
ESLint由 JavaScript 红宝书 作者 Nicholas C.Zakas 编写, 2013 年发布第一个版本. ESLint是一个以可扩展.每条规则独立的,被设计为完全可配置的lint工具 ...
- 【微信小程序项目实践总结】30分钟从陌生到熟悉 web app 、native app、hybrid app比较 30分钟ES6从陌生到熟悉 【原创】浅谈内存泄露 HTML5 五子棋 - JS/Canvas 游戏 meta 详解,html5 meta 标签日常设置 C#中回滚TransactionScope的使用方法和原理
[微信小程序项目实践总结]30分钟从陌生到熟悉 前言 我们之前对小程序做了基本学习: 1. 微信小程序开发07-列表页面怎么做 2. 微信小程序开发06-一个业务页面的完成 3. 微信小程序开发05- ...
- JS模块
本文主要内容翻译自<Exploring ES6-upgrade to the next version of javascript> 模块系统 定义模块接口,通过接口 隐藏模块的内部实现, ...
随机推荐
- C# 提供两种切割圆形图片的方式
效果: private void Form1_Load(object sender, EventArgs e) { string file =System.IO.Path.Combine(Enviro ...
- 建模分析之机器学习算法(附python&R代码)
0序 随着移动互联和大数据的拓展越发觉得算法以及模型在设计和开发中的重要性.不管是现在接触比较多的安全产品还是大互联网公司经常提到的人工智能产品(甚至人类2045的的智能拐点时代).都基于算法及建模来 ...
- jfinal 解决ajax 跨域访问--jsonp
JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.简单地理解就是因为JavaScript同源策略的限制,a.com 域名下的 js无法操作b.com或是c.a.com域名下的对象. ...
- hashmap的底层实现
HashMap的底层实现都是数组+链表结构实现的,添加.删除.获取元素都是先计算hash值,根据hash值和table.length计算出index也就是table的数组的下标,然后进行相应的操作. ...
- mybatis里的foreach语句
相信用了Mybatis的朋友们,都曾有一个疑惑,就是foreach是怎么用的,下面我就简单讲讲我的理解: foreach主要用在SQL语句中迭代一个集合.foreach元素的属性主要由item,ind ...
- Android 开发基础及环境配置
2011年买了第一部安卓操作系统的手机,当时势头正盛的HTC不可思议(incredible),当时的想法就是想学习下智能手机开发,但是由于各种原因,客观上是公司的项目太忙了,忙于项目管理.团队建设.客 ...
- Coursera台大机器学习课程笔记4 -- Training versus Testing
这节的主题感觉和training,testing关系不是很大,其根本线索在于铺垫并求解一个问题: 为什么算法PLA可以正确的work?因为前面的知识告诉我们,只有当假设的个数有限的时候,我们才 ...
- 二十一、Java基础--------IO流之综合案例分析
前三篇文章详细介绍了IO流体系所涉及的重点内容,为了帮助理解与学习,本片博客主要是分析一个与IO操作相关的题目. 例1:在我们观看视频时经常要关注的就是视频的时间长度,在学习了IO操作之后,就可以自己 ...
- oracle的IMU和ora-01555
IMU: 01555: 按照上图找啊找,已经提交事物的undo块找不到了,就产生01555错误,解决这样问题:1. 确保undo表空间数据的保留时间至少大于最长sql语句的时间 2. 增大undo表空 ...
- SOA和WCF&WebAPI
SOA http://www.cnblogs.com/leslies2/archive/2011/12/12/2272722.html WCF开发框架形成之旅--如何实现X509证书加密 WebAPI ...