├── .gitignore ├── 10react.html ├── 11.js ├── 13.js ├── 14.js ├── 16.js ├── 3.html ├── 3.js ├── 4.js ├── 5.js ├── 6.js ├── 7.js ├── 8.js ├── 9.js ├── LICENSE ├── README.md └── tempCodeRunnerFile.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | -------------------------------------------------------------------------------- /10react.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 22 | 23 | 24 | Document 25 | 26 | 27 | 28 |
29 | 30 |
31 |

hi

32 | 33 | 34 | -------------------------------------------------------------------------------- /11.js: -------------------------------------------------------------------------------- 1 | var json = {a: 12, b: 5} 2 | var str = 'hi,' + JSON.stringify(json) 3 | var url = 'http://www.xx.com/' + encodeURIComponent(JSON.stringify(json)) 4 | console.log(str) 5 | console.log(url) 6 | 7 | var str = '{"a": 12, "b": 4, "c": "abc"}' 8 | var json = JSON.parse(str) 9 | console.log(json) 10 | 11 | var a = 12, b = 5 12 | console.log({a:a, b:b}) 13 | console.log({a, b}) 14 | console.log({a, b, c:"c"}) 15 | console.log({ a, b, show(){ console.log('a')}}) -------------------------------------------------------------------------------- /13.js: -------------------------------------------------------------------------------- 1 | function show() { 2 | console.log('a') 3 | console.log('b') 4 | } 5 | show() // 普通函数 6 | 7 | function *show2() { 8 | console.log('1') 9 | yield 10 | console.log('2') 11 | } 12 | let genObj = show2() 13 | genObj.next() 14 | genObj.next() 15 | genObj.next() // 最后了,没有结果 16 | 17 | -------------------------------------------------------------------------------- /14.js: -------------------------------------------------------------------------------- 1 | 2 | function * show() { 3 | console.log('1') 4 | var a = yield 5 | console.log('2') 6 | console.log(a) 7 | } 8 | // yield 传参 9 | var gen = show() 10 | gen.next() // 1 11 | gen.next() // 2 和 undefined 因为没有传参,yield没有返回值 12 | var gen = show() 13 | gen.next(10) // 1 第一次执行到yield,但没有执行赋值 14 | gen.next(20) // 2 和 20 15 | 16 | function* show2() { 17 | console.log('1') 18 | yield 10 19 | console.log('2') 20 | } 21 | // yield 返回 22 | var gen = show2() 23 | var res1 = gen.next() 24 | console.log(res1) // { value: 10, done: false } 25 | var res2 = gen.next() 26 | console.log(res2) 27 | // { value: undefined, done: true } 最后的value需要return返回 -------------------------------------------------------------------------------- /16.js: -------------------------------------------------------------------------------- 1 | let arr = ['a', 'b', 'c'] 2 | console.log(arr.includes(1)) 3 | 4 | for (let i in arr) { 5 | console.log(i) // 循环的时下标 key 6 | } 7 | 8 | for (let i of arr) { 9 | console.log(i) // 循环的是值 value 10 | } 11 | for (let i of arr.keys()) { 12 | console.log('>'+i) // 循环的是值 value 13 | } 14 | for (let [key, value] of arr.entries()) { 15 | console.log('>' + key + value) // 循环的是值 value 16 | } 17 | 18 | let json = { a: 12, b: 5, c: 7 } 19 | for (let i in json) { 20 | console.log(i) 21 | } 22 | 23 | // for (let i of json) { 24 | // // console.log(i) // 不能用于 json is not iterable 25 | // } 26 | 27 | console.log('=' + 'abcd'.padStart(6, '0') + '=') 28 | console.log('=' + 'abcd'.padEnd(6, '0') + '=') 29 | -------------------------------------------------------------------------------- /3.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /3.js: -------------------------------------------------------------------------------- 1 | var a = 1 2 | var a = 2 3 | console.log(a) 4 | 5 | // var 没有块级作用域 6 | if (true) { 7 | var b = 12 8 | } 9 | console.log(b) 10 | 11 | const c = 1 12 | // const c = 2 13 | let d = 2 14 | d = 3 15 | 16 | if (true) { 17 | let f = 12 18 | } 19 | // console.log(f) 20 | 21 | -------------------------------------------------------------------------------- /4.js: -------------------------------------------------------------------------------- 1 | let show1 = function () { 2 | console.log('abc') 3 | } 4 | 5 | let show2 = () => { 6 | console.log('abc') 7 | } 8 | 9 | show1() // 调用函数 10 | show2() 11 | 12 | let show4 = function (a) { 13 | return a*2 14 | } 15 | 16 | let show5 = a => a * 2 //简洁,类似python lambda 函数 17 | 18 | console.log(show4(10)) 19 | console.log(show5(10)) 20 | -------------------------------------------------------------------------------- /5.js: -------------------------------------------------------------------------------- 1 | function show(a, b, ...args) { 2 | console.log(a) 3 | console.log(b) 4 | console.log(args) 5 | } 6 | console.log(show(1, 2, 3, 4, 5)) 7 | 8 | let arr1 = [1, 2, 3] 9 | let arr2 = [4, 5, 6] 10 | let arr3 = [...arr1, ...arr2] 11 | console.log(arr3) 12 | 13 | function show2(a, b=5, c=8) { 14 | console.log(a, b, c) 15 | } 16 | show2(88, 12) -------------------------------------------------------------------------------- /6.js: -------------------------------------------------------------------------------- 1 | let [a, b, c] = [1, 2, 3] 2 | console.log(a, b, c) 3 | 4 | let {x, y, z} = {x: 1, y: 2, z: 3} 5 | console.log(x, y, z) 6 | 7 | let [json, arr, num, str] = [{ a: 1, b: 2 }, [1, 2, 3], 8, 'str'] 8 | console.log(json, arr, num, str) 9 | 10 | -------------------------------------------------------------------------------- /7.js: -------------------------------------------------------------------------------- 1 | var arr = [1, 3, 5, 7] 2 | var result = arr.reduce(function (tmp, item, index) { 3 | //tmp 上次结果,item当前数,index次数1开始 4 | console.log(tmp, item, index) 5 | return tmp + item 6 | }) 7 | console.log(result) 8 | 9 | var arr = [1, 3, 5, 7] 10 | var result = arr.reduce(function (tmp, item, index) { 11 | if (index != arr.length - 1) { // 不是最后一次 12 | return tmp + item 13 | } else { 14 | return (tmp + item)/arr.length 15 | } 16 | }) 17 | console.log(result) // 平均值 18 | 19 | 20 | var arr = [12, 4, 8, 9] 21 | var result = arr.filter(item => (item % 3 === 0) ? true : false) 22 | console.log(result) 23 | var result = arr.filter(item => item % 3 === 0) 24 | console.log(result) 25 | 26 | var arr = [ 27 | { title: '苹果', price: 10 }, 28 | { title: '西瓜', price: 20 }, 29 | ] 30 | var result = arr.filter(json => json.price >= 20) 31 | console.log(result) 32 | 33 | var arr = [12, 4, 8, 9] 34 | var result = arr.forEach(item => console.log(item)) 35 | var result = arr.forEach((item, index)=>console.log(item, index)) -------------------------------------------------------------------------------- /8.js: -------------------------------------------------------------------------------- 1 | var url = 'http://qq.com' 2 | console.log(url.startsWith('http')) 3 | console.log(url.endsWith('com')) 4 | 5 | let a = 12 6 | let str1 = `asdf${a}` 7 | console.log(str1) 8 | 9 | let title = '标题' 10 | let content = '内容' 11 | let str = `
12 |

