Guest User

Untitled

a guest
Dec 17th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. // Set data structure
  2. let s = new Set();
  3. s.add(1);
  4. s.add(2);
  5. s.add(3);
  6. console.log(s.size);
  7.  
  8. console.log(s.has(2), s.has(10));
  9.  
  10. for (let k of s.values()) {
  11. console.log(k);
  12. }
  13.  
  14. // Map data structure
  15. let m = new Map();
  16. m.set('hello', 42);
  17. m.set('alex', 29);
  18. m.set('eunice', 30);
  19. console.log(m);
  20.  
  21. for (let [k, v] of m.entries()) {
  22. console.log(`key: ${k}, value: ${v}`)
  23. }
  24.  
  25. let isMarked = new WeakSet()
  26. let attachedData = new WeakMap()
  27.  
  28. // weakSet & weakMap
  29. class Node {
  30. constructor (id) { this.id = id }
  31. mark () { isMarked.add(this) }
  32. unmark () { isMarked.delete(this) }
  33. marked () { return isMarked.has(this) }
  34. set data (data) { attachedData.set(this, data) }
  35. get data () { return attachedData.get(this) }
  36. }
  37.  
  38. let foo = new Node("foo")
  39.  
  40. JSON.stringify(foo) === '{"id":"foo"}'
  41. foo.mark()
  42. foo.data = "bar"
  43. foo.data === "bar"
  44. JSON.stringify(foo) === '{"id":"foo"}'
  45.  
  46. isMarked.has(foo) === true
  47. attachedData.has(foo) === true
  48. foo = null /* remove only reference to foo */
  49. attachedData.has(foo) === false
  50. isMarked.has(foo) === false
  51.  
  52. // type arrays
  53. class Example {
  54. constructor (buffer = new ArrayBuffer(24)) {
  55. this.buffer = buffer
  56. }
  57. set buffer (buffer) {
  58. this._buffer = buffer
  59. this._id = new Uint32Array (this._buffer, 0, 1)
  60. this._username = new Uint8Array (this._buffer, 4, 16)
  61. this._amountDue = new Float32Array(this._buffer, 20, 1)
  62. }
  63. get buffer () { return this._buffer }
  64. set id (v) { this._id[0] = v }
  65. get id () { return this._id[0] }
  66. set username (v) { this._username[0] = v }
  67. get username () { return this._username[0] }
  68. set amountDue (v) { this._amountDue[0] = v }
  69. get amountDue () { return this._amountDue[0] }
  70. }
  71.  
  72. let example = new Example()
  73. example.id = 7
  74. example.username = "John Doe"
  75. example.amountDue = 42.0
  76.  
  77. // New built-in methods
  78. Object.assign({}, { a: 'b' }, { a: 'c' });
  79. [1, 2, 3].find(x => x >= 3);
  80. [1, 2, 3].findIndex(x => x >= 3)
  81. '*'.repeat(16);
  82. 'hello_world'.startsWith('ello', 1);
  83. 'hello_world'.endsWith('world', 11);
  84. Number.isNaN(24);
  85. Number.isFinite(123);
  86. Number.isFinite(NaN);
  87. Number.isFinite(Infinity);
  88. Number.isSafeInteger(42) === true
  89. Number.isSafeInteger(9007199254740992) === false
  90. Number.EPSILON;
  91. console.log(`
  92. all numbers are double-precision 64-bit IEEE 754 values
  93. .2 + .1 = ${0.2 + 0.1}
  94. `);
  95. console.log(0.1 + 0.2 === 0.3) // false
  96. console.log(Math.abs((0.1 + 0.2) - 0.3) < Number.EPSILON);
  97.  
  98. Math.trunc(42.7);
  99. Math.trunc(0.1);
  100. Math.sign(7);
  101. Math.sign(-2);
  102. Math.sign(0);
  103. Math.sign(-0);
  104. Math.sign(NaN);
  105.  
  106. // Promises
  107. const promFactory = {
  108. *[Symbol.iterator]() {
  109. let count = 0;
  110. for(;;) {
  111. count += 1;
  112. yield new Promise((resolve, reject) => {
  113. if (count < 100) {
  114. resolve(count);
  115. } else {
  116. reject(`count is ${count}. count exceeded 100!`);
  117. }
  118. });
  119. }
  120. }
  121. }
  122.  
  123. // for (p of promFactory) {
  124. // let ex = false;
  125. // p.then((v) => {
  126. // console.log(`value: ${v}`);
  127. // }).catch(err => {
  128. // console.log(`error: ${err}`);
  129. // ex = true;
  130. // });
  131. // if (ex) break;
  132. // }
  133.  
  134. // proxying
  135. let target = {
  136. foo: "Welcome, foo"
  137. }
  138. let proxy = new Proxy(target, {
  139. get (receiver, name) {
  140. return name in receiver ? receiver[name] : `Hello, ${name}`
  141. }
  142. })
  143. proxy.foo === "Welcome, foo"
  144. proxy.world === "Hello, world"
Add Comment
Please, Sign In to add comment