René's URL Explorer Experiment


Title: 百度&阿里编程题:模拟实现一个 localStorage · Issue #108 · sisterAn/JavaScript-Algorithms · GitHub

Open Graph Title: 百度&阿里编程题:模拟实现一个 localStorage · Issue #108 · sisterAn/JavaScript-Algorithms

X Title: 百度&阿里编程题:模拟实现一个 localStorage · Issue #108 · sisterAn/JavaScript-Algorithms

Description: 可参考来源: 第 103 题:模拟实现一个 localStorage 一、localStorage 特性 1. Storage Interface getItem(key) setItem(key, value) removeItem(key) clear() key(index) length:readonly 2. 持久化存储 刷新、关闭页面依然存在 3. localStorage 中的键值对总是以字符串的形式存储 key.toString() value.toSt...

Open Graph Description: 可参考来源: 第 103 题:模拟实现一个 localStorage 一、localStorage 特性 1. Storage Interface getItem(key) setItem(key, value) removeItem(key) clear() key(index) length:readonly 2. 持久化存储 刷新、关闭页面依然存在 3. localStorage 中的...

X Description: 可参考来源: 第 103 题:模拟实现一个 localStorage 一、localStorage 特性 1. Storage Interface getItem(key) setItem(key, value) removeItem(key) clear() key(index) length:readonly 2. 持久化存储 刷新、关闭页面依然存在 3. localStorage 中的...

