From b68f3005c406300e6d32083f97a635630e92168c Mon Sep 17 00:00:00 2001 From: YangJ0605 <1442122744@qq.com> Date: Wed, 24 Jun 2020 18:03:58 +0800 Subject: [PATCH] learn es6 --- ES6/symbol.html | 35 ++++++++++ for0f.js | 13 ++++ index.html | 1 + "set\344\270\216map/map.js" | 25 ++++++- symbol.js | 68 +++++++++++++++++++ ...256\200\345\215\225\347\232\204promise.js" | 30 ++++++++ 6 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 ES6/symbol.html create mode 100644 for0f.js create mode 100644 symbol.js diff --git a/ES6/symbol.html b/ES6/symbol.html new file mode 100644 index 0000000..491349d --- /dev/null +++ b/ES6/symbol.html @@ -0,0 +1,35 @@ + + + + + + 模拟实现Symbol + + +
+

Symbol 函数前不能使用 new 命令,否则会报错。这是因为生成的 Symbol 是一个原始类型的值,不是对象。

+

Symbol 值不能与其他类型的值进行运算,会报错。

+
+ + + \ No newline at end of file diff --git a/for0f.js b/for0f.js new file mode 100644 index 0000000..1ddd701 --- /dev/null +++ b/for0f.js @@ -0,0 +1,13 @@ +function forOf(obj, cb) { + let iterable, result + if (typeof obj[Symbol.iterator] !== 'function') { + throw new TypeError(obj + ' is not iterable') + } + if (typeof cb !== 'function') throw new TypeError('cb must bu calllable') + iterable = obj[Symbol.iterator]() + result = iterable.next() + while (!result.done) { + cb(result.value) + result = iterable.next() + } +} \ No newline at end of file diff --git a/index.html b/index.html index 2dde5c0..2079a48 100644 --- a/index.html +++ b/index.html @@ -8,6 +8,7 @@
+
diff --git "a/set\344\270\216map/map.js" "b/set\344\270\216map/map.js" index 3513e78..edc0eb2 100644 --- "a/set\344\270\216map/map.js" +++ "b/set\344\270\216map/map.js" @@ -4,4 +4,27 @@ const o = { name: 'object' } m.set(o, 1) m.get(o) -console.log(m.has(o)) \ No newline at end of file +console.log(m.has(o)) + + +// WeakMap 只接受对象作为键名 +// WeakMap 的键名所引用的对象是弱引用 弱引用就是能够被垃圾回收的引用 + + +const privateData = new WeakMap(); + +class Person { + constructor(name, age) { + privateData.set(this, { name: name, age: age }); + } + + getName() { + return privateData.get(this).name; + } + + getAge() { + return privateData.get(this).age; + } +} + +export default Person; \ No newline at end of file diff --git a/symbol.js b/symbol.js new file mode 100644 index 0000000..dd76b62 --- /dev/null +++ b/symbol.js @@ -0,0 +1,68 @@ +var generateName = (function () { + var endfix = 0 + return function (desc) { + endfix++ + return `@@${desc}_${endfix}` + } +})() + +var SymbolPolyfill = function Symbol(desc) { + if (this instanceof SymbolPolyfill) throw new TypeError('Symbom is not a function') + var descString = desc === undefined ? undefined : String(desc) + var symbol = Object.create({ + toString: function () { + return this.__Name__ + }, + valueOf: function () { + throw new Error('Cannot convert a Symbol value') + } + }) + Object.defineProperties(symbol, { + '__Description__': { + value: descString, + writable: false, + enumerable: false, + configurable: false + }, + '__Name__': { + value: generateName(descString), + writable: false, + enumerable: false, + configurable: false + } + }) + return symbol +} + +var forMap = {} +Object.defineProperties(SymbolPolyfill, { + 'for': { + value: function (desc) { + var descString = desc === undefined ? undefined : String(desc) + return forMap[descString] ? forMap[descString] : forMap[descString] = SymbolPolyfill(descString) + }, + writable: true, + enumerable: false, + configurable: true + }, + 'keyFor': { + value: function (symbol) { + for (let key in forMap) { + if(forMap[key] === symbol) return key + } + }, + writable: true, + enumerable: false, + configurable: true + } +}) + +var a = SymbolPolyfill('a') +var b = SymbolPolyfill('a') +console.log(a === b) +console.log(a.toString()) +var obj = {} +obj[a] = '1111' +obj[b] = '222' +console.log(obj) +// console.log(a + 1) \ No newline at end of file diff --git "a/\345\256\236\347\216\260\344\270\200\344\270\252\347\256\200\345\215\225\347\232\204promise.js" "b/\345\256\236\347\216\260\344\270\200\344\270\252\347\256\200\345\215\225\347\232\204promise.js" index ae21c59..0966782 100644 --- "a/\345\256\236\347\216\260\344\270\200\344\270\252\347\256\200\345\215\225\347\232\204promise.js" +++ "b/\345\256\236\347\216\260\344\270\200\344\270\252\347\256\200\345\215\225\347\232\204promise.js" @@ -174,3 +174,33 @@ window.Promise = Promise }(window) + +function red() { + console.log('red') +} +function green() { + console.log('green') +} +function yellow() { + console.log('yellow') +} + +function light(cb, timer) { + return new Promise((resolve, reject) => { + setTimeout(() => { + cb() + resolve() + }, timer); + }) +} + +function change() { + light(red, 3000). + then(() => { + return light(green, 2000) + }).then(() => { + return light(yellow, 2000) + }).then(() => { + change() + }) +} \ No newline at end of file