1、let命令

Tips:

  1. 块级作用域(只在当前块中有效)
  2. 不会变量提升(必须先申明在使用)
  3. 让变量独占该块,不再受外部影响
  4. 不允许重复声明

总之:let更像我们熟知的静态语言的的变量声明指令

ES6新增了let命令,用来声明变量。用法类似于var,但所声明的变量,只能在let命令所在的代码块内有效。

let声明的变量只有块级作用域

'use strict'
{
let a = 1;
}
console.log(a); //结果是什么?

看一段熟悉的代码:

var a = [];
for (var i = 0; i < 10; i++) {
a[i] = function () {
console.log(i);
};
}
console.log(a[6]()); //结果是什么?

如果改用let的话,那么看以下代码输出什么?

'use strict'
var a = [];
for (let i = 0; i < 10; i++) {
a[i] = function () {
console.log(i);
};
}
console.log(a[6]()); // ?

同时,在使用let的时候,必须先申明再使用,不像var会变量提升:

'use strict'
console.log(a);
let a = 1;

ES6中明确规定,如果区块存在let和const,那么该区块就形成封闭作用域,凡是在声明致歉就使用这些变量,就会报错。简称“暂时性死区”(temporal dead zone,简称TDZ)。

看一个不太容易发现的死区:(注:该代码未测试)

function bar(x=y, y=2) {
return [x, y];
} bar(); // 报错

调用bar之所以报错,是因为参数x默认值等于另一个参数y,而此时y还没有声明,属于“死区”。

需要注意,函数参数作用域和函数体的作用域是分离的:

let foo = 'outer';

function bar(func) {
let foo = 'inner';
console.log(func()); // outer
} bar(function(){
console.log(foo);
});

同时,let还不允许重复声明

{
let a = 1;
var a = 1;
}
{
let a = 1;
let a = 2;
}

2、const命令

Tips:

  1. const用于声明常量,一旦声明,值就不能改变
  2. const具有块级作用域
  3. const不能变量提升(先声明后使用)
  4. 不可重复声明

const看起来很像我们熟知的静态语言的只读对象

const声明常量,一旦声明,值将是不可变的。

'use strict'
const PI = 3.1415;
PI // 3.1415
PI = 3; //Error

const指令指向变量所在的地址,所以对该变量进行属性设置是可行的(未改变变量地址),如果想完全不可变化(包括属性),那么可以使用冻结。

'use strict'
const C1 = {};
C1.a = 1;
console.log(C1.a); // 1 //冻结对象,此时前面用不用const都是一个效果
const C2 = Object.freeze({});
C2.a = 1; //Error,对象不可扩展
console.log(C2.a);

3、全局对象属性

JavaScript中,全局对象是最顶层的对象,浏览器中是window对象,Node中是global对象,ES5规定,所有全局变量都是全局对象的属性。

在ES6中,var和function申明的变量,属于全局对象的属性,let和const则不是全局对象的属性。

'use strict'
let b = 2;
console.log(global.b); // undefined

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

ES6入门系列一(基础)的更多相关文章

  1. 快速入门系列--WebAPI--01基础

    ASP.NET MVC和WebAPI已经是.NET Web部分的主流,刚开始时两个公用同一个管道,之后为了更加的轻量化(WebAPI是对WCF Restful的轻量化),WebAPI使用了新的管道,因 ...

  2. [转]快速入门系列--WebAPI--01基础

    本文转自:http://www.cnblogs.com/wanliwang01/p/aspnet_webapi_base01.html ASP.NET MVC和WebAPI已经是.NET Web部分的 ...

  3. ES6入门系列三(特性总览下)

    0.导言 最近从coffee切换到js,代码量一下子变大了不少,也多了些许陌生感.为了在JS代码中,更合理的使用ES6的新特性,特在此对ES6的特性做一个简单的总览. 1.模块(Module) --C ...

  4. ES6入门系列 ----- Reflect

    Reflect   是ES6 为了操作对象而提供的新的API, 目的是: 将Object 上一些明显属于语言内部的方法,比如 Object.defineProperty  放到 Reflect对象上 ...

  5. ES6 入门系列 (一)ES6的前世今生

    要学好javascript , ECMAScript标准比什么都强, ESMAScript标准已经用最严谨的语言和最完美的角度展现了语言的实质和特性. 理解语言的本质后,你已经从沙堆里挑出了珍珠,能经 ...

  6. 快速入门系列--WCF--01基础概念

    转眼微软的WCF已走过十个年头,它是微软通信框架的集大成者,将之前微软所有的通信框架进行了整合,提供了统一的应用方式.记得从自己最开始做MFC时,就使用过Named Pipe命名管道,之后做Winfo ...

  7. 快速入门系列--TSQL-01基础概念

    作为一名程序员,对于SQL的使用算是基础中的基础,虽然也写了很多年的SQL,但常常还是记不清一些常见的命令,故而通过一篇博文巩固相关的记忆,并把T-SQL本身的一些新特性再进行一次学习. 首先回顾基础 ...

  8. ES6入门系列四(测试题分析)

    0.导言 ES6中新增了不少的新特性,来点测试题热热身.具体题目来源请看:http://perfectionkills.com/javascript-quiz-es6/. 以下将一题一题来解析what ...

  9. ES6 入门系列 - 函数的扩展

    1函数参数的默认值 基本用法 在ES6之前,不能直接为函数的参数指定默认值,只能采用变通的方法. function log(x, y) { y = y || 'World'; console.log( ...

随机推荐

  1. Java band [Cite]

    SampleModel  取样模型Databuffer 数据缓冲区 Raster 光栅Sample 样本band  带 SampleModel是java awt中的一个抽象类,它定义了一个接口,用于提 ...

  2. 解决stackoverflow打开慢不能注册登录

    http://blog.csdn.net/dream_an/article/details/50280977 解决stackoverflow打开慢不能注册登录 标签: stack overflowfi ...

  3. ACPI I/O resource conflict with SMBus

    ACPI I/O resource conflict with SMBus 以電子郵件傳送這篇文章BlogThis!分享至 Twitter分享至 Facebook分享到 Pinterest 這幾天遇到 ...

  4. Revit利用对正工具快速修改风管对齐方式

    绘制风管的时候,可以对风管的对正方式进行设置,水平方向可以设置左对齐或者中心对齐或者右对齐,垂直方向可以设置为顶对齐或者中心对齐或者低对齐,如果需要对原来的对齐方式进行修改的时候应该如何操作呢?比如, ...

  5. td 自动换行

    Two solutions for cell width:1. Omit words: <td style="width:60px;"><div style=&q ...

  6. ELK——Logstash 2.2 date 插件【翻译+实践】

    官网地址 本文内容 语法 测试数据 可配置选项 参考资料 date 插件是日期插件,这个插件,常用而重要. 如果不用 date 插件,那么 Logstash 将处理时间作为时间戳.时间戳字段是 Log ...

  7. [LeetCode] Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

  8. 动态生成Table内文字换行。

    后台动态生成table,并把td内的文字进行换行. 前台: <body style="width:100%;height:540px;margin-left:0px;margin-to ...

  9. Android开发(二十五)——Android上传文件至七牛

    设置头像: Drawable drawable = new BitmapDrawable(dBitmap); //Drawable drawable = Drawable.createFromPath ...

  10. javascript 的一些理解和随笔

    一.iframe里面的页面调用父窗口,左右窗口js函数的方法 iframe里面的页面调用父窗口,左右窗口js函数的方法 实现iframe内部页面直接调用该iframe所属父窗口自定义函数的方法. 比如 ...