${title}

13 |

${content}

14 | ` 15 | console.log(str) -------------------------------------------------------------------------------- /9.js: -------------------------------------------------------------------------------- 1 | /* 2 | function User(name, pass) { 3 | this.name = name 4 | this.pass = pass 5 | } 6 | 7 | User.prototype.showName = function () { 8 | console.log(this.name) 9 | } 10 | User.prototype.showPass = function () { 11 | console.log(this.pass) 12 | } 13 | 14 | var u1 = new User('able', '1233') 15 | u1.showName() 16 | u1.showPass() 17 | 18 | */ 19 | class User { 20 | constructor(name, pass) { 21 | this.name = name 22 | this.pass = pass 23 | } 24 | showName() { 25 | console.log(this.name) 26 | } 27 | showPass() { 28 | console.log(this.pass) 29 | } 30 | } 31 | 32 | var u1 = new User('able2', '111') 33 | u1.showName() 34 | u1.showPass() -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 able8 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hello-es6 2 | 3 | ES6 各种新语法 入门了解 石川blue讲解 4 | 5 | 视频地址 6 | 7 | - [b站:深入解读ES6系列](https://www.bilibili.com/video/av20327829/) 8 | 9 | 看视频整理要点笔记: 10 | 11 | ---- 12 | 13 | - [hello-es6](#hello-es6) 14 | - [1.ES6怎么来的](#1es6%E6%80%8E%E4%B9%88%E6%9D%A5%E7%9A%84) 15 | - [2.ES6兼容性](#2es6%E5%85%BC%E5%AE%B9%E6%80%A7) 16 | - [3.变量 let 和 常量 const](#3%E5%8F%98%E9%87%8F-let-%E5%92%8C-%E5%B8%B8%E9%87%8F-const) 17 | - [4.函数-箭头函数](#4%E5%87%BD%E6%95%B0-%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0) 18 | - [5.函数-参数](#5%E5%87%BD%E6%95%B0-%E5%8F%82%E6%95%B0) 19 | - [6.解构赋值](#6%E8%A7%A3%E6%9E%84%E8%B5%8B%E5%80%BC) 20 | - [7.数组](#7%E6%95%B0%E7%BB%84) 21 | - [8.字符串](#8%E5%AD%97%E7%AC%A6%E4%B8%B2) 22 | - [9.面向对象-基础](#9%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1-%E5%9F%BA%E7%A1%80) 23 | - [10.面向对象应用](#10%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E5%BA%94%E7%94%A8) 24 | - [11.json](#11json) 25 | - [12.Promise](#12promise) 26 | - [13.generator-认识生成器函数](#13generator-%E8%AE%A4%E8%AF%86%E7%94%9F%E6%88%90%E5%99%A8%E5%87%BD%E6%95%B0) 27 | - [14.generator-yield是啥](#14generator-yield%E6%98%AF%E5%95%A5) 28 | - [15.generator-实例](#15generator-%E5%AE%9E%E4%BE%8B) 29 | - [16.ES7 预览](#16es7-%E9%A2%84%E8%A7%88) 30 | 31 | ---- 32 | 33 | ## 1.ES6怎么来的 34 | 35 | - ECMAScript 和 JavaScript 36 | - ECMA 是标准,JS 是实现 37 | - ECMAScript 简称 ECMA 或 ES 38 | 39 | - 历史版本 40 | - 1996, ES1.0 Netscape 将 JS 提交给 ECMA 组织,ES 正式出现 41 | - 1999, ES3.0 被广泛支持 42 | - 2011, ES5.1 成为 ISO 国际标准 43 | - 2015, ES6.0 正式发布 44 | 45 | ## 2.ES6兼容性 46 | 47 | - ES6(ES2015) 支持的环境 IE10+, Chrome, FireFox, 移动端, NodeJS 48 | - 解决不兼容办法,编译、转换 49 | - 在线转换 50 | - 或者提前编译 51 | 52 | - [Babel 中文网](https://www.babeljs.cn) 53 | - [Babel 入门教程 阮一峰](http://www.ruanyifeng.com/blog/2016/01/babel.html) 54 | - Babel 是一个 JavaScript 编译器 55 | - 一个广泛使用的转码器,可以将ES6代码转为ES5代码,从而在现有环境执行 56 | - 现在就用 ES6 编写程序,而不用担心现有环境是否支持 57 | 58 | ## 3.变量 let 和 常量 const 59 | 60 | - var 的问题 61 | - 可以重复声明,没有报错和警告 62 | - 无法限制修改 63 | - 没有块级作用域, `{ }` 64 | 65 | - let 和 const 66 | - 不能重复声明 67 | - 都是块级作用域, `{ }` 块内声明的,块外无效 68 | - let 是变量,可以修改 69 | - const 是常量,不能修改 70 | 71 | - 块级作用域举例 72 | - 原来用 var 的方式,结果弹出的都是 3 73 | - 或者将变量 封装到函数里,限制作用域,但比较麻烦 74 | - 用 let 最简单,直接 var 改 let,解决作用域问题 75 | 76 | ```html 77 | 78 | 79 | 80 | 81 | 82 | 83 | Document 84 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | ``` 119 | 120 | ## 4.函数-箭头函数 121 | 122 | - 箭头函数,就是函数的简写 123 | - 如果只有一个参数,`()` 可以省 124 | - 如果只有一个`return`,`{}`可以省 125 | 126 | ```js 127 | // 普通函数 128 | function name() { 129 | 130 | } 131 | // 箭头函数,去掉 function, 加上 => 132 | () => { 133 | 134 | } 135 | ``` 136 | 137 | ```js 138 | let show1 = function () { 139 | console.log('abc') 140 | } 141 | 142 | let show2 = () => { 143 | console.log('abc') 144 | } 145 | 146 | show1() // 调用函数 147 | show2() 148 | 149 | let show4 = function (a) { 150 | return a*2 151 | } 152 | 153 | let show5 = a => a * 2 //简洁,类似python lambda 函数 154 | 155 | console.log(show4(10)) 156 | console.log(show5(10)) 157 | ``` 158 | 159 | ## 5.函数-参数 160 | 161 | - 参数扩展/展开 `...args` 162 | - 收集剩余的参数,必须当到最后一个参数位置 163 | - 展开数组,简写,效果和直接把数组的内容写在这儿一样 164 | - 默认参数 165 | 166 | ```js 167 | function show(a, b, ...args) { 168 | console.log(a) 169 | console.log(b) 170 | console.log(args) 171 | } 172 | console.log(show(1, 2, 3, 4, 5)) 173 | 174 | let arr1 = [1, 2, 3] 175 | let arr2 = [4, 5, 6] 176 | let arr3 = [...arr1, ...arr2] 177 | console.log(arr3) 178 | 179 | function show2(a, b=5, c=8) { 180 | console.log(a, b, c) 181 | } 182 | show2(88, 12) 183 | ``` 184 | 185 | ## 6.解构赋值 186 | 187 | ```js 188 | let [a, b, c] = [1, 2, 3] 189 | console.log(a, b, c) 190 | 191 | let {x, y, z} = {x: 1, y: 2, z: 3} 192 | console.log(x, y, z) 193 | 194 | let [json, arr, num, str] = [{ a: 1, b: 2 }, [1, 2, 3], 8, 'str'] 195 | console.log(json, arr, num, str) 196 | ``` 197 | 198 | - 解构赋值 199 | - 左右两个边结构必须一样 200 | - 右边必须是个东西 201 | - 声明和赋值赋值不能分开,必须在一句话里 202 | 203 | ## 7.数组 204 | 205 | - 新增4个方法 206 | - map 映射 一个对一个 207 | 208 | ```js 209 | let arr = [12, 5, 8] 210 | let result = arr.map(function (item) { 211 | return item*2 212 | }) 213 | let result2 = arr.map(item=>item*2) // 简写 214 | console.log(result) 215 | console.log(result2) 216 | 217 | let score = [18, 86, 88, 24] 218 | let result3 = score.map(item => item >= 60 ? '及格' : '不及格') 219 | console.log(result3) 220 | 221 | // 结果 222 | [ 24, 10, 16 ] 223 | [ 24, 10, 16 ] 224 | [ '不及格', '及格', '及格', '不及格' ] 225 | ``` 226 | 227 | - reduce 汇总 一堆出来一个 228 | - 用于比如,算个总数,算个平均 229 | 230 | ```js 231 | var arr = [1, 3, 5, 7] 232 | var result = arr.reduce(function (tmp, item, index) { 233 | //tmp 上次结果,item当前数,index次数1开始 234 | console.log(tmp, item, index) 235 | return tmp + item 236 | }) 237 | console.log(result) 238 | 239 | var arr = [1, 3, 5, 7] 240 | var result = arr.reduce(function (tmp, item, index) { 241 | if (index != arr.length - 1) { // 不是最后一次 242 | return tmp + item 243 | } else { 244 | return (tmp + item)/arr.length 245 | } 246 | }) 247 | console.log(result) // 平均值 248 | ``` 249 | 250 | - filter 过滤器 保留为true的 251 | 252 | ```js 253 | var arr = [12, 4, 8, 9] 254 | var result = arr.filter(item => (item % 3 === 0) ? true : false) 255 | console.log(result) 256 | var result = arr.filter(item => item % 3 === 0) 257 | console.log(result) 258 | 259 | var arr = [ 260 | { title: '苹果', price: 10 }, 261 | { title: '西瓜', price: 20 }, 262 | ] 263 | var result = arr.filter(json => json.price >= 20) 264 | console.log(result) 265 | ``` 266 | 267 | - forEach 循环迭代 268 | 269 | ```js 270 | var arr = [12, 4, 8, 9] 271 | var result = arr.forEach(item => console.log(item)) 272 | var result = arr.forEach((item, index)=>console.log(item, index)) 273 | ``` 274 | 275 | ## 8.字符串 276 | 277 | - 多了两个新方法 278 | - `startsWith` 279 | - `endsWith` 280 | 281 | ```js 282 | var url = 'http://qq.com' 283 | console.log(url.startsWith('http')) 284 | console.log(url.endsWith('com')) 285 | // 都是 true 286 | ``` 287 | 288 | - 字符串模版 289 | - 使用反引号,`${变量}` 290 | - 可以折行 291 | 292 | ```js 293 | let a = 12 294 | let str1 = `asdf${a}` 295 | console.log(str1) 296 | 297 | let title = '标题' 298 | let content = '内容' 299 | let str = `
300 |

