├── .gitattributes ├── .idea ├── ES6-learn.iml ├── jsLibraryMappings.xml ├── misc.xml ├── modules.xml ├── vcs.xml ├── watcherTasks.xml └── workspace.xml ├── Array.js ├── README.md ├── es6-one-day.js ├── git.js ├── let-and-const.js ├── promise.js └── set-or-map.js /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto -------------------------------------------------------------------------------- /.idea/ES6-learn.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/jsLibraryMappings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/watcherTasks.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 19 | 20 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 72 | 73 | 74 | 76 | 77 | 85 | 86 | 87 | 88 | 89 | true 90 | DEFINITION_ORDER 91 | 92 | 93 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 132 | 133 | 134 | 135 | 138 | 139 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 189 | 190 | project 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | project 207 | 208 | 209 | true 210 | 211 | 212 | 213 | DIRECTORY 214 | 215 | false 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 1508315766383 239 | 252 | 253 | 254 | 255 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 283 | 286 | 287 | 288 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | -------------------------------------------------------------------------------- /Array.js: -------------------------------------------------------------------------------- 1 | //ES6数组的扩展 2 | //Array.from() 3 | 4 | //下面是一个类的对象 5 | let arrayLike = { 6 | '0': 'a', 7 | '1': 'b', 8 | '2': 'c', 9 | length: 3 10 | }; 11 | 12 | //ES5的写法 13 | var arr1 = [].slice.call(arrayLike); 14 | 15 | //ES6的写法 16 | let arr2 = Array.from(arrayLike); 17 | 18 | //常见的类似数组的对象是DOM操作返回的NodeList集合, 19 | //以及函数内部的arguments对象。Array.from都可以将它们转为真正的数组。 20 | 21 | //NodeList 对象 22 | let ps = document.querySelectorAll('p'); 23 | Array.from(ps).forEach(function(p) { 24 | console.log(p); 25 | }) 26 | 27 | // arguments对象 28 | function foo() { 29 | var args = Array.from(arguments); 30 | // ... 31 | } 32 | 33 | //只要是部署了Iterator接口的数据结构,Array.from都能将其转为数组。 34 | Array.from('hello'); 35 | //['h', 'e', 'l', 'l', 'o'] 36 | 37 | let namesSet = new Set(['a', 'b']); 38 | Array.from(namesSet); 39 | //字符串和Set结构都具有Iterator接口,因此可以被Array.from转为真正的数组。 40 | 41 | Array.from(1, 2, 3); 42 | 43 | //扩展运算符(...)也可以将某些数据结构转为数组。 44 | //arguments 45 | function foo() { 46 | var args = [...arguments]; 47 | } 48 | Array.from({ length: 3 }); 49 | // [ undefined, undefined, undefined ] 50 | 51 | const toArray = (() => { 52 | Array.form ? Array.form : obj => [].slice.call(obj); 53 | })(); 54 | 55 | //Array.form 还可以接收第二个参数 类似于数组的map方法 56 | 57 | Array.form(arrayLike, x => x * x); 58 | 59 | //等同于 60 | 61 | Array.form(arrayLike).map(x => x * x); 62 | 63 | Array.form([1, 2, 3], (x) => x * x); 64 | 65 | //[1,4,9] 66 | 67 | //获取dom 68 | 69 | let spans = document.querySelectorAll(span.name); 70 | 71 | //map() 72 | let names1 = Array.prototype.map.call(spans, s => s.textContent); 73 | 74 | //Array.form 75 | let names2 = Array.form(spans, s => s.s.textContent); 76 | 77 | //Array.form() 将数组转成false的成员转为0 78 | Array.form([1, 2, 3], (n) => n || 0); 79 | console.log(n); 80 | 81 | Array() // [] 82 | Array(3) // [, , ,] 83 | Array(3, 11, 8) // [3, 11, 8] 84 | 85 | //Array.of总是返回参数值组成的数组。如果没有参数,就返回一个空数组。 86 | 87 | function ArrayOf() { 88 | return [].slice.call(arguments); 89 | } 90 | 91 | 92 | //数组实例的copyWithin() 93 | Array.prototype.copyWithin(target, start = 0, end = this.length); 94 | 95 | [1, 2, 3, 4, 5].copyWithin(0, 3); 96 | //4 5 3 4 5 97 | 98 | // 将3号位复制到0号位 99 | [1, 2, 3, 4, 5].copyWithin(0, 3, 4) 100 | // [4, 2, 3, 4, 5] 101 | 102 | //数组实例的find()和findIndex() 103 | //数组中找出小于零的 104 | [1, 4, -5, 10].find((n) => n < 0) 105 | console.log([1, 4, -5, 10].find((n) => n < 0)); 106 | 107 | [1, 5, 10, 15].find(function(value, index, err) { 108 | return value > 9; 109 | }) 110 | console.log([1, 5, 10, 15].find(function(value, index, err) { 111 | return value > 9; 112 | })) //10 113 | 114 | //findindex()返回符合条件的数组成员位置 115 | [1, 5, 10, 15].findIndex(function(value, inidex, err) { 116 | return value > 9 117 | }) 118 | 119 | //fill() 120 | //fill方法给定值 填充数组 121 | ['a', 'b', 'c'].fill(7); 122 | 123 | let min = 14.1; 124 | let eaiml = "hzl.shenwei@gmail.com"; 125 | 126 | let arrData = [ 127 | { 128 | name : "111", 129 | arr:[ 130 | { 131 | name:'333', 132 | children:[ 133 | { 134 | name:'yyy' 135 | } 136 | ] 137 | } 138 | ] 139 | }, 140 | { name : "311" }, 141 | { name : "11" }, 142 | { name : "111" }, 143 | { name : "111" }, 144 | ] 145 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 根据ES6文档学习... 2 | ===================== 3 | 4 | 5 | -------------------------------------------------------------------------------- /es6-one-day.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Administrator on 2017/10/18. 3 | */ 4 | //ES6编程风格 5 | 6 | //1、块级作用域 7 | //let 取代 var 8 | 'use strict'; 9 | if(true){ 10 | let x = 'hello'; 11 | } 12 | for(let i = 0; i<10; i++){ 13 | console.log(i); 14 | } 15 | //var 命令存在变量提升效用 let命令没有这个问题 16 | 'use strict'; 17 | if(true){ 18 | //console.log(x); 19 | let x = 'word'; //如果用var console.log()就不会报错 而是undefined 20 | } 21 | //使用const 有利于提升程序的运行效率 22 | 23 | var a = 1,b=2,c= 3; 24 | //good 25 | const a = 1; 26 | const b = 2; 27 | const c = 3; 28 | 29 | //best 30 | const [a,b,c] = [1,2,3]; 31 | //const声明的是常用 有利防止无意间改变量值导致错误 32 | 33 | // 字符串 静态使用单引号 或者小撇号 不使用双引号 动态一律小撇 34 | //bad 35 | const a ="foobar"; 36 | 37 | //acceptable 38 | const c = `foobar`; 39 | 40 | //good 41 | const a = `foobar`; 42 | const b = `foo{a}bar`; 43 | 44 | //解构赋值 45 | //使用数组成员对变量赋值 优先使用解构赋值 46 | const arr = [1,2,3,4]; 47 | 48 | //错误的 49 | const first = arr[0]; 50 | const second = arr[1]; 51 | 52 | //good 53 | const [first,second] = arr; 54 | 55 | //函数的参数如果也事对象成员 也解构 56 | //bad 57 | function getFullName(user) { 58 | const firstName = user.firstName; 59 | const lastName = user.lastName; 60 | } 61 | 62 | //good 63 | function getFullName(obj) { 64 | const { firstName,lastName } = obj; 65 | } 66 | 67 | //best 68 | function getFullName({firstName, lastName}) { 69 | 70 | } 71 | 72 | //返回多个值 依然对象解构fuzhi 73 | //bad 74 | function processInput(input) { 75 | return [left,right,top,bottom]; 76 | } 77 | 78 | //good 79 | function processInput(input) { 80 | return { left,right,top,bottom }; 81 | } 82 | 83 | const {left,right} = processInput(input); 84 | 85 | //定义对象 单行不能逗号结尾 多行以逗号结尾 86 | //错误写法 87 | const a ={ k1:v1,k2:v2,}; 88 | const b = { 89 | k1:v1, 90 | k2:v2 91 | } 92 | 93 | //good code 94 | const a ={ k1:v1,k2:v2}; 95 | const b = { 96 | k1:v1, 97 | k2:v2, 98 | } 99 | //对象尽量静态化,一旦定义,就不得随意添加新的属性。如果添加属性不可避免,要使用Object.assign方法。 100 | 101 | //错误 102 | const a ={}; 103 | a.x= 3; 104 | 105 | //如果要添加 106 | const a = {}; 107 | Object.assign(a,{x:2}); 108 | 109 | //good 110 | const a = { x:null }; 111 | a.x = 3; 112 | 113 | //如果是动态的 使用属性表达式定义 114 | //bad 115 | const obj = { 116 | id:5, 117 | name:'mary', 118 | }; 119 | obj[getKey('emable')] = true; 120 | //good 121 | const obj ={ 122 | id:2, 123 | name:'getbum', 124 | [getKey('hhehh')]:true 125 | } 126 | 127 | //对象的属性和方法 尽量采用简洁表达方法 128 | //如 129 | var ref = 'some value'; 130 | 131 | //bad 132 | const atom = { 133 | ref:ref, 134 | value:1, 135 | addValue:function (value) { 136 | return atom.value+value; 137 | }, 138 | }; 139 | 140 | //good 141 | const atom = { 142 | ref, 143 | value:1, 144 | addValue(value){ 145 | return atom.value+value; 146 | }, 147 | }; 148 | 149 | //拷贝数组 150 | //bad 151 | const len = items.length; 152 | const itemsCopy = []; 153 | 154 | let i; 155 | 156 | for(i = 0; i{ 169 | console.log('hello word'); 170 | })(); 171 | 172 | //需要函数表达式的场景 用箭头函数 173 | 174 | //bad 175 | [1,2,3].map(function (x) { 176 | return x*x ; 177 | }); 178 | 179 | //good 180 | [1,2,3].map((x)=>{ 181 | return x*x ; 182 | }); 183 | 184 | //best 185 | [1,2,3].map(x=>x*x); 186 | 187 | //箭头函数取代Function.prototype.bid 不再用that之类绑定this 188 | //bad 189 | const self = this; 190 | const boundMethod = function (...params) { 191 | return method.apply(self,params); 192 | } 193 | 194 | //accepable 195 | const boundMethod = method.bind(this); 196 | 197 | //best 198 | const boundMethod = (...params)=>method.apply(this,params); 199 | 200 | //简单的、单行的、不会复用的函数,建议采用箭头函数。 201 | // 如果函数体较为复杂,行数较多,还是应该采用传统的函数写法。 202 | 203 | //bad 204 | function divide(a, b, option = false) { 205 | 206 | } 207 | 208 | //good 209 | function divide(a, b, {option = false} = {}) { 210 | 211 | } 212 | //不要在函数体内使用arguments变量,使用rest运算符(...)代替 213 | //bad 214 | function concatenateAll() { 215 | const args = Array.prototype.slice.call(arguments); 216 | return args.join(''); 217 | } 218 | 219 | //good 220 | function concatenateAll(...args) { 221 | return args.join(''); 222 | } 223 | 224 | //设置函数参数的默认值 225 | //bad 226 | function handleThings(opts) { 227 | opts = opts || {}; 228 | } 229 | 230 | //good 231 | function handleThings(opts = {}) { 232 | //... 233 | } 234 | 235 | //Map 结构 236 | //它內建有遍历机制 237 | let map = new Map(arr); 238 | 239 | for(let key of map.keys()){ 240 | console.log(key); 241 | } 242 | 243 | for(let value of map.value()){ 244 | console.log(value); 245 | } 246 | 247 | for(let item of map.entries()){ 248 | console.log(item[0],item[1]); 249 | } 250 | 251 | //用class取代prototype的操作 写法更简洁 姿势更帅 252 | //bad 253 | function Queue(contents = []) { 254 | this._queue = [...contents]; 255 | } 256 | Queue.prototype.pop = function () { 257 | const value = this._queue[0]; 258 | this._queue.slice(0,1); 259 | return value; 260 | } 261 | 262 | //good 263 | class Queue{ 264 | constructor(contents = []){ 265 | this._queue = [...contents]; 266 | } 267 | pop(){ 268 | const value = this._queue[0]; 269 | this._queue.slice(0,1); 270 | return value; 271 | } 272 | } 273 | 274 | //使用extend实现继承 275 | 276 | //bad 277 | const inherits = require('inherits'); 278 | function PeekableQueue(contents) { 279 | Queue.apply(this,contents); 280 | } 281 | inherits(PeekableQueue,Queue); 282 | PeekableQueue.prototype.peek = function () { 283 | return this._queue[0]; 284 | } 285 | 286 | //good 287 | class PeekableQueue extends Queue{ 288 | peek(){ 289 | return this._queue[0]; 290 | } 291 | } 292 | //模块 293 | //module语法是JavaScript模块的标准写法 坚持使用这种写法 294 | //使用import取代require 295 | 296 | //bad 297 | const moduleA = require('moduleA'); 298 | const func1 = moduleA.func1; 299 | const func2 = moduleA.func2; 300 | 301 | //import 302 | import { func1 , func2 } from 'moduleA'; 303 | 304 | //使用export取代module.exports 305 | 306 | //commonJS的写法 307 | var React = require('react'); 308 | 309 | var Breadcrumbs = React.createClass({ 310 | render(){ 311 | return '