Last active
November 11, 2016 11:04
-
-
Save sj82516/0bbcfc9f3639cce028bcae9b1f393cb4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let person = {name: 'John Smith'}; | |
| let tpl = `My name is ${person.name}.`; | |
| console.log(tpl); | |
| let x = 5; | |
| let y =3; | |
| let tpl2 = `${x} + ${y} = ${x+y}`; | |
| console.log(tpl2); | |
| // template literal本身不具重用性,除非自己包成函式 | |
| let tpl3 = (x,y) => `${x} + ${y} = ${x+y}` | |
| let four = tpl3(2,2); | |
| console.log(four); | |
| //Destructure | |
| var data = {x:1, y:2}; | |
| var data2 = {...data, z:3}; | |
| console.log(data2); // Object {x: 1, y: 2, z: 3} | |
| //注意用法,這邊會取出字串長度 | |
| const {length} = "Wth?"; | |
| console.log(length); | |
| //有時候不好讀 | |
| const employee = { | |
| name: "Woo", | |
| address:{ | |
| countruy:"TW", | |
| state:"Taipei" | |
| }, | |
| favorateNumber:[11,22,33,44] | |
| } | |
| //state : Taipei, second: 22 | |
| const { | |
| name, address:{state}, favorateNumber:[,second] | |
| } = employee; | |
| const data4 = null; | |
| let data3 = {...data4?data4:{default:true}} | |
| console.log(data3) | |
| const constX = 3; | |
| //Error: constX is read-only | |
| //constX = 4; | |
| // 關於Const | |
| const arr = [1,2,3]; | |
| arr.push(4); | |
| console.log(arr); // 4還是加上去了!因為Array是Mutable | |
| //解法 | |
| Object.freeze(arr); | |
| //arr.push(4); Error:Can't add property 4, object is not extensible | |
| ========= | |
| let person = {name: 'John Smith'}; | |
| let tpl = `My name is ${person.name}.`; | |
| console.log(tpl); | |
| let x = 5; | |
| let y =3; | |
| let tpl2 = `${x} + ${y} = ${x+y}`; | |
| console.log(tpl2); | |
| // template literal本身不具重用性,除非自己包成函式 | |
| let tpl3 = (x,y) => `${x} + ${y} = ${x+y}` | |
| let four = tpl3(2,2); | |
| console.log(four); | |
| //Destructure | |
| var data = {x:1, y:2}; | |
| var data2 = {...data, z:3}; | |
| console.log(data2); // Object {x: 1, y: 2, z: 3} | |
| //注意用法,這邊會取出字串長度 | |
| const {length} = "Wth?"; | |
| console.log(length); | |
| //有時候不好讀 | |
| const employee = { | |
| name: "Woo", | |
| address:{ | |
| countruy:"TW", | |
| state:"Taipei" | |
| }, | |
| favorateNumber:[11,22,33,44] | |
| } | |
| //state : Taipei, second: 22 | |
| const { | |
| name, address:{state}, favorateNumber:[,second] | |
| } = employee; | |
| const data4 = null; | |
| let data3 = {...data4?data4:{default:true}} | |
| console.log(data3) | |
| const constX = 3; | |
| //Error: constX is read-only | |
| //constX = 4; | |
| // 關於Const | |
| const arr = [1,2,3]; | |
| arr.push(4); | |
| console.log(arr); // 4還是加上去了!因為Array是Mutable | |
| //解法 | |
| Object.freeze(arr); | |
| //arr.push(4); Error:Can't add property 4, object is not extensible | |
| //創建 Array | |
| let arr1 = Array(1,2,3); | |
| let arr2 = Array(10); //產生長度為十的空陣列 | |
| let arr3 = Array.of(10); | |
| let arr4 = Array.from({length:5}, (v,k)=> k*2);//0,2,4,6,8 | |
| console.log(arr1, arr2, arr3, arr4);// 1,2,3 //,,,,,,,,, //10 //0,2,4,6,8 | |
| //關於Set | |
| let x = new Set([1, 2, 3, 4, 4, 4, 5]); | |
| x.add(6); | |
| x.delete(2); | |
| console.log('The set contains', x.size, 'elements.'); | |
| console.log('The set has 1:', x.has(1)); | |
| console.log('The set has 8:', x.has(8)); | |
| //values and keys are the same in a set | |
| x.forEach((value, key, set) => console.log(value, key, set)); | |
| //簡單的Map | |
| const map = new Map([[1,"hello"], ['2', "world"]]); | |
| console.log(map.get('2')); | |
| ===== | |
| //兩者在操作上是等價的 | |
| class Polygon1 { | |
| constructor(height, width) { //class constructor | |
| this.name = 'Polygon'; | |
| this.height = height; | |
| this.width = width; | |
| } | |
| sayName() { //class method | |
| console.log('Hi, I am a', this.name + '.'); | |
| } | |
| } | |
| // De-sugar | |
| let Polygon2 = function(height, width){ | |
| this.name = 'Polygon2'; | |
| this.height = height; | |
| this.width = width; | |
| } | |
| Polygon2.prototype = { | |
| sayName: function(){ | |
| console.log('Hi, I am a', this.name + '.'); | |
| } | |
| } | |
| let p1 = new Polygon1(100,100); | |
| let p2 = new Polygon2(100,100); | |
| console.log(p1, p2); | |
| //查看所有屬性 | |
| let name = []; | |
| for(let n in new Polygon1()){ | |
| name.push(n); | |
| } | |
| console.log(name); | |
| name = []; | |
| for(let n in new Polygon2()){ | |
| name.push(n); | |
| } | |
| console.log(name) | |
| //可以看到 宣告class的少了function,需要換另一個方式 | |
| //Object.getPrototypeOf() 回傳指定物件的原型,換句話說,就是取得該物件的 [[Prototype]] 屬性的值). | |
| // Object.getOwnPropertyNames() 以陣列方式回傳物件的所有屬性(Enumerable or not) | |
| name = []; | |
| console.log(Object.getPrototypeOf(new Polygon1())) | |
| for(let n of Object.getOwnPropertyNames(Object.getPrototypeOf(new Polygon1()))){ | |
| name.push(n); | |
| } | |
| console.log(name) | |
| //如果把Polygon1改成Polygon2,會發現只有sayName而已 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment