# es2015 **Repository Path**: kangpanwork/es2015 ## Basic Information - **Project Name**: es2015 - **Description**: 重学 JavaScript 之你不一定知道的 ES6 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-01-02 - **Last Updated**: 2023-01-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: ES6 ## README # es2015 #### 介绍 重学 JavaScript 之你不一定知道的 ES6 #### 准备工作 1. vscode 2. node 3. nodemon 开发环境 [https://www.npmjs.com/package/nodemon](https://www.npmjs.com/package/nodemon) 4. vscode 使用 code 命令新建 js 文件 ### Proxy ```js const person = { name: "pankang", age: 18, }; const proxyPerson = new Proxy(person, { get(target, property) { return property in target ? target[property] : "default"; }, set(target, property, value) { if (property === "age") { if (!Number.isInteger(value)) { throw new TypeError(`${value} is not an int`); } } target[property] = value; }, }); proxyPerson.age = 19; console.log(proxyPerson); ``` ```js const list = []; const proxyList = new Proxy(list, { set(target, property, value) { console.log('set', property, value); target[property] = value; return target; } }); proxyList.push(100); ``` ### Reflect ```js const obj = { name :'zce', age: 18 }; console.log('name' in obj); console.log(delete obj['age']); console.log(Object.keys(obj)); console.log(Reflect.has(obj, 'name')); console.log(Reflect.deleteProperty(obj, 'age')); console.log(Reflect.ownKeys(obj)); ``` ### Class ```js class Person { constructor(name) { this.name = name; } say() { console.log(`hi, my name is ${this.name}`); } static create(name) { return new Person(name); } } class Student extends Person { constructor(name, number) { super(name); this.number = number; } hello() { super.say(); console.log(`hi, my number is ${this.number}`) } } const p = Person.create("tom"); p.say(); const student = new Student('jacky', 100); student.hello(); ``` ### Set ```js const arr = [1, 2, 3, 1, 3, 4, 5]; const result = [...new Set(arr)]; console.log(result); const rt = Array.from(new Set(arr)); console.log(rt); ``` ### Map ```js const m = new Map(); const tom = { name: "tom" }; m.set(tom, 90); m.forEach((k,v) => { console.log(k, v); }) ``` ### Symbol ```js const name = Symbol(); const person = { [name]: "jack", say() { console.log(this[name]); } }; person.say(); console.log(person[Symbol()]); // undefined console.log(person[name]); console.log(Object.keys(person)); console.log(JSON.stringify(person)); console.log(Object.getOwnPropertySymbols(person)); ``` ### Iterator ```js const set = new Set(["foo", "bar"]); for (const item of set) { console.log(item); } const map = new Map(); map.set("foo", "123"); map.set("bar", "456"); for (const [k, v] of map) { console.log(k, v); } const iterator = set[Symbol.iterator](); console.log(iterator.next()); const obj = { store: ["foo", "bar", "baz"], [Symbol.iterator]: function () { let index = 0; const self = this; return { next: function () { return { value: self.store[index], done: index++ >= self.store.length, }; }, }; }, }; for (const item of obj) { console.log(item); } const todos = { life: ["吃饭", "睡觉", "打豆豆"], learn: ["语文", "数学", "物理"], work: ["java", "javaScript", "vue"], each: function (callback) { const all = [].concat(this.life, this.learn, this.work); for (const item of all) { callback(item); } }, }; todos.each(function (item) { console.log(item); }); const todos = { life:['吃饭','睡觉','打豆豆'], learn:['语文','数学','英语'], work:['扫地','端茶','倒水'], [Symbol.iterator]:function * () { const all = [...this.life, ...this.learn, ...this.work] for(const item of all) { yield item } } } for (const item of todos) { console.log(item); } ``` ### other ```js const arr = ['foo', 'bar', 'baz', NaN, false, 1]; console.log(arr.indexOf(NaN)); console.log(arr.indexOf('foo')); console.log(arr.includes(NaN)); console.log(Math.pow(2,10)); console.log(2 ** 10); const obj = {foo:'foo', bar: 'bar'}; console.log(Object.values(obj)); console.log(Object.entries(obj)); for (const [key,value] of Object.entries(obj)) { console.log(key, value); } console.log(new Map(Object.entries(obj))); const p1 = { firstname: 'li', lastname: 'ming', get fullname() { return this.firstname + this.lastname; } }; const p2 = Object.assign({}, p1); p2.firstname = 'liu'; // li ming ? console.log(p2.fullname); const des = Object.getOwnPropertyDescriptors(p1); const p3 = Object.defineProperties({}, des); p3.firstname = 'liu'; // liu ming console.log(p3.fullname); const books = { html:5, javascript:112, css:16, } for (const[name, count] of Object.entries(books)) { console.log(`${name.padEnd(16,'-')} | ${count.toString().padStart(3,'0')}`) } ```