弓长良 發表於 2022-6-7 11:57:00

js iterator protocol & next() All In One

<h1 id="js-iterator-protocol--next-all-in-one">js iterator protocol &amp; next() All In One</h1>
<blockquote>
<p><code>.next()</code></p>
</blockquote>
<pre><code class="language-js">// Satisfies both the Iterator Protocol and Iterable
const myIterator = {
    next: function() {
      // ...
    },
    : function() { return this; }
};
</code></pre>
<p><img src="https://img2022.cnblogs.com/blog/740516/202206/740516-20220607115549439-1802628900.png" alt="" loading="lazy"></p>
<h2 id="iterator-protocol">iterator protocol</h2>
<blockquote>
<p>迭代器协议</p>
</blockquote>
<pre><code class="language-js">// .next()

// .next().value

// .next().done

</code></pre>
<p>https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol</p>
<h2 id="iterators--generators">iterators &amp; generators</h2>
<blockquote>
<p>迭代器、生成器</p>
</blockquote>
<pre><code class="language-js">
</code></pre>
<pre><code class="language-js">
</code></pre>
<p>https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators</p>
<h2 id="demo">demo</h2>
<pre><code class="language-js">
"use strict";

/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-11-26
* @modified
*
* @description 146. LRU Cache
* @description 146. LRU Cache
* @difficulty Medium
* @complexity O(n)
* @time O(n)
* @augments
* @example
* @link https://leetcode.com/problems/lru-cache/
* @link https://leetcode-cn.com/problems/lru-cache/
* @link https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU 最近最少使用(LRU)缓存算法
* @solutions
*
* @best_solutions
*
*/

const log = console.log;

/**
* @param {number} capacity
*/
var LRUCache = function(capacity) {
this.capacity = capacity;
this.map = new Map();
};

/**
* @param {number} key
* @return {number}
*/
LRUCache.prototype.get = function(key) {
// ✅ 优先处理代码少的逻辑,错误优先
if(!this.map.has(key)) {
    return -1;
} else {
    const value = this.map.get(key)
    // 读取后,更新顺序
    this.put(key, value);
    return value;
}
// if(this.map.has(key)) {
//   const value = this.map.get(key)
//   // 读取后,更新顺序
//   this.put(key, value);
//   return value;
// } else {
//   return -1;
// }
};

/**
* @param {number} key
* @param {number} value
* @return {void}
*/
LRUCache.prototype.put = function(key, value) {
// 替换式更新,先删除
if(this.map.has(key)) {
    this.map.delete(key);
}
// 达到容量限制,先删除最近最少使用的 map 第一个元素
if(this.map.size === this.capacity) {
    const oldKey = this.map.keys().next().value;
    this.map.delete(oldKey);
}
// 再重新添加
this.map.set(key, value);
};

/**
* Your LRUCache object will be instantiated and called as such:
* var obj = new LRUCache(capacity)
* var param_1 = obj.get(key)
* obj.put(key,value)
*/



/*

lRUCache = new LRUCache(2);

lRUCache.put(1, 1);
// cache is {1=1}

lRUCache.put(2, 2);
// cache is {1=1, 2=2}

lRUCache.get(1);
// return 1

lRUCache.put(3, 3);
// LRU key was 2, evicts key 2, cache is {1=1, 3=3}

lRUCache.get(2);
// returns -1 (not found)

lRUCache.put(4, 4);
// LRU key was 1, evicts key 1, cache is {4=4, 3=3}

lRUCache.get(1);
// return -1 (not found)

lRUCache.get(3);
// return 3

lRUCache.get(4);
// return 4


*/


</code></pre>
<p><img src="https://img2022.cnblogs.com/blog/740516/202206/740516-20220607122343942-529774300.png" alt="" loading="lazy"></p>
<p>https://leetcode.com/problems/lru-cache/</p>
<p>https://leetcode.cn/problems/lru-cache/</p>
<h2 id="refs">refs</h2>
<p>https://www.cnblogs.com/xgqfrms/p/16344654.html</p>
<p>https://leetcode.com/problems/lru-cache/discuss/2074758/Simple-Javascript-solution-or-Easy-to-understand</p>
<hr>
<div>

</div>
<hr>
<blockquote style="display: flex; flex-flow: column; align-items: center; justify-content: center; text-align: center; border: none">
<h3><strong><span style="font-size: 16pt; color: rgba(0, 255, 0, 1)">©xgqfrms 2012-<span data-uid="copyright-aside">2020</span></span></strong>
<p><span style="font-size: 18pt; color: rgba(0, 255, 0, 1)"><strong>www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!</strong></span></p>
<p><span style="font-size: 18pt; color: rgba(0, 255, 0, 1)"><strong>原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!</strong></span></p>
</h3></blockquote>
<hr>


</div>
<div id="MySignature" role="contentinfo">
    <div style="display: flex; flex-flow: column nowrap; align-items: center; justify-content: center;">
<p>本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16351282.html</p>
<p style="color: red; font-size: 23px; margin-top: 5px; margin-botom: 5px;">未经授权禁止转载,违者必究!</P>
</div>
<hr/><br><br>
来源:https://www.cnblogs.com/xgqfrms/p/16351282.html
頁: [1]
查看完整版本: js iterator protocol & next() All In One