Git Product home page Git Product logo

ecma-js6's Introduction

ECMA-JS6入门

关于ECMA-JS6的初学笔记


##目录

关于let你应该知道的:

  • let 命令只在其所在的代码块内有效
  • let 命令特别适合循环语句( for / while / do...while )
  • 不存在变量提升
  • 暂时性死区 (在代码块内,使用let命令声明变量之前,该变量都是不可用的)
  • 不允许重复声明

关于const命令你应该知道的

  • const声明一个只读的常量,一旦声明,则该常量的值就不可更改.
  • 一旦声明,必须赋值.
  • 其他性质和let一致.
    • 只在其所在的代码块内有效
    • 不存在变量提升
    • 暂时性死区
    • 不允许重复声明

(1) 解构赋值 : ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值.这被称为解构赋值.

(2) 只要等号两边的模式相同,左边的变量就会被赋予对应的值

所谓模式相同:指的是等号左边的数据类型 和 等号右边的数据类型相同.

1. 数组的解构赋值

ES5写法:
	let a = 1;
	let b = 2;
	let c = 3;
	
ES6写法:
	let[a,b,c] = [1,2,3];

1.1 解构赋值

```js
	let[a,b,c] = [1,2,3];
	let [ , , third] = ["foo", "bar", "baz"];
	let [x,y,z] = [1,2];
	// x -> 1
	// y -> 2
	// z -> undefined
	*如果解构不成功,变量的值就等于undefined.
``` 

2. 对象的解构赋值

解构不仅可以用于数组,还可以用于对象。

对象的解构与数组有一个重要的不同。数组的元素是按次序排列的,变量的取值由它的位置决定;而对象的属性没有次序,变量必须与属性同名,才能取到正确的值。

	let { bar, foo } = { foo: "aaa", bar: "bbb" };
	foo // "aaa"
	bar // "bbb"

	let { baz } = { foo: "aaa", bar: "bbb" };
	baz // undefined

3. 字符串的解构赋值

字符串也可以解构赋值。这是因为此时,字符串被转换成了一个类似数组的对象。

	const [a, b, c, d, e] = 'hello';
	a // "h"
	b // "e"
	c // "l"
	d // "l"
	e // "o"

ecma-js6's People

Contributors

jiml007 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.