Opengraph URL: https://github.com/sisterAn/JavaScript-Algorithms/issues/108

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"百度\u0026阿里编程题:模拟实现一个 localStorage","articleBody":"可参考来源:\r\n[第 103 题:模拟实现一个 localStorage](https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/166)\r\n\r\n### 一、localStorage 特性\r\n\r\n**1. Storage Interface**\r\n- getItem(key)\r\n- setItem(key, value)\r\n- removeItem(key)\r\n- clear()\r\n- key(index)\r\n- length:readonly\r\n\r\n**2. 持久化存储**\r\n刷新、关闭页面依然存在\r\n\r\n**3. localStorage 中的键值对总是以字符串的形式存储 key.toString() value.toString()**\r\n\r\n```js\r\nlocalStorage.setItem('pzijun', 100)\r\nlocalStorage.getItem('pzijun')\r\n// \"100\"\r\n\r\nvar a = {a: 1}\r\nlocalStorage.setItem(a, {a:1})\r\nlocalStorage.getItem(a)\r\n// \"[object Object]\"\r\n\r\nvar b = {}\r\nlocalStorage.setItem(b, '11')\r\nlocalStorage.getItem(b)\r\n// \"11\"\r\nlocalStorage.getItem(a)\r\n// \"11\"\r\n```\r\n\r\n**4. same-origin rules 特定于页面的协议,以及隐身模式等的区别**\r\n\r\n当浏览器进入隐身模式(private browsing mode)的时候,会创建一个新的、临时的、空的数据库,用以存储本地数据(local storage data)。当浏览器关闭时,里面的所有数据都将被丢弃。\r\n\r\n\r\n### 二、用 cookie 模拟 localStorage\r\n\r\n\u003e 参考 [https://developer.mozilla.org/zh-CN/docs/Web/API/Document/cookie](https://developer.mozilla.org/zh-CN/docs/Web/API/Document/cookie)\r\n\r\n```javascript\r\nwindow.LS = {\r\n  getItem: function (sKey) {\r\n    if (!sKey || !this.hasOwnProperty(sKey)) {\r\n      return null;\r\n    }\r\n    return unescape(document.cookie.replace(new RegExp(\"(?:^|.*;\\\\s*)\" + escape(sKey).replace(/[\\-\\.\\+\\*]/g, \"\\\\$\u0026\") + \"\\\\s*\\\\=\\\\s*((?:[^;](?!;))*[^;]?).*\"), \"$1\"));\r\n  },\r\n  key: function (nKeyId) {\r\n    return unescape(document.cookie.replace(/\\s*\\=(?:.(?!;))*$/, \"\").split(/\\s*\\=(?:[^;](?!;))*[^;]?;\\s*/)[nKeyId]);\r\n  },\r\n  setItem: function (sKey, sValue) {\r\n    if (!sKey) {\r\n      return;\r\n    }\r\n    document.cookie = escape(sKey) + \"=\" + escape(sValue) + \"; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/\";\r\n    this.length = document.cookie.match(/\\=/g).length;\r\n  },\r\n  length: 0,\r\n  removeItem: function (sKey) {\r\n    if (!sKey || !this.hasOwnProperty(sKey)) {\r\n      return;\r\n    }\r\n    document.cookie = escape(sKey) + \"=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/\";\r\n    this.length--;\r\n  },\r\n  hasOwnProperty: function (sKey) {\r\n    return (new RegExp(\"(?:^|;\\\\s*)\" + escape(sKey).replace(/[\\-\\.\\+\\*]/g, \"\\\\$\u0026\") + \"\\\\s*\\\\=\")).test(document.cookie);\r\n  }\r\n};\r\nwindow.LS.length = (document.cookie.match(/\\=/g) || window.LS).length;\r\n\r\nif (!window.localStorage) {\r\n  window.localStorage = window.LS;\r\n}\r\n```\r\n测试:\r\n```js\r\nLS.setItem('pzijun', 100)\r\nLS.getItem('pzijun')\r\n// \"100\"\r\n\r\nvar a = {\r\n  a: 1\r\n}\r\nLS.setItem(a, {\r\n  a: 1\r\n})\r\nLS.getItem(a)\r\n// \"[object Object]\"\r\n\r\nvar b = {}\r\nLS.setItem(b, '11')\r\nLS.getItem(b)\r\n// \"11\"\r\nLS.getItem(a)\r\n// \"11\"\r\n```\r\n\r\n另外还有容量控制、异常处理等,这里不再讨论,感兴趣的可以自己尝试实现下\r\n\r\n### 三、扩展 localStorage 支持 expires 过期时间\r\n```javascript\r\n(function () {\r\n  var getItem = localStorage.getItem.bind(localStorage)\r\n  var setItem = localStorage.setItem.bind(localStorage)\r\n  var removeItem = localStorage.removeItem.bind(localStorage)\r\n  localStorage.getItem = function (keyName) {\r\n    var expires = getItem(keyName + '_expires')\r\n    if (expires \u0026\u0026 new Date() \u003e new Date(Number(expires))) {\r\n      removeItem(keyName)\r\n      removeItem(keyName + '_expires')\r\n    }\r\n    return getItem(keyName)\r\n  }\r\n  localStorage.setItem = function (keyName, keyValue, expires) {\r\n    if (typeof expires !== 'undefined') {\r\n      var expiresDate = new Date(expires).valueOf()\r\n      setItem(keyName + '_expires', expiresDate)\r\n    }\r\n    return setItem(keyName, keyValue)\r\n  }\r\n})()\r\n```\r\n\r\n测试:\r\n```javascript\r\nlocalStorage.setItem('key', 'value', new Date() + 10000) // 10 秒钟后过期\r\nlocalStorage.getItem('key')\r\n```","author":{"url":"https://github.com/sisterAn","@type":"Person","name":"sisterAn"},"datePublished":"2020-09-16T00:05:21.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/108/JavaScript-Algorithms/issues/108"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:24a0efad-3a4f-3f5b-0e74-7454f8a6d10d
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id810C:1254A9:B16421:F55878:696A8626
html-safe-noncea8d4a1714fc5991e3415e86373b89c57ec5c88ea0008dece38dfc3dacb4bb13e
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MTBDOjEyNTRBOTpCMTY0MjE6RjU1ODc4OjY5NkE4NjI2IiwidmlzaXRvcl9pZCI6IjI2ODk3OTgxNTc5MzgzNjE4OTQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac4833fbbdd342dfa40b3928ac41f213d496ea04a1e2a3dd9c03108eadc03c91cd
hovercard-subject-tagissue:702351033
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/sisterAn/JavaScript-Algorithms/108/issue_layout
twitter:imagehttps://opengraph.githubassets.com/d289f631f9566e83c7fd45cfbb9ef765f8ff881c63a1d65b98daa007bb86fca0/sisterAn/JavaScript-Algorithms/issues/108
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/d289f631f9566e83c7fd45cfbb9ef765f8ff881c63a1d65b98daa007bb86fca0/sisterAn/JavaScript-Algorithms/issues/108
og:image:alt可参考来源: 第 103 题:模拟实现一个 localStorage 一、localStorage 特性 1. Storage Interface getItem(key) setItem(key, value) removeItem(key) clear() key(index) length:readonly 2. 持久化存储 刷新、关闭页面依然存在 3. localStorage 中的...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamesisterAn
hostnamegithub.com
expected-hostnamegithub.com
None913560fa317c3c5a71e34f9b19253c9f09d02b4b958a86c2a56f4c8541116377
turbo-cache-controlno-preview
go-importgithub.com/sisterAn/JavaScript-Algorithms git https://github.com/sisterAn/JavaScript-Algorithms.git
octolytics-dimension-user_id19721451
octolytics-dimension-user_loginsisterAn
octolytics-dimension-repository_id252061924
octolytics-dimension-repository_nwosisterAn/JavaScript-Algorithms
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id252061924
octolytics-dimension-repository_network_root_nwosisterAn/JavaScript-Algorithms
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release5998c30593994bf2589055aef7b22d368a499367
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/sisterAn/JavaScript-Algorithms/issues/108#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FsisterAn%2FJavaScript-Algorithms%2Fissues%2F108
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FsisterAn%2FJavaScript-Algorithms%2Fissues%2F108
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=sisterAn%2FJavaScript-Algorithms
Reloadhttps://github.com/sisterAn/JavaScript-Algorithms/issues/108
Reloadhttps://github.com/sisterAn/JavaScript-Algorithms/issues/108
Reloadhttps://github.com/sisterAn/JavaScript-Algorithms/issues/108
sisterAn https://github.com/sisterAn
JavaScript-Algorithmshttps://github.com/sisterAn/JavaScript-Algorithms
Notifications https://github.com/login?return_to=%2FsisterAn%2FJavaScript-Algorithms
Fork 649 https://github.com/login?return_to=%2FsisterAn%2FJavaScript-Algorithms
Star 5.7k https://github.com/login?return_to=%2FsisterAn%2FJavaScript-Algorithms
Code https://github.com/sisterAn/JavaScript-Algorithms
Issues 158 https://github.com/sisterAn/JavaScript-Algorithms/issues
Pull requests 0 https://github.com/sisterAn/JavaScript-Algorithms/pulls
Actions https://github.com/sisterAn/JavaScript-Algorithms/actions
Projects 0 https://github.com/sisterAn/JavaScript-Algorithms/projects
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/sisterAn/JavaScript-Algorithms/security
Please reload this pagehttps://github.com/sisterAn/JavaScript-Algorithms/issues/108
Insights https://github.com/sisterAn/JavaScript-Algorithms/pulse
Code https://github.com/sisterAn/JavaScript-Algorithms
Issues https://github.com/sisterAn/JavaScript-Algorithms/issues
Pull requests https://github.com/sisterAn/JavaScript-Algorithms/pulls
Actions https://github.com/sisterAn/JavaScript-Algorithms/actions
Projects https://github.com/sisterAn/JavaScript-Algorithms/projects
Security https://github.com/sisterAn/JavaScript-Algorithms/security
Insights https://github.com/sisterAn/JavaScript-Algorithms/pulse
New issuehttps://github.com/login?return_to=https://github.com/sisterAn/JavaScript-Algorithms/issues/108
New issuehttps://github.com/login?return_to=https://github.com/sisterAn/JavaScript-Algorithms/issues/108
百度&阿里编程题:模拟实现一个 localStoragehttps://github.com/sisterAn/JavaScript-Algorithms/issues/108#top
百度https://github.com/sisterAn/JavaScript-Algorithms/issues?q=state%3Aopen%20label%3A%22%E7%99%BE%E5%BA%A6%22
编程题https://github.com/sisterAn/JavaScript-Algorithms/issues?q=state%3Aopen%20label%3A%22%E7%BC%96%E7%A8%8B%E9%A2%98%22
阿里https://github.com/sisterAn/JavaScript-Algorithms/issues?q=state%3Aopen%20label%3A%22%E9%98%BF%E9%87%8C%22
https://github.com/sisterAn
https://github.com/sisterAn
sisterAnhttps://github.com/sisterAn
on Sep 16, 2020https://github.com/sisterAn/JavaScript-Algorithms/issues/108#issue-702351033
第 103 题:模拟实现一个 localStoragehttps://github.com/Advanced-Frontend/Daily-Interview-Question/issues/166
https://developer.mozilla.org/zh-CN/docs/Web/API/Document/cookiehttps://developer.mozilla.org/zh-CN/docs/Web/API/Document/cookie
百度https://github.com/sisterAn/JavaScript-Algorithms/issues?q=state%3Aopen%20label%3A%22%E7%99%BE%E5%BA%A6%22
编程题https://github.com/sisterAn/JavaScript-Algorithms/issues?q=state%3Aopen%20label%3A%22%E7%BC%96%E7%A8%8B%E9%A2%98%22
阿里https://github.com/sisterAn/JavaScript-Algorithms/issues?q=state%3Aopen%20label%3A%22%E9%98%BF%E9%87%8C%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.