
ES6
importmap
导入映射表 (Import Maps)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <script type="importmap"> { "imports": { "vue": "https://unpkg.com/vue@3/dist/vue.esm-browser.js" } } </script>
<div id="app">{{ message }}</div>
<script type="module"> import { createApp, ref } from 'vue'
createApp({ setup() { const message = ref('Hello Vue!') return { message } } }).mount('#app') </script>
|
for await of
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| function TimeOut(time) { return new Promise(function (resolve, reject) { setTimeout(function () { resolve(time) }, time) }) } async function test() { let arr = [TimeOut(2000), TimeOut(1000), TimeOut(3000)] for await (let item of arr) { console.log(Date.now(), item) } } test()
|
class
getter/setter
在“类”的内部可以使用get和set关键字,对某个属性设置存值函数和取值函数,拦截该属性的存取行为。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| Object.defineProperty(obj,"name",{ get:function(){ console.log("正在访问name"); return this._name; }, set:function(val){ console.log("正在修改name"); this._name = val; } });
obj.name = 10;
class Person{ constructor(){ this._name = ''; } get name(){ console.log("正在访问name"); return `我的名字是${this._name}`; } set name(val){ console.log("正在修改name"); this._name = val; } }
const person = new Person(); person.name = "歌王"; console.log(person.name);
|