35.ES6语法介绍——2019年12月24日
2019年12月24日16:22:24
2019年10月09日12:04:44
1. ES6介绍
1.1 新的 Javascript 语法标准
——2015年6月正式发布
——使用babel语法转换器,支持低端浏览器
——流行的库基本都基于ES6构建,React默认使用ES6新语法开发
1.2 ES6包含内容
块级作用域、字符串、函数
对象扩展、解构
类、模块化等
2. 作用域
2.1 let
let 声明的变量只在 let 命令所在的代码块内有效。
定义变量使用let替代var
{
let a = 0;
a // 0
}
a // 报错 ReferenceError: a is not defined
{
let a = 0;
var b = 1;
}
a // ReferenceError: a is not defined
b // 1
2.1.1 let与var的区别
let 只能声明一次 var 可以声明多次:
let a = 1;
let a = 2;
var b = 3;
var b = 4;
a // Identifier 'a' has already been declared
b // 4
let 不存在变量提升,var 会变量提升:
console.log(a); //ReferenceError: a is not defined
let a = "apple";
console.log(b); //undefined
var b = "banana";
变量 b 用 var 声明存在变量提升,所以当脚本开始运行的时候,b 已经存在了,但是还没有赋值,所以会输出 undefined。
变量 a 用 let 声明不存在变量提升,在声明变量 a 之前,a 不存在,所以会报错。
2.2 const
Const 定义不可修改的变量
const PI = "3.1415926";
PI // 3.1415926
const MY_AGE; // SyntaxError: Missing initializer in const declaration
2.3 注意要点
const 如何做到变量在声明初始化之后不允许改变的?
其实 const 其实保证的不是变量的值不变,而是保证变量指向的内存地址所保存的数据不允许改动。
此时,你可能已经想到,简单类型和复合类型保存值的方式是不同的。
是的,对于简单类型(数值 number、字符串 string 、布尔值 boolean),值就保存在变量指向的那个内存地址,因此 const 声明的简单类型变量等同于常量。
而复杂类型(对象 object,数组 array,函数 function),变量指向的内存地址其实是保存了一个指向实际数据的指针,所以 const 只能保证指针是固定的,至于指针指向的数据结构变不变就无法控制了,所以使用 const 声明复杂类型对象时要慎重。
3. 字符串
3.1 使用反引号,直接写变量
let str1="xiaoye"
let str2="12"
console.log(`hello my name is ${str1},and my age is ${str2}`)
//hello my name is xiaoye,and my age is 12
3.2 多行字符串
console.log(`
hello
world
12
`)
//hello
//world
//12
4. 函数扩展
参数默认值
箭头函数
展开运算符
4.1 一般形式
const hello1=()=>{
console.log("hello world")
}
hello1()
4.2 return语句省略
const hell2=x=>x*2
console.log(hell2(12))
4.3 传参,有默认值
const hello3=(name="xioaye")=>{
console.log(`hello my name is ${name}`)
}
hello3();
hello3("xiaowang1");
//hello my name is xioaye
//hello my name is xiaowang1
4.4 展开符...
function hello4(name1,name2){
console.log(name1,name2)
}
let arr=['hello','world']
hello4(...arr)
5. 对象扩展
Object.keys、values、entries
对象方法简写,计算属性
开运算符(不是ES6标准,但是babel也支持)
5.1 Object.keys(),Object.values(),entries()
let obj={
"name":"xiaoye",
"age":"12"
}
console.log(obj)
console.log(Object.keys(obj))
console.log(Object.values(obj))
//{ name: 'xiaoye', age: '12' }
//[ 'name', 'age' ]
//[ 'xiaoye', '12' ]
5.2 字段放进对象key中【name】
let name="imooc"
let obj={
[name]:"hello imooc"
}
console.log(obj)
//{ imooc: 'hello imooc' }
6.解构赋值
6.1 数组解构
let arr=['hello','world']
let [arr1,arr2]=arr
console.log(arr1,arr2)
//hello world
6.2 对象解构
const obj={"name":'xiaoye','age':"12"}
const {name,age}=obj
console.log(name,age)
//xiaoye 12
7.类
class MyApp{
constructor(){
this.name='imooc'
}
sayHello(){
console.log(`hello,${this.name}`)
}
}
const app=new MyApp()
app.sayHello()
8.新的数据结构
8.1 Set,元素不可重复
8.2 Map
8.3 Symbol
9.模块化
export
import
10.常见代码片段
10.1 遍历数组 map
let arr=[1,2,3]
console.log(arr.map(function(v){
console.log(v*2)
}))
console.log(arr.map(v=>v*2))
2
4
6
[ undefined, undefined, undefined ]
[ 2, 4, 6 ]
10.2 数组去重
let arr=[1,2,3,3]
console.log([...new Set(arr)])
//[ 1, 2, 3 ]
10.3 连接数组
let arr=[1,2,3,3]
let arr1=[5,6]
console.log([...arr,...arr1])
//[ 1, 2, 3, 3, 5, 6 ]
11.箭头函数
参数 => 函数体
var f = v => v;
//等价于
var f = function(a){
return a;
}
f(1); //1
12.如何直接运行es6语法的js文件
查了好多教程,都是清一色的要装什么babel,我想说的是,在nodejs8以上已经原生支持es6语法书写代码了。
需要做如下两步:
- 把原来的
*.js改为*.mjs
start-point.js
↓
start-point.mjs
- 需要增加node运行参数
--experimental-modules
//举个栗子
node --experimental-modules start-point.mjs
35.ES6语法介绍——2019年12月24日的更多相关文章
- 36.React基础介绍——2019年12月24日
2019年12月24日16:47:12 2019年10月25日11:24:29 主要介绍react入门知识. 1.jsx语法介绍 1.1 介绍 jsx语法是一种类似于html标签的语法,它的作用相当于 ...
- 34.ITerm配置使用——2019年12月24日
2019年12月23日15:17:36 ITerm使用教程 1.快键键配置 设置方法 设置方法如下: (1)选择 Preference 进入偏好设置: (2)选择 Profiles > Keys ...
- 16.go语言基础学习(上)——2019年12月16日
2019年12月13日10:35:20 1.介绍 2019年10月31日15:09:03 2.基本语法 2.1 定义变量 2019年10月31日16:12:34 1.函数外必须使用var定义变量 va ...
- 22.Express框架——2019年12月19日
2019年12月19日14:16:36 1. express简介 1.1 介绍 Express框架是后台的Node框架,所以和jQuery.zepto.yui.bootstrap都不一个东西. Exp ...
- 19.go语言基础学习(下)——2019年12月16日
2019年12月16日16:57:04 5.接口 2019年11月01日15:56:09 5.1 duck typing 1. 2. 接口 3.介绍 Go 语言的接口设计是非侵入式的,接口编写者无须知 ...
- 日常Git使用——2019年12月11日16:19:03
1.git介绍 1.1 什么是git? 什么是Git? 比如一个项目,两个人同时参与开发,那么就把这个项目放在一个公共的地方,需要的时候都可以去获取,有什么改动,都可以进行提交. 为了做到这一点,就需 ...
- 24.mongodb可视化工具部署——2019年12月19日
2019年10月09日17:05:54 教程链接:https://blog.csdn.net/qq_32340877/article/details/79142129 项目名:adminMongo g ...
- 20.Nodejs基础知识(上)——2019年12月16日
2019年12月16日18:58:55 2019年10月04日12:20:59 1. nodejs简介 Node.js是一个让JavaScript运行在服务器端的开发平台,它让JavaScript的触 ...
- 根据随身固态U盘卷标搜索U盘盘符并打开文件的批处理脚本.bat 徐晓亮 595076941@qq.com 2019年12月19日6点50分
@Echo offRem 根据随身固态U盘卷标搜索U盘盘符并打开文件的批处理脚本.batRem 徐晓亮 595076941@qq.com 2019年12月19日6点50分 Rem 此批处理脚本源代码的 ...
随机推荐
- Microsoft:Team Foundation Server 20XX Release Notes
ylbtech-Microsoft:Team Foundation Server 2017 Release Notes 1.返回顶部 1. https://docs.microsoft.com/en- ...
- Delphi XE2 之 FireMonkey 入门(43) - 控件基础: TStringGrid、TGrid
Delphi XE2 之 FireMonkey 入门(43) - 控件基础: TStringGrid.TGrid TStringGrid.TGrid 都是从 TCustomGrid 继承; 区别有:1 ...
- Eclipse与Tomcat
新进一个Web项目,有些小坎坷,于是引发了对于Eclipse和Tomcat的配置关系的思考. 首先提及一点当年的观点:Tomcat是一个容器,所有的功能都是以插件的形式放入其中:比如tomcat就是o ...
- Selenium学习之==>常见面试题
转自:http://www.imdsx.cn/ 一.selenium中如何判断元素是否存在? expected_conditions模块提供了多种校验方式,我常用的一种是presence_of_ele ...
- hadoop报错WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
19/06/14 10:44:58 WARN common.Util: Path /opt/hadoopdata/hdfs/name should be specified as a URI in c ...
- WEB框架实战总结
Django 在新一代的 Web框架 中非常出色 使用Python开发Web,最简单,原始和直接的办法是使用CGI标准,可以用WSGI接口 一.WSGI接口实现web页面 运行WSGI服务 我们先编写 ...
- 什么是Shell?Shell脚本基础知识详细介绍
这篇文章主要介绍了什么是Shell?Shell脚本基础知识介绍,本文是一篇Shell脚本入门文章,在本文你可学到什么是Shell.有多少种Shell.一个Shell脚本代码实例,需要的朋友可以参考下 ...
- 【Python】我的第一个完整的小说爬虫
写在开头 纪念我的第一个爬虫程序,一共写了三个白天,其中有两个上午没有看,中途遇到了各种奇怪的问题,伴随着他们的解决,对于一些基本的操作也弄清楚了.果然,对于这些东西的最号的学习方式,就是在使用中学习 ...
- 深入理解java:2.3.2. 并发编程concurrent包 之重入锁/读写锁/条件锁
重入锁 Java中的重入锁(即ReentrantLock) 与JVM内置锁(即synchronized)一样,是一种排它锁. ReentrantLock提供了多样化的同步,比如有时间限制的同步(定 ...
- win10安装mysql时报错[MY-012576] [InnoDB] Unable to create temporary file; errno: 2
报错信息 解决: 在my.ini文件里面的 [mysqld]区段内加入: #自己指定的临时文件目录 tmpdir="临时目录" 添加好后初始化成功 接下来启动mysql服务的时候报 ...