<!DOCTYPE html> <html> <head> <title>ES6标准入门(第二版)</title> </head> <body> <script type="text/javascript"> //1、解构赋值 字符串->字符 const [a, b, c, d, e] = "abcde"; alert(a); // a //2、数值数组 -> 数值 var [a, b, c] = [1, 2, 3]; alert(a); //a //3、解构赋值 var [foo, [ [bar], baz ]] = [1, [ [2] ], 3]; alert(foo); // 1 // 4、解构赋值->互换数值 var x = 10; var y = 15; alert("x" + x + ",y" + y); //x:10,y:15 [x, y] = [y, x]; alert("x" + x + ",y" + y); // x:15,y:10 //5、map 的赋值遍历 var map = new Map(); map.set("first", "hello"); map.set("second", "world"); for (var [key, value] of map) { console.log(key + ' is ' + value); } //6、Symbol 类型 : 一个类似字符串的新数据类型 var s1 = Symbol("foo"); var s2 = Symbol("bar"); console.log(typeof(s1)); //7、ArrayBuffer对象 : 代表存储二进制数据的一段内存,不能直接读写 var buf = new ArrayBuffer(32); var dataView = new DataView(buf); var da = dataView.getUint8(0); console.log(da); //8、读取arraybuffer 对象内容 var buffer = new ArrayBuffer(12); var x1 = new Int32Array(buffer); x1[0] = 1; var x2 = new Int32Array(buffer); x2[0] = 2; console.log(x1); //9、二进制数组的应用 var xhr = new xmlHttpRequest(); xhr.open("get", url); xhr.responseType = "arrayBuffer"; xhr.onload = function() { let arrayBuffer = xhr.response; } onreadystatechange = function() { if (req.readyState === 4) { var arrayResponse = xhr.response; var dataView = new DataView(arrayResponse); var ints = new Uint32Array(dataView.byteLength / 4); xhrDiv.style.backgroundColor = "red"; xhrDiv.innerText = "Array is " + ints.length + "uints long"; } } //10、set数据结构,类似数组,但是成员的值是唯一的 var s = new Set(); [2, 3, 4, 5, 6, 7, 2, 3, 4].map(x => s.add(x)); for (let i in s) { console.log(i); } //11、 set赋值 var set = new Set([1, 2, 1, 2, 5, 6, 7]); console.log(set.length); // 还不支持 for (let i in set) { console.log(i); } //12、map,set var m = new Map(); var o = { "p": "Hello World!" }; console.log(o); // {p:"Hello World!"} m.set(o, "content"); console.log(m.get(o)); //content //13、菱形 document.write("<h1>8</h1>"); var n = 5; for (var i = 0; i < 2 * n - 1; i++) { if (i < n) { for (var j = 0; j < 2 * n - 1; j++) { if (j == n - i - 1 || j == n + i - 1) { document.write("蓝"); } else { document.write("一"); } } document.write("</br>"); } else { for (var j = 0; j < 2 * n - 1; j++) { if (j == i - n + 1 || j == 3 * n - i - 3) { document.write("蓝"); } else { document.write("一"); } } document.write("</br>"); } } </script> </body> </html>