${title}

301 |

${content}

302 | ` 303 | console.log(str) 304 |
305 |

标题

306 |

内容

307 | ``` 308 | 309 | ## 9.面向对象-基础 310 | 311 | - 原来写法 312 | - 类和构造函数一样 313 | - 属性和方法分开写的 314 | 315 | ```js 316 | // 老版本 317 | function User(name, pass) { 318 | this.name = name 319 | this.pass = pass 320 | } 321 | 322 | User.prototype.showName = function () { 323 | console.log(this.name) 324 | } 325 | User.prototype.showPass = function () { 326 | console.log(this.pass) 327 | } 328 | 329 | var u1 = new User('able', '1233') 330 | u1.showName() 331 | u1.showPass() 332 | // 老版本继承 333 | function VipUser(name, pass, level) { 334 | User.call(this, name, pass) 335 | this.level = level 336 | } 337 | VipUser.prototype = new User() 338 | VipUser.prototype.constructor = VipUser 339 | VipUser.prototype.showLevel = function () { 340 | console.log(this.level) 341 | } 342 | 343 | var v1 = new VipUser('blue', '1234', 3) 344 | v1.showName() 345 | v1.showLevel() 346 | 347 | ``` 348 | 349 | - 新版面向对象 350 | - 有了 class 关键字、构造器 351 | - class 里面直接加方法 352 | - 继承,super 超类==父类 353 | 354 | ```js 355 | class User { 356 | constructor(name, pass) { 357 | this.name = name 358 | this.pass = pass 359 | } 360 | 361 | showName() { 362 | console.log(this.name) 363 | } 364 | showPass() { 365 | console.log(this.pass) 366 | } 367 | } 368 | 369 | var u1 = new User('able2', '111') 370 | u1.showName() 371 | u1.showPass() 372 | 373 | // 新版本继承 374 | class VipUser extends User { 375 | constructor(name, pass, level) { 376 | super(name, pass) 377 | this.level = level 378 | } 379 | showLevel(){ 380 | console.log(this.level) 381 | } 382 | } 383 | 384 | v1 = new VipUser('blue', '123', 3) 385 | v1.showLevel() 386 | ``` 387 | 388 | ## 10.面向对象应用 389 | 390 | - [React](https://www.reactjscn.com) 391 | - 用于构建用户界面的 JavaScript 库 392 | - 组件化,一个组件就是一个 class 393 | - JSX == bable == browser.js 394 | 395 | ## 11.json 396 | 397 | - JSON 格式 398 | - JavaScript Object Notation 的缩写,是一种用于数据交换的文本格式 399 | - JSON 是 JS对象 的严格子集 400 | - JSON 的标准写法 401 | - 只能用双引号 402 | - 所有的key都必须用双引号包起来 403 | 404 | - JSON 对象 405 | - JSON 对象是 JavaScript 的原生对象,用来处理 JSON 格式数据,有两个静态方法 406 | - JSON.parse(string) :接受一个 **JSON 字符串**并将其转换成一个 JavaScript **对象**。 407 | - JSON.stringify(obj) :接受一个 JavaScript **对象**并将其转换为一个 **JSON 字符串**。 408 | 409 | ```js 410 | var json = {a: 12, b: 5} 411 | var str = 'hi,' + JSON.stringify(json) 412 | var url = 'http://www.xx.com/' + encodeURIComponent(JSON.stringify(json)) 413 | console.log(str) 414 | console.log(url) 415 | 416 | var str = '{"a": 12, "b": 4, "c": "abc"}' 417 | var json = JSON.parse(str) 418 | console.log(json) 419 | hi,{"a":12,"b":5} 420 | http://www.xx.com/%7B%22a%22%3A12%2C%22b%22%3A5%7D 421 | { a: 12, b: 4, c: 'abc' } 422 | ``` 423 | 424 | - 对象(object) 425 | - 是 JavaScript 语言的核心概念,也是最重要的数据类型 426 | - 对象就是一组“键值对”(key-value)的集合,是一种无序的复合数据集合 427 | - 对象的所有键名都是字符串, 所以加不加引号都可以 428 | - 如果键名是数值,会被自动转为字符串 429 | - 对象的每一个键名又称为“属性”(property),它的“键值”可以是任何数据类型 430 | - 如果一个属性的值为函数,通常把这个属性称为“方法”,它可以像函数那样调用 431 | - in 运算符用于检查对象是否包含某个属性(注意,检查的是键名,不是键值 432 | - for...in循环用来遍历一个对象的全部属性 433 | 434 | - 对象 简写 435 | - key-value 一样时可以简写 436 | - 里面函数可以简写, 去掉 437 | 438 | ```js 439 | var a = 12, b = 5 440 | console.log({a:a, b:b}) 441 | console.log({a, b}) 442 | console.log({a, b, c:"c"}) 443 | console.log({ a, b, show(){ console.log('a') }}) 444 | { a: 12, b: 5 } 445 | { a: 12, b: 5 } 446 | { a: 12, b: 5, c: 'c' } 447 | { a: 12, b: 5, show: [Function: show] } 448 | ``` 449 | 450 | ## 12.Promise 451 | 452 | - 异步和同步 453 | - 异步,操作之间没有关系,同时执行多个操作, 代码复杂 454 | - 同步,同时只能做一件事,代码简单 455 | 456 | - Promise 对象 457 | - 用同步的方式来书写异步代码 458 | - Promise 让异步操作写起来,像在写同步操作的流程,不必一层层地嵌套回调函数 459 | - 改善了可读性,对于多层嵌套的回调函数很方便 460 | - 充当异步操作与回调函数之间的中介,使得异步操作具备同步操作的接口 461 | 462 | - Promise 也是一个构造函数 463 | - 接受一个回调函数f1作为参数,f1里面是异步操作的代码 464 | - 返回的p1就是一个 Promise 实例 465 | - 所有异步任务都返回一个 Promise 实例 466 | - Promise 实例有一个then方法,用来指定下一步的回调函数 467 | 468 | ```js 469 | function f1(resolve, reject) { 470 | // 异步代码... 471 | } 472 | var p1 = new Promise(f1); 473 | p1.then(f2); // f1的异步操作执行完成,就会执行f2。 474 | ``` 475 | 476 | - Promise 使得异步流程可以写成同步流程 477 | 478 | ```js 479 | // 传统写法 480 | step1(function (value1) { 481 | step2(value1, function(value2) { 482 | step3(value2, function(value3) { 483 | step4(value3, function(value4) { 484 | // ... 485 | }); 486 | }); 487 | }); 488 | }); 489 | 490 | // Promise 的写法 491 | (new Promise(step1)) 492 | .then(step2) 493 | .then(step3) 494 | .then(step4); 495 | ``` 496 | 497 | - Promise.all(promiseArray)方法 498 | - 将多个Promise对象实例包装,生成并返回一个新的Promise实例 499 | - promise数组中所有的promise实例都变为resolve的时候,该方法才会返回 500 | - 并将所有结果传递results数组中 501 | - promise数组中任何一个promise为reject的话,则整个Promise.all调用会立即终止,并返回一个reject的新的promise对象 502 | 503 | ```js 504 | var p1 = Promise.resolve(1), 505 | p2 = Promise.resolve(2), 506 | p3 = Promise.resolve(3); 507 | Promise.all([p1, p2, p3]).then(function (results) { 508 | console.log(results); // [1, 2, 3] 509 | }); 510 | ``` 511 | 512 | - Promise.race([p1, p2, p3]) 513 | - Promse.race就是赛跑的意思 514 | - 哪个结果获得的快,就返回那个结果 515 | - 不管结果本身是成功状态还是失败状态 516 | 517 | ## 13.generator-认识生成器函数 518 | 519 | - generator 生成器函数 520 | - 普通函数,一路到底 521 | - generator函数,中间可以停,到哪停呢,用 yield 配合,交出执行权 522 | - yield 有 放弃、退让、退位的意思 523 | - 需要调用next()方法启动执行,需要遇到 yield 停, 踹一脚走一步 524 | - generator函数前面加一个 `*` 两边可以有空格,或靠近函数或`function` 525 | - 背后实际生成多个小函数,实现走走停停 526 | 527 | ```js 528 | function show() { 529 | console.log('a') 530 | console.log('b') 531 | } 532 | show() // 普通函数 533 | 534 | function *show2() { 535 | console.log('1') 536 | yield 537 | console.log('2') 538 | } 539 | let genObj = show2() 540 | genObj.next() // 1 541 | genObj.next() // 2 542 | genObj.next() // 最后了,没有结果 543 | ``` 544 | 545 | ## 14.generator-yield是啥 546 | 547 | - `yield` 548 | - 既可传参,又可以返回 549 | - 第一个`next()`传参无效,只用来启动 550 | 551 | - 如果函数前漏掉 `*` 552 | - 就是普通函数 553 | - 如果有`yield`会报错, `ReferenceError: yield is not defined` 554 | - yield 只能在Generator函数内部使用 555 | 556 | ```js 557 | function * show() { 558 | console.log('1') 559 | var a = yield 560 | console.log('2') 561 | console.log(a) 562 | } 563 | // yield 传参 564 | var gen = show() 565 | gen.next() // 1 566 | gen.next() // 2 和 undefined 因为没有传参,yield没有返回值 567 | var gen = show() 568 | gen.next(10) // 1 第一次执行到yield,但没有执行赋值 569 | gen.next(20) // 2 和 20 570 | 571 | function* show2() { 572 | console.log('1') 573 | yield 10 574 | console.log('2') 575 | } 576 | // yield 返回 577 | var gen = show2() 578 | var res1 = gen.next() 579 | console.log(res1) // { value: 10, done: false } 580 | var res2 = gen.next() 581 | console.log(res2) 582 | // { value: undefined, done: true } 最后的value需要return返回 583 | ``` 584 | 585 | ## 15.generator-实例 586 | 587 | - Promise 适合一次读一组 588 | - generator 适合逻辑性的 589 | 590 | ```js 591 | // 带逻辑-generator 592 | runner(function * () { 593 | let userData = yield $.ajax({url: 'getUserData'}) 594 | 595 | if (userData.type == 'VIP') { 596 | let items = yield $.ajax({url: 'getVIPItems'}) 597 | } else { 598 | let items = yield $.ajax({url: 'getItems'}) 599 | } 600 | }) 601 | ``` 602 | 603 | ```js 604 | // yield 实例,用同步方式写异步 605 | server.use(function * () { 606 | let data = yield db.query(`select * from user_table`) 607 | this.body = data 608 | }) 609 | ``` 610 | 611 | ## 16.ES7 预览 612 | 613 | - 数组 614 | - `arr.includes()` 数组是否包含某个东西 615 | - 数组的 arr.keys(), arr,entries() 616 | - for ... in 遍历数组 下标 key 617 | - for ... of 遍历数组 值 value, 不能用于json 618 | 619 | ```js 620 | let arr = ['a', 'b', 'c'] 621 | console.log(arr.includes(1)) 622 | 623 | for (let i in arr) { 624 | console.log(i) // 循环的时下标 key 625 | } 626 | 627 | for (let i of arr) { 628 | console.log(i) // 循环的是值 value 629 | } 630 | for (let i of arr.keys()) { 631 | console.log('>'+i) 632 | } 633 | for (let [key, value] of arr.entries()) { 634 | console.log('>' + key + value) 635 | } 636 | 637 | let json = { a: 12, b: 5, c: 7 } 638 | for (let i in json) { 639 | console.log(i) 640 | } 641 | ``` 642 | 643 | - 字符串 644 | - padStart()/padEnd() 指定宽度,不够就补空格或指定字符 645 | 646 | ```js 647 | console.log('=' + 'abcd'.padStart(6, '0') + '=') 648 | console.log('=' + 'abcd'.padEnd(6, '0') + '=') 649 | =00abcd= 650 | =abcd00= 651 | ``` 652 | 653 | - 容忍度 654 | - [1, 2, 3,] 老版数组最后不能有逗号,新的可以有 655 | - 函数参数最后多的逗号也可以 656 | 657 | - async await 658 | - 和 generator yield 类似 659 | - generator 不可以写成箭头函数, async 可以 660 | 661 | ```js 662 | async function show() { 663 | console.log(1) 664 | await 665 | console.log(2) 666 | } 667 | ``` -------------------------------------------------------------------------------- /tempCodeRunnerFile.js: -------------------------------------------------------------------------------- 1 | let score = [18, 86, 88, 24] 2 | score.map() --------------------------------------------------------------------------------