1 Star 0 Fork 0

weihua / interview

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
js.js 970.31 KB
一键复制 编辑 原始数据 按行查看 历史
liuweihua 提交于 2020-08-22 16:06 . dd
var arr = [{"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4"], "date": "Fri, 19 Jun 2020 16:21:40 GMT", "favorite_num": 30, "qid": 16, "qtype": "short_answer", "short_answer": {"analysis": "\u94fe\u5f0f\u8c03\u7528\u7684\u6838\u5fc3\u5c31\u5728\u4e8e\u8c03\u7528\u5b8c\u7684\u65b9\u6cd5\u5c06\u81ea\u8eab\u5b9e\u4f8b\u8fd4\u56de\n1\uff09\u793a\u4f8b\u4e00\n```js\nfunction Class1() {\n console.log('\u521d\u59cb\u5316')\n}\nClass1.prototype.method = function(param) {\n console.log(param)\n return this\n}\nlet cl = new Class1()\n//\u7531\u4e8enew \u5728\u5b9e\u4f8b\u5316\u7684\u65f6\u5019this\u4f1a\u6307\u5411\u521b\u5efa\u7684\u5bf9\u8c61\uff0c \u6240\u4ee5this.method\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u5728\u539f\u578b\u94fe\u4e2d\u627e\u5230\u3002\ncl.method('\u7b2c\u4e00\u6b21\u8c03\u7528').method('\u7b2c\u4e8c\u6b21\u94fe\u5f0f\u8c03\u7528').method('\u7b2c\u4e09\u6b21\u94fe\u5f0f\u8c03\u7528')\n```\n2\uff09\u793a\u4f8b\u4e8c\n```js\nvar obj = {\n a: function() {\n console.log(\"a\");\n return this;\n },\n b: function() {\n console.log(\"b\");\n return this;\n },\n};\nobj.a().b();\n```\n3\uff09\u793a\u4f8b\u4e09\n```js\n// \u7c7b\nclass Math {\n constructor(value) {\n this.hasInit = true;\n this.value = value;\n if (!value) {\n this.value = 0;\n this.hasInit = false;\n }\n }\n add() {\n let args = [...arguments]\n let initValue = this.hasInit ? this.value : args.shift()\n const value = args.reduce((prev, curv) => prev + curv, initValue)\n return new Math(value)\n }\n minus() {\n let args = [...arguments]\n let initValue = this.hasInit ? this.value : args.shift()\n const value = args.reduce((prev, curv) => prev - curv, initValue)\n return new Math(value)\n }\n mul() {\n let args = [...arguments]\n let initValue = this.hasInit ? this.value : args.shift()\n const value = args.reduce((prev, curv) => prev * curv, initValue)\n return new Math(value)\n }\n divide() {\n let args = [...arguments]\n let initValue = this.hasInit ? this.value : args.shift()\n const value = args.reduce((prev, curv) => prev / (+curv ? curv : 1), initValue)\n return new Math(value)\n }\n}\n\nlet test = new Math()\nconst res = test.add(222, 333, 444).minus(333, 222).mul(3, 3).divide(2, 3)\nconsole.log(res.value)\n\n// \u539f\u578b\u94fe\nNumber.prototype.add = function() {\n let _that = this\n _that = [...arguments].reduce((prev, curv) => prev + curv, _that)\n return _that\n}\nNumber.prototype.minus = function() {\n let _that = this\n _that = [...arguments].reduce((prev, curv) => prev - curv, _that)\n return _that\n}\nNumber.prototype.mul = function() {\n let _that = this\n _that = [...arguments].reduce((prev, curv) => prev * curv, _that)\n return _that\n}\nNumber.prototype.divide = function() {\n let _that = this\n _that = [...arguments].reduce((prev, curv) => prev / (+curv ? curv : 1), _that)\n return _that\n}\nlet num = 0;\nlet newNum = num.add(222, 333, 444).minus(333, 222).mul(3, 3).divide(2, 3)\nconsole.log(newNum)\n```", "answer": "", "biz_type": 1, "qid": 16, "subject": "", "title": "\u5b9e\u73b0\u94fe\u5f0f\u8c03\u7528"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4"], "date": "Fri, 19 Jun 2020 16:21:40 GMT", "favorite_num": 30, "qid": 15, "qtype": "short_answer", "short_answer": {"analysis": "\u8003\u70b9\uff1a\u51fd\u6570\u67ef\u91cc\u5316\n\n\u51fd\u6570\u67ef\u91cc\u5316\u6982\u5ff5\uff1a \u67ef\u91cc\u5316\uff08Currying\uff09\u662f\u628a\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\u7684\u51fd\u6570\u8f6c\u53d8\u4e3a\u63a5\u53d7\u4e00\u4e2a\u5355\u4e00\u53c2\u6570\u7684\u51fd\u6570\uff0c\u5e76\u4e14\u8fd4\u56de\u63a5\u53d7\u4f59\u4e0b\u7684\u53c2\u6570\u4e14\u8fd4\u56de\u7ed3\u679c\u7684\u65b0\u51fd\u6570\u7684\u6280\u672f\u3002\n\n1\uff09\u7c97\u66b4\u7248\n\n```js\nfunction add (a) {\n return function (b) {\n return function (c) {\n return a + b + c;\n }\n }\n}\nconsole.log(add(1)(2)(3)); // 6\n```\n\n2\uff09\u67ef\u91cc\u5316\u89e3\u51b3\u65b9\u6848\n\n- \u53c2\u6570\u957f\u5ea6\u56fa\u5b9a\n\n```js\nconst curry = (fn) =>\n(judge = (...args) =>\n args.length === fn.length\n ? fn(...args)\n : (...arg) => judge(...args, ...arg));\nconst add = (a, b, c) => a + b + c;\nconst curryAdd = curry(add);\nconsole.log(curryAdd(1)(2)(3)); // 6\nconsole.log(curryAdd(1, 2)(3)); // 6\nconsole.log(curryAdd(1)(2, 3)); // 6\n```\n\n- \u53c2\u6570\u957f\u5ea6\u4e0d\u56fa\u5b9a\n\n```js\nfunction add (...args) {\n //\u6c42\u548c\n return args.reduce((a, b) => a + b)\n}\n\nfunction currying (fn) {\n let args = []\n return function temp (...newArgs) {\n if (newArgs.length) {\n args = [\n ...args,\n ...newArgs\n ]\n return temp\n } else {\n let val = fn.apply(this, args)\n args = [] //\u4fdd\u8bc1\u518d\u6b21\u8c03\u7528\u65f6\u6e05\u7a7a\n return val\n }\n }\n}\n\nlet addCurry = currying(add)\nconsole.log(addCurry(1)(2)(3)(4, 5)()) //15\nconsole.log(addCurry(1)(2)(3, 4, 5)()) //15\nconsole.log(addCurry(1)(2, 3, 4, 5)()) //15\n```\n", "answer": "", "biz_type": 1, "qid": 15, "subject": "", "title": "\u5b9e\u73b0 add(1)(2)(3)"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4"], "date": "Fri, 19 Jun 2020 16:21:40 GMT", "favorite_num": 20, "qid": 14, "qtype": "short_answer", "short_answer": {"analysis": "\u5728 js \u4e2d\u7ecf\u5e38\u4f1a\u51fa\u73b0\u5d4c\u5957\u8c03\u7528\u8fd9\u79cd\u60c5\u51b5\uff0c\u5982 a.b.c.d.e\uff0c\u4f46\u662f\u8fd9\u4e48\u5199\u5f88\u5bb9\u6613\u629b\u51fa\u5f02\u5e38\u3002\u4f60\u9700\u8981\u8fd9\u4e48\u5199 a && a.b && a.b.c && a.b.c.d && a.b.c.d.e\uff0c\u4f46\u662f\u663e\u5f97\u6709\u4e9b\u5570\u55e6\u4e0e\u5197\u957f\u4e86\u3002\u7279\u522b\u662f\u5728 graphql \u4e2d\uff0c\u8fd9\u79cd\u5d4c\u5957\u8c03\u7528\u66f4\u662f\u96be\u4ee5\u907f\u514d\u3002\n\u8fd9\u65f6\u5c31\u9700\u8981\u4e00\u4e2a get \u51fd\u6570\uff0c\u4f7f\u7528 get(a, 'b.c.d.e') \u7b80\u5355\u6e05\u6670\uff0c\u5e76\u4e14\u5bb9\u9519\u6027\u63d0\u9ad8\u4e86\u5f88\u591a\u3002\n\n1\uff09\u4ee3\u7801\u5b9e\u73b0\n```js\nfunction get(source, path, defaultValue = undefined) {\n // a[3].b -> a.3.b -> [a,3,b]\n // path \u4e2d\u4e5f\u53ef\u80fd\u662f\u6570\u7ec4\u7684\u8def\u5f84\uff0c\u5168\u90e8\u8f6c\u5316\u6210 . \u8fd0\u7b97\u7b26\u5e76\u7ec4\u6210\u6570\u7ec4\n const paths = path.replace(/\\[(\\d+)\\]/g, \".$1\").split(\".\");\n let result = source;\n for (const p of paths) {\n // \u6ce8\u610f null \u4e0e undefined \u53d6\u5c5e\u6027\u4f1a\u62a5\u9519\uff0c\u6240\u4ee5\u4f7f\u7528 Object \u5305\u88c5\u4e00\u4e0b\u3002\n result = Object(result)[p];\n if (result == undefined) {\n return defaultValue;\n }\n }\n return result;\n}\n// \u6d4b\u8bd5\u7528\u4f8b\nconsole.log(get({ a: null }, \"a.b.c\", 3)); // output: 3\nconsole.log(get({ a: undefined }, \"a\", 3)); // output: 3\nconsole.log(get({ a: null }, \"a\", 3)); // output: 3\nconsole.log(get({ a: [{ b: 1 }] }, \"a[0].b\", 3)); // output: 1\n```\n2\uff09\u4ee3\u7801\u5b9e\u73b0 \n\u4e0d\u8003\u8651\u6570\u7ec4\u7684\u60c5\u51b5\n```js\nconst _get = (object, keys, val) => {\n return keys.split(/\\./).reduce(\n (o, j)=>( (o || {})[j] ), \n object\n ) || val\n}\nconsole.log(get({ a: null }, \"a.b.c\", 3)); // output: 3\nconsole.log(get({ a: undefined }, \"a\", 3)); // output: 3\nconsole.log(get({ a: null }, \"a\", 3)); // output: 3\nconsole.log(get({ a: { b: 1 } }, \"a.b\", 3)); // output: 1\n```", "answer": "", "biz_type": 1, "qid": 14, "subject": "", "title": "\u5b9e\u73b0 lodash \u7684\\_.get"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4"], "date": "Fri, 19 Jun 2020 16:21:40 GMT", "favorite_num": 12, "qid": 30, "qtype": "short_answer", "short_answer": {"analysis": "```js\nconst negativeArray = els =>\n new Proxy(els, {\n get: (target, propKey, receiver) =>\n Reflect.get(\n target,\n +propKey < 0 ? String(target.length + +propKey) : propKey,\n receiver\n )\n });\nconst unicorn = negativeArray([\"\u4eac\", \"\u7a0b\", \"\u4e00\", \"\u706f\"]);\nunicorn[-1]; \n```", "answer": "", "biz_type": 1, "qid": 30, "subject": "", "title": "\u624b\u5199\u7528 ES6proxy \u5982\u4f55\u5b9e\u73b0 arr[-1] \u7684\u8bbf\u95ee"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4", "\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:21:40 GMT", "favorite_num": 22, "qid": 28, "qtype": "short_answer", "short_answer": {"analysis": "```js\n// \u53d1\u5e03\u8ba2\u9605\u4e2d\u5fc3, on-\u8ba2\u9605, off\u53d6\u6d88\u8ba2\u9605, emit\u53d1\u5e03, \u5185\u90e8\u9700\u8981\u4e00\u4e2a\u5355\u72ec\u4e8b\u4ef6\u4e2d\u5fc3caches\u8fdb\u884c\u5b58\u50a8;\n\ninterface CacheProps {\n [key: string]: Array<((data?: unknown) => void)>;\n}\n\nclass Observer {\n\n private caches: CacheProps = {}; // \u4e8b\u4ef6\u4e2d\u5fc3\n\n on (eventName: string, fn: (data?: unknown) => void){ // eventName\u4e8b\u4ef6\u540d-\u72ec\u4e00\u65e0\u4e8c, fn\u8ba2\u9605\u540e\u6267\u884c\u7684\u81ea\u5b9a\u4e49\u884c\u4e3a\n this.caches[eventName] = this.caches[eventName] || [];\n this.caches[eventName].push(fn);\n }\n\n emit (eventName: string, data?: unknown) { // \u53d1\u5e03 => \u5c06\u8ba2\u9605\u7684\u4e8b\u4ef6\u8fdb\u884c\u7edf\u4e00\u6267\u884c\n if (this.caches[eventName]) {\n this.caches[eventName].forEach((fn: (data?: unknown) => void) => fn(data));\n }\n }\n\n off (eventName: string, fn?: (data?: unknown) => void) { // \u53d6\u6d88\u8ba2\u9605 => \u82e5fn\u4e0d\u4f20, \u76f4\u63a5\u53d6\u6d88\u8be5\u4e8b\u4ef6\u6240\u6709\u8ba2\u9605\u4fe1\u606f\n if (this.caches[eventName]) {\n const newCaches = fn ? this.caches[eventName].filter(e => e !== fn) : [];\n this.caches[eventName] = newCaches;\n }\n }\n \n}\n```", "answer": "", "biz_type": 1, "qid": 28, "subject": "", "title": "\u624b\u5199\u53d1\u5e03\u8ba2\u9605"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4"], "date": "Fri, 19 Jun 2020 16:21:40 GMT", "favorite_num": 19, "qid": 29, "qtype": "short_answer", "short_answer": {"analysis": "```js\n// \u4f8b\u5982\u5c06 input \u8f6c\u6210output\u7684\u5f62\u5f0f\nlet input = [\n {\n id: 1, val: '\u5b66\u6821', parentId: null\n }, {\n id: 2, val: '\u73ed\u7ea71', parentId: 1\n }, {\n id: 3, val: '\u73ed\u7ea72', parentId: 1\n }, {\n id: 4, val: '\u5b66\u751f1', parentId: 2\n }, {\n id: 5, val: '\u5b66\u751f2', parentId: 3\n }, {\n id: 6, val: '\u5b66\u751f3', parentId: 3\n },\n]\n\nlet output = {\n id: 1,\n val: '\u5b66\u6821',\n children: [{\n id: 2,\n val: '\u73ed\u7ea71',\n children: [\n {\n id: 4,\n val: '\u5b66\u751f1',\n children: []\n },\n {\n id: 5,\n val: '\u5b66\u751f2',\n children: []\n }\n ]\n }, {\n id: 3,\n val: '\u73ed\u7ea72',\n children: [{\n id: 6,\n val: '\u5b66\u751f3',\n children: []\n }]\n }]\n}\n```\n\n```js\n// \u4ee3\u7801\u5b9e\u73b0\nfunction arrayToTree(array) {\n let root = array[0]\n array.shift()\n let tree = {\n id: root.id,\n val: root.val,\n children: array.length > 0 ? toTree(root.id, array) : []\n }\n return tree;\n}\n\nfunction toTree(parenId, array) {\n let children = []\n let len = array.length\n for (let i = 0; i < len; i++) {\n let node = array[i]\n if (node.parentId === parenId) {\n children.push({\n id: node.id,\n val: node.val,\n children: toTree(node.id, array)\n })\n }\n }\n return children\n}\n\nconsole.log(arrayToTree(input))\n```", "answer": "", "biz_type": 1, "qid": 29, "subject": "", "title": "\u624b\u5199\u6570\u7ec4\u8f6c\u6811"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4", "\u864e\u6251", "\u6316\u8d22", "58", "\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:21:40 GMT", "favorite_num": 140, "qid": 9, "qtype": "short_answer", "short_answer": {"analysis": "1\uff09\u9632\u6296\n- \u539f\u7406\uff1a\u5728\u4e8b\u4ef6\u88ab\u89e6\u53d1n\u79d2\u540e\u518d\u6267\u884c\u56de\u8c03\uff0c\u5982\u679c\u5728\u8fd9n\u79d2\u5185\u53c8\u88ab\u89e6\u53d1\uff0c\u5219\u91cd\u65b0\u8ba1\u65f6\u3002\n- \u9002\u7528\u573a\u666f\uff1a\n - \u6309\u94ae\u63d0\u4ea4\u573a\u666f\uff1a\u9632\u6b62\u591a\u6b21\u63d0\u4ea4\u6309\u94ae\uff0c\u53ea\u6267\u884c\u6700\u540e\u63d0\u4ea4\u7684\u4e00\u6b21\n - \u641c\u7d22\u6846\u8054\u60f3\u573a\u666f\uff1a\u9632\u6b62\u8054\u60f3\u53d1\u9001\u8bf7\u6c42\uff0c\u53ea\u53d1\u9001\u6700\u540e\u4e00\u6b21\u8f93\u5165\n- \u7b80\u6613\u7248\u5b9e\u73b0\n\n```js\nfunction debounce(func, wait) {\n let timeout;\n return function () {\n const context = this;\n const args = arguments;\n clearTimeout(timeout)\n timeout = setTimeout(function(){\n func.apply(context, args)\n }, wait);\n }\n}\n```\n\n- \u7acb\u5373\u6267\u884c\u7248\u5b9e\u73b0\n - \u6709\u65f6\u5e0c\u671b\u7acb\u523b\u6267\u884c\u51fd\u6570\uff0c\u7136\u540e\u7b49\u5230\u505c\u6b62\u89e6\u53d1 n \u79d2\u540e\uff0c\u624d\u53ef\u4ee5\u91cd\u65b0\u89e6\u53d1\u6267\u884c\u3002\n\n```js\n// \u6709\u65f6\u5e0c\u671b\u7acb\u523b\u6267\u884c\u51fd\u6570\uff0c\u7136\u540e\u7b49\u5230\u505c\u6b62\u89e6\u53d1 n \u79d2\u540e\uff0c\u624d\u53ef\u4ee5\u91cd\u65b0\u89e6\u53d1\u6267\u884c\u3002\nfunction debounce(func, wait, immediate) {\n let timeout;\n return function () {\n const context = this;\n const args = arguments;\n if (timeout) clearTimeout(timeout);\n if (immediate) {\n const callNow = !timeout;\n timeout = setTimeout(function () {\n timeout = null;\n }, wait)\n if (callNow) func.apply(context, args)\n } else {\n timeout = setTimeout(function () {\n func.apply(context, args)\n }, wait);\n }\n }\n}\n```\n\n- \u8fd4\u56de\u503c\u7248\u5b9e\u73b0\n - func\u51fd\u6570\u53ef\u80fd\u4f1a\u6709\u8fd4\u56de\u503c\uff0c\u6240\u4ee5\u9700\u8981\u8fd4\u56de\u51fd\u6570\u7ed3\u679c\uff0c\u4f46\u662f\u5f53 immediate \u4e3a false \u7684\u65f6\u5019\uff0c\u56e0\u4e3a\u4f7f\u7528\u4e86 setTimeout \uff0c\u6211\u4eec\u5c06 func.apply(context, args) \u7684\u8fd4\u56de\u503c\u8d4b\u7ed9\u53d8\u91cf\uff0c\u6700\u540e\u518d return \u7684\u65f6\u5019\uff0c\u503c\u5c06\u4f1a\u4e00\u76f4\u662f undefined\uff0c\u6240\u4ee5\u53ea\u5728 immediate \u4e3a true \u7684\u65f6\u5019\u8fd4\u56de\u51fd\u6570\u7684\u6267\u884c\u7ed3\u679c\u3002\n\n```js\nfunction debounce(func, wait, immediate) {\n let timeout, result;\n return function () {\n const context = this;\n const args = arguments;\n if (timeout) clearTimeout(timeout);\n if (immediate) {\n const callNow = !timeout;\n timeout = setTimeout(function () {\n timeout = null;\n }, wait)\n if (callNow) result = func.apply(context, args)\n }\n else {\n timeout = setTimeout(function () {\n func.apply(context, args)\n }, wait);\n }\n return result;\n }\n}\n```\n\n2\uff09\u8282\u6d41\n- \u539f\u7406\uff1a\u89c4\u5b9a\u5728\u4e00\u4e2a\u5355\u4f4d\u65f6\u95f4\u5185\uff0c\u53ea\u80fd\u89e6\u53d1\u4e00\u6b21\u51fd\u6570\u3002\u5982\u679c\u8fd9\u4e2a\u5355\u4f4d\u65f6\u95f4\u5185\u89e6\u53d1\u591a\u6b21\u51fd\u6570\uff0c\u53ea\u6709\u4e00\u6b21\u751f\u6548\u3002\n- \u9002\u7528\u573a\u666f\n - \u62d6\u62fd\u573a\u666f\uff1a\u56fa\u5b9a\u65f6\u95f4\u5185\u53ea\u6267\u884c\u4e00\u6b21\uff0c\u9632\u6b62\u8d85\u9ad8\u9891\u6b21\u89e6\u53d1\u4f4d\u7f6e\u53d8\u52a8\n - \u7f29\u653e\u573a\u666f\uff1a\u76d1\u63a7\u6d4f\u89c8\u5668resize\n- \u4f7f\u7528\u65f6\u95f4\u6233\u5b9e\u73b0\n - \u4f7f\u7528\u65f6\u95f4\u6233\uff0c\u5f53\u89e6\u53d1\u4e8b\u4ef6\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53d6\u51fa\u5f53\u524d\u7684\u65f6\u95f4\u6233\uff0c\u7136\u540e\u51cf\u53bb\u4e4b\u524d\u7684\u65f6\u95f4\u6233(\u6700\u4e00\u5f00\u59cb\u503c\u8bbe\u4e3a 0 )\uff0c\u5982\u679c\u5927\u4e8e\u8bbe\u7f6e\u7684\u65f6\u95f4\u5468\u671f\uff0c\u5c31\u6267\u884c\u51fd\u6570\uff0c\u7136\u540e\u66f4\u65b0\u65f6\u95f4\u6233\u4e3a\u5f53\u524d\u7684\u65f6\u95f4\u6233\uff0c\u5982\u679c\u5c0f\u4e8e\uff0c\u5c31\u4e0d\u6267\u884c\u3002\n\n```js\nfunction throttle(func, wait) {\n let context, args;\n let previous = 0;\n\n return function () {\n let now = +new Date();\n context = this;\n args = arguments;\n if (now - previous > wait) {\n func.apply(context, args);\n previous = now;\n }\n }\n}\n```\n\n- \u4f7f\u7528\u5b9a\u65f6\u5668\u5b9e\u73b0\n - \u5f53\u89e6\u53d1\u4e8b\u4ef6\u7684\u65f6\u5019\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u518d\u89e6\u53d1\u4e8b\u4ef6\u7684\u65f6\u5019\uff0c\u5982\u679c\u5b9a\u65f6\u5668\u5b58\u5728\uff0c\u5c31\u4e0d\u6267\u884c\uff0c\u76f4\u5230\u5b9a\u65f6\u5668\u6267\u884c\uff0c\u7136\u540e\u6267\u884c\u51fd\u6570\uff0c\u6e05\u7a7a\u5b9a\u65f6\u5668\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u8bbe\u7f6e\u4e0b\u4e2a\u5b9a\u65f6\u5668\u3002\n\n```js\nfunction throttle(func, wait) {\n let timeout;\n return function () {\n const context = this;\n const args = arguments;\n if (!timeout) {\n timeout = setTimeout(function () {\n timeout = null;\n func.apply(context, args)\n }, wait)\n }\n\n }\n}\n```\n", "answer": "", "biz_type": 1, "qid": 9, "subject": "", "title": "\u4ecb\u7ecd\u9632\u6296\u8282\u6d41\u539f\u7406\u3001\u533a\u522b\u4ee5\u53ca\u5e94\u7528\uff0c\u5e76\u7528JavaScript\u8fdb\u884c\u5b9e\u73b0"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6d77\u5eb7\u5a01\u89c6"], "date": "Fri, 19 Jun 2020 16:21:40 GMT", "favorite_num": 22, "qid": 18, "qtype": "short_answer", "short_answer": {"analysis": "1\uff09\u5b9a\u4e49\n\n- \u6570\u7ec4\u662f\u4e00\u4e2a\u7279\u6b8a\u5bf9\u8c61,\u4e0e\u5e38\u89c4\u5bf9\u8c61\u7684\u533a\u522b\uff1a\n 1. \u5f53\u7531\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u65f6\uff0c\u81ea\u52a8\u66f4\u65b0length\u5c5e\u6027\n 2. \u8bbe\u7f6elength\u5c5e\u6027\uff0c\u53ef\u4ee5\u622a\u65ad\u6570\u7ec4\n 3. \u4eceArray.protoype\u4e2d\u7ee7\u627f\u4e86\u65b9\u6cd5\n 4. \u5c5e\u6027\u4e3a'Array'\n- \u7c7b\u6570\u7ec4\u662f\u4e00\u4e2a\u62e5\u6709length\u5c5e\u6027\uff0c\u5e76\u4e14\u4ed6\u5c5e\u6027\u4e3a\u975e\u8d1f\u6574\u6570\u7684\u666e\u901a\u5bf9\u8c61\uff0c\u7c7b\u6570\u7ec4\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528\u6570\u7ec4\u65b9\u6cd5\u3002\n\n2\uff09\u533a\u522b\n\n\u672c\u8d28\uff1a\u7c7b\u6570\u7ec4\u662f\u7b80\u5355\u5bf9\u8c61\uff0c\u5b83\u7684\u539f\u578b\u5173\u7cfb\u4e0e\u6570\u7ec4\u4e0d\u540c\u3002\n\n```js\n// \u539f\u578b\u5173\u7cfb\u548c\u539f\u59cb\u503c\u8f6c\u6362\nlet arrayLike = {\n length: 10,\n};\nconsole.log(arrayLike instanceof Array); // false\nconsole.log(arrayLike.__proto__.constructor === Array); // false\nconsole.log(arrayLike.toString()); // [object Object]\nconsole.log(arrayLike.valueOf()); // {length: 10}\n\nlet array = [];\nconsole.log(array instanceof Array); // true\nconsole.log(array.__proto__.constructor === Array); // true\nconsole.log(array.toString()); // ''\nconsole.log(array.valueOf()); // []\n```\n\n3\uff09\u7c7b\u6570\u7ec4\u8f6c\u6362\u4e3a\u6570\u7ec4\n\n- \u8f6c\u6362\u65b9\u6cd5\n 1. \u4f7f\u7528 `Array.from()`\n 2. \u4f7f\u7528 `Array.prototype.slice.call()` \n 3. \u4f7f\u7528 `Array.prototype.forEach()` \u8fdb\u884c\u5c5e\u6027\u904d\u5386\u5e76\u7ec4\u6210\u65b0\u7684\u6570\u7ec4 \n- \u8f6c\u6362\u987b\u77e5\n - \u8f6c\u6362\u540e\u7684\u6570\u7ec4\u957f\u5ea6\u7531 `length` \u5c5e\u6027\u51b3\u5b9a\u3002\u7d22\u5f15\u4e0d\u8fde\u7eed\u65f6\u8f6c\u6362\u7ed3\u679c\u662f\u8fde\u7eed\u7684\uff0c\u4f1a\u81ea\u52a8\u8865\u4f4d\u3002\n - \u4ee3\u7801\u793a\u4f8b\n\n```js\nlet al1 = {\n length: 4,\n 0: 0,\n 1: 1,\n 3: 3,\n 4: 4,\n 5: 5,\n};\nconsole.log(Array.from(al1)) // [0, 1, undefined, 3]\n```\n\n - \u2461\u4ec5\u8003\u8651 0\u6216\u6b63\u6574\u6570 \u7684\u7d22\u5f15\n\n```js\n// \u4ee3\u7801\u793a\u4f8b\nlet al2 = {\n length: 4,\n '-1': -1,\n '0': 0,\n a: 'a',\n 1: 1\n};\nconsole.log(Array.from(al2)); // [0, 1, undefined, undefined]\n```\n\n - \u2462\u4f7f\u7528slice\u8f6c\u6362\u4ea7\u751f\u7a00\u758f\u6570\u7ec4\n\n```js\n// \u4ee3\u7801\u793a\u4f8b\nlet al2 = {\n length: 4,\n '-1': -1,\n '0': 0,\n a: 'a',\n 1: 1\n};\nconsole.log(Array.prototype.slice.call(al2)); //[0, 1, empty \u00d7 2]\n```\n\n4\uff09\u4f7f\u7528\u6570\u7ec4\u65b9\u6cd5\u64cd\u4f5c\u7c7b\u6570\u7ec4\u6ce8\u610f\u5730\u65b9\n\n```js\n let arrayLike2 = {\n 2: 3,\n 3: 4,\n length: 2,\n push: Array.prototype.push\n }\n\n // push \u64cd\u4f5c\u7684\u662f\u7d22\u5f15\u503c\u4e3a length \u7684\u4f4d\u7f6e\n arrayLike2.push(1);\n console.log(arrayLike2); // {2: 1, 3: 4, length: 3, push: \u0192}\n arrayLike2.push(2);\n console.log(arrayLike2); // {2: 1, 3: 2, length: 4, push: \u0192}\n```", "answer": "", "biz_type": 1, "qid": 18, "subject": "", "title": "\u7c7b\u6570\u7ec4\u548c\u6570\u7ec4\u7684\u533a\u522b\uff0cdom \u7684\u7c7b\u6570\u7ec4\u5982\u4f55\u8f6c\u6362\u6210\u6570\u7ec4"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4", "\u5934\u6761", "\u559c\u9a6c\u62c9\u96c5", "\u5151\u5427", "\u5bfa\u5e93", "\u767e\u5206\u70b9", "58", "\u5b89\u5c45\u5ba2"], "date": "Fri, 19 Jun 2020 16:21:40 GMT", "favorite_num": 42, "qid": 23, "qtype": "short_answer", "short_answer": {"analysis": "1\uff09Promise\u57fa\u672c\u7279\u6027\n- 1\u3001Promise\u6709\u4e09\u79cd\u72b6\u6001\uff1apending(\u8fdb\u884c\u4e2d)\u3001fulfilled(\u5df2\u6210\u529f)\u3001rejected(\u5df2\u5931\u8d25)\n- 2\u3001Promise\u5bf9\u8c61\u63a5\u53d7\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570, \u8be5\u56de\u8c03\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u662f\u6210\u529f\u65f6\u7684\u56de\u8c03resolve\u548c\u5931\u8d25\u65f6\u7684\u56de\u8c03reject\uff1b\u53e6\u5916resolve\u7684\u53c2\u6570\u9664\u4e86\u6b63\u5e38\u503c\u4ee5\u5916\uff0c \u8fd8\u53ef\u80fd\u662f\u4e00\u4e2aPromise\u5bf9\u8c61\u7684\u5b9e\u4f8b\uff1breject\u7684\u53c2\u6570\u901a\u5e38\u662f\u4e00\u4e2aError\u5bf9\u8c61\u7684\u5b9e\u4f8b\u3002\n- 3\u3001then\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684Promise\u5b9e\u4f8b\uff0c\u5e76\u63a5\u6536\u4e24\u4e2a\u53c2\u6570onResolved(fulfilled\u72b6\u6001\u7684\u56de\u8c03)\uff1bonRejected(rejected\u72b6\u6001\u7684\u56de\u8c03\uff0c\u8be5\u53c2\u6570\u53ef\u9009)\n- 4\u3001catch\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684Promise\u5b9e\u4f8b\n- 5\u3001finally\u65b9\u6cd5\u4e0d\u7ba1Promise\u72b6\u6001\u5982\u4f55\u90fd\u4f1a\u6267\u884c\uff0c\u8be5\u65b9\u6cd5\u7684\u56de\u8c03\u51fd\u6570\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\n- 6\u3001Promise.all()\u65b9\u6cd5\u5c06\u591a\u4e2a\u591a\u4e2aPromise\u5b9e\u4f8b\uff0c\u5305\u88c5\u6210\u4e00\u4e2a\u65b0\u7684Promise\u5b9e\u4f8b\uff0c\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u7531Promise\u5bf9\u8c61\u7ec4\u6210\u7684\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570(Promise.all()\u65b9\u6cd5\u7684\u53c2\u6570\u53ef\u4ee5\u4e0d\u662f\u6570\u7ec4\uff0c\u4f46\u5fc5\u987b\u5177\u6709Iterator\u63a5\u53e3\uff0c\u4e14\u8fd4\u56de\u7684\u6bcf\u4e2a\u6210\u5458\u90fd\u662fPromise\u5b9e\u4f8b)\uff0c\u6ce8\u610f\u53c2\u6570\u4e2d\u53ea\u8981\u6709\u4e00\u4e2a\u5b9e\u4f8b\u89e6\u53d1catch\u65b9\u6cd5\uff0c\u90fd\u4f1a\u89e6\u53d1Promise.all()\u65b9\u6cd5\u8fd4\u56de\u7684\u65b0\u7684\u5b9e\u4f8b\u7684catch\u65b9\u6cd5\uff0c\u5982\u679c\u53c2\u6570\u4e2d\u7684\u67d0\u4e2a\u5b9e\u4f8b\u672c\u8eab\u8c03\u7528\u4e86catch\u65b9\u6cd5\uff0c\u5c06\u4e0d\u4f1a\u89e6\u53d1Promise.all()\u65b9\u6cd5\u8fd4\u56de\u7684\u65b0\u5b9e\u4f8b\u7684catch\u65b9\u6cd5\n- 7\u3001Promise.race()\u65b9\u6cd5\u7684\u53c2\u6570\u4e0ePromise.all\u65b9\u6cd5\u4e00\u6837\uff0c\u53c2\u6570\u4e2d\u7684\u5b9e\u4f8b\u53ea\u8981\u6709\u4e00\u4e2a\u7387\u5148\u6539\u53d8\u72b6\u6001\u5c31\u4f1a\u5c06\u8be5\u5b9e\u4f8b\u7684\u72b6\u6001\u4f20\u7ed9Promise.race()\u65b9\u6cd5\uff0c\u5e76\u5c06\u8fd4\u56de\u503c\u4f5c\u4e3aPromise.race()\u65b9\u6cd5\u4ea7\u751f\u7684Promise\u5b9e\u4f8b\u7684\u8fd4\u56de\u503c\n- 8\u3001Promise.resolve()\u5c06\u73b0\u6709\u5bf9\u8c61\u8f6c\u4e3aPromise\u5bf9\u8c61\uff0c\u5982\u679c\u8be5\u65b9\u6cd5\u7684\u53c2\u6570\u4e3a\u4e00\u4e2aPromise\u5bf9\u8c61\uff0cPromise.resolve()\u5c06\u4e0d\u505a\u4efb\u4f55\u5904\u7406\uff1b\u5982\u679c\u53c2\u6570thenable\u5bf9\u8c61(\u5373\u5177\u6709then\u65b9\u6cd5)\uff0cPromise.resolve()\u5c06\u8be5\u5bf9\u8c61\u8f6c\u4e3aPromise\u5bf9\u8c61\u5e76\u7acb\u5373\u6267\u884cthen\u65b9\u6cd5\uff1b\u5982\u679c\u53c2\u6570\u662f\u4e00\u4e2a\u539f\u59cb\u503c\uff0c\u6216\u8005\u662f\u4e00\u4e2a\u4e0d\u5177\u6709then\u65b9\u6cd5\u7684\u5bf9\u8c61\uff0c\u5219Promise.resolve\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684Promise\u5bf9\u8c61\uff0c\u72b6\u6001\u4e3afulfilled\uff0c\u5176\u53c2\u6570\u5c06\u4f1a\u4f5c\u4e3athen\u65b9\u6cd5\u4e2donResolved\u56de\u8c03\u51fd\u6570\u7684\u53c2\u6570\uff0c\u5982\u679cPromise.resolve\u65b9\u6cd5\u4e0d\u5e26\u53c2\u6570\uff0c\u4f1a\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2afulfilled\u72b6\u6001\u7684 Promise \u5bf9\u8c61\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7acb\u5373resolve()\u7684 Promise \u5bf9\u8c61\uff0c\u662f\u5728\u672c\u8f6e\u201c\u4e8b\u4ef6\u5faa\u73af\u201d\uff08event loop\uff09\u7684\u7ed3\u675f\u65f6\u6267\u884c\uff0c\u800c\u4e0d\u662f\u5728\u4e0b\u4e00\u8f6e\u201c\u4e8b\u4ef6\u5faa\u73af\u201d\u7684\u5f00\u59cb\u65f6\u3002\n- 9\u3001Promise.reject()\u540c\u6837\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684Promise\u5bf9\u8c61\uff0c\u72b6\u6001\u4e3arejected\uff0c\u65e0\u8bba\u4f20\u5165\u4efb\u4f55\u53c2\u6570\u90fd\u5c06\u4f5c\u4e3areject()\u7684\u53c2\u6570\n\n\n2\uff09Promise\u4f18\u70b9\n\n- \u2460\u7edf\u4e00\u5f02\u6b65 API\n - Promise \u7684\u4e00\u4e2a\u91cd\u8981\u4f18\u70b9\u662f\u5b83\u5c06\u9010\u6e10\u88ab\u7528\u4f5c\u6d4f\u89c8\u5668\u7684\u5f02\u6b65 API \uff0c\u7edf\u4e00\u73b0\u5728\u5404\u79cd\u5404\u6837\u7684 API \uff0c\u4ee5\u53ca\u4e0d\u517c\u5bb9\u7684\u6a21\u5f0f\u548c\u624b\u6cd5\u3002\n- \u2461Promise \u4e0e\u4e8b\u4ef6\u5bf9\u6bd4\n - \u548c\u4e8b\u4ef6\u76f8\u6bd4\u8f83\uff0c Promise \u66f4\u9002\u5408\u5904\u7406\u4e00\u6b21\u6027\u7684\u7ed3\u679c\u3002\u5728\u7ed3\u679c\u8ba1\u7b97\u51fa\u6765\u4e4b\u524d\u6216\u4e4b\u540e\u6ce8\u518c\u56de\u8c03\u51fd\u6570\u90fd\u662f\u53ef\u4ee5\u7684\uff0c\u90fd\u53ef\u4ee5\u62ff\u5230\u6b63\u786e\u7684\u503c\u3002 Promise \u7684\u8fd9\u4e2a\u4f18\u70b9\u5f88\u81ea\u7136\u3002\u4f46\u662f\uff0c\u4e0d\u80fd\u4f7f\u7528 Promise \u5904\u7406\u591a\u6b21\u89e6\u53d1\u7684\u4e8b\u4ef6\u3002\u94fe\u5f0f\u5904\u7406\u662f Promise \u7684\u53c8\u4e00\u4f18\u70b9\uff0c\u4f46\u662f\u4e8b\u4ef6\u5374\u4e0d\u80fd\u8fd9\u6837\u94fe\u5f0f\u5904\u7406\u3002\n- \u2462Promise \u4e0e\u56de\u8c03\u5bf9\u6bd4\n - \u89e3\u51b3\u4e86\u56de\u8c03\u5730\u72f1\u7684\u95ee\u9898\uff0c\u5c06\u5f02\u6b65\u64cd\u4f5c\u4ee5\u540c\u6b65\u64cd\u4f5c\u7684\u6d41\u7a0b\u8868\u8fbe\u51fa\u6765\u3002\n- \u2463Promise \u5e26\u6765\u7684\u989d\u5916\u597d\u5904\u662f\u5305\u542b\u4e86\u66f4\u597d\u7684\u9519\u8bef\u5904\u7406\u65b9\u5f0f\uff08\u5305\u542b\u4e86\u5f02\u5e38\u5904\u7406\uff09\uff0c\u5e76\u4e14\u5199\u8d77\u6765\u5f88\u8f7b\u677e\uff08\u56e0\u4e3a\u53ef\u4ee5\u91cd\u7528\u4e00\u4e9b\u540c\u6b65\u7684\u5de5\u5177\uff0c\u6bd4\u5982 Array.prototype.map() \uff09\u3002\n\n3\uff09Promise\u7f3a\u70b9\n\n- 1\u3001\u65e0\u6cd5\u53d6\u6d88Promise\uff0c\u4e00\u65e6\u65b0\u5efa\u5b83\u5c31\u4f1a\u7acb\u5373\u6267\u884c\uff0c\u65e0\u6cd5\u4e2d\u9014\u53d6\u6d88\u3002\n- 2\u3001\u5982\u679c\u4e0d\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\uff0cPromise\u5185\u90e8\u629b\u51fa\u7684\u9519\u8bef\uff0c\u4e0d\u4f1a\u53cd\u5e94\u5230\u5916\u90e8\u3002\n- 3\u3001\u5f53\u5904\u4e8ePending\u72b6\u6001\u65f6\uff0c\u65e0\u6cd5\u5f97\u77e5\u76ee\u524d\u8fdb\u5c55\u5230\u54ea\u4e00\u4e2a\u9636\u6bb5\uff08\u521a\u521a\u5f00\u59cb\u8fd8\u662f\u5373\u5c06\u5b8c\u6210\uff09\u3002\n- 4\u3001Promise \u771f\u6b63\u6267\u884c\u56de\u8c03\u7684\u65f6\u5019\uff0c\u5b9a\u4e49 Promise \u90a3\u90e8\u5206\u5b9e\u9645\u4e0a\u5df2\u7ecf\u8d70\u5b8c\u4e86\uff0c\u6240\u4ee5 Promise \u7684\u62a5\u9519\u5806\u6808\u4e0a\u4e0b\u6587\u4e0d\u592a\u53cb\u597d\u3002\n\n\n4\uff09\u7b80\u5355\u4ee3\u7801\u5b9e\u73b0\n\u6700\u7b80\u5355\u7684Promise\u5b9e\u73b0\u67097\u4e2a\u4e3b\u8981\u5c5e\u6027, state(\u72b6\u6001), value(\u6210\u529f\u8fd4\u56de\u503c), reason(\u9519\u8bef\u4fe1\u606f), resolve\u65b9\u6cd5, reject\u65b9\u6cd5, then\u65b9\u6cd5.\n\n```js\nclass Promise{\n constructor(executor) {\n this.state = 'pending';\n this.value = undefined;\n this.reason = undefined;\n let resolve = value => {\n if (this.state === 'pending') {\n this.state = 'fulfilled';\n this.value = value;\n }\n };\n let reject = reason => {\n if (this.state === 'pending') {\n this.state = 'rejected';\n this.reason = reason;\n }\n };\n try {\n // \u7acb\u5373\u6267\u884c\u51fd\u6570\n executor(resolve, reject);\n } catch (err) {\n reject(err);\n }\n }\n then(onFulfilled, onRejected) {\n if (this.state === 'fulfilled') {\n let x = onFulfilled(this.value);\n };\n if (this.state === 'rejected') {\n let x = onRejected(this.reason);\n };\n }\n}\n```\n\n5\uff09\u9762\u8bd5\u591f\u7528\u7248\n\n```js\nfunction myPromise(constructor){ let self=this;\n self.status=\"pending\" //\u5b9a\u4e49\u72b6\u6001\u6539\u53d8\u524d\u7684\u521d\u59cb\u72b6\u6001 \n self.value=undefined;//\u5b9a\u4e49\u72b6\u6001\u4e3aresolved\u7684\u65f6\u5019\u7684\u72b6\u6001 \n self.reason=undefined;//\u5b9a\u4e49\u72b6\u6001\u4e3arejected\u7684\u65f6\u5019\u7684\u72b6\u6001 \n function resolve(value){\n //\u4e24\u4e2a===\"pending\"\uff0c\u4fdd\u8bc1\u4e86\u4e86\u72b6\u6001\u7684\u6539\u53d8\u662f\u4e0d\u4e0d\u53ef\u9006\u7684 \n if(self.status===\"pending\"){\n self.value=value;\n self.status=\"resolved\"; \n }\n }\n function reject(reason){\n //\u4e24\u4e2a===\"pending\"\uff0c\u4fdd\u8bc1\u4e86\u4e86\u72b6\u6001\u7684\u6539\u53d8\u662f\u4e0d\u4e0d\u53ef\u9006\u7684\n if(self.status===\"pending\"){\n self.reason=reason;\n self.status=\"rejected\"; \n }\n }\n //\u6355\u83b7\u6784\u9020\u5f02\u5e38 \n try{\n constructor(resolve,reject);\n }catch(e){\n reject(e);\n } \n}\nmyPromise.prototype.then=function(onFullfilled,onRejected){ \n let self=this;\n switch(self.status){\n case \"resolved\": onFullfilled(self.value); break;\n case \"rejected\": onRejected(self.reason); break;\n default: \n }\n}\n\n// \u6d4b\u8bd5\nvar p=new myPromise(function(resolve,reject){resolve(1)}); \np.then(function(x){console.log(x)})\n//\u8f93\u51fa1\n```\n\n6\uff09\u5927\u5382\u4e13\u4f9b\u7248\n\n```js\nconst PENDING = \"pending\"; \nconst FULFILLED = \"fulfilled\"; \nconst REJECTED = \"rejected\";\nfunction Promise(excutor) {\n let that = this; // \u7f13\u5b58\u5f53\u524dpromise\u5b9e\u4f8b\u4f8b\u5bf9\u8c61\n that.status = PENDING; // \u521d\u59cb\u72b6\u6001\n that.value = undefined; // fulfilled\u72b6\u6001\u65f6 \u8fd4\u56de\u7684\u4fe1\u606f\n that.reason = undefined; // rejected\u72b6\u6001\u65f6 \u62d2\u7edd\u7684\u539f\u56e0 \n that.onFulfilledCallbacks = []; // \u5b58\u50a8fulfilled\u72b6\u6001\u5bf9\u5e94\u7684onFulfilled\u51fd\u6570\n that.onRejectedCallbacks = []; // \u5b58\u50a8rejected\u72b6\u6001\u5bf9\u5e94\u7684onRejected\u51fd\u6570\n function resolve(value) { // value\u6210\u529f\u6001\u65f6\u63a5\u6536\u7684\u7ec8\u503c\n if(value instanceof Promise) {\n return value.then(resolve, reject);\n }\n // \u5b9e\u8df5\u4e2d\u8981\u786e\u4fdd onFulfilled \u548c onRejected \u2f45\u65b9\u6cd5\u5f02\u6b65\u6267\u2f8f\u884c\u884c\uff0c\u4e14\u5e94\u8be5\u5728 then \u2f45\u65b9\u6cd5\u88ab\u8c03\u2f64\u7528\u7684\u90a3\u2f00\u4e00\u8f6e\u4e8b\u4ef6\u5faa\u73af\u4e4b\u540e\u7684\u65b0\u6267\u2f8f\u884c\u884c\u6808\u4e2d\u6267\u2f8f\u884c\u884c\u3002\n setTimeout(() => {\n // \u8c03\u2f64\u7528resolve \u56de\u8c03\u5bf9\u5e94onFulfilled\u51fd\u6570\n if (that.status === PENDING) {\n // \u53ea\u80fd\u7531pending\u72b6\u6001 => fulfilled\u72b6\u6001 (\u907f\u514d\u8c03\u2f64\u7528\u591a\u6b21resolve reject)\n that.status = FULFILLED;\n that.value = value;\n that.onFulfilledCallbacks.forEach(cb => cb(that.value));\n }\n });\n }\n function reject(reason) { // reason\u5931\u8d25\u6001\u65f6\u63a5\u6536\u7684\u62d2\u56e0\n setTimeout(() => {\n // \u8c03\u2f64\u7528reject \u56de\u8c03\u5bf9\u5e94onRejected\u51fd\u6570\n if (that.status === PENDING) {\n // \u53ea\u80fd\u7531pending\u72b6\u6001 => rejected\u72b6\u6001 (\u907f\u514d\u8c03\u2f64\u7528\u591a\u6b21resolve reject)\n that.status = REJECTED;\n that.reason = reason;\n that.onRejectedCallbacks.forEach(cb => cb(that.reason));\n }\n });\n }\n\n // \u6355\u83b7\u5728excutor\u6267\u2f8f\u884c\u884c\u5668\u5668\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\n // new Promise((resolve, reject) => {\n // throw new Error('error in excutor')\n // })\n try {\n excutor(resolve, reject);\n } catch (e) {\n reject(e);\n }\n}\nPromise.prototype.then = function(onFulfilled, onRejected) {\n const that = this;\n let newPromise;\n // \u5904\u7406\u7406\u53c2\u6570\u9ed8\u8ba4\u503c \u4fdd\u8bc1\u53c2\u6570\u540e\u7eed\u80fd\u591f\u7ee7\u7eed\u6267\u2f8f\u884c\u884c\n onFulfilled = typeof onFulfilled === \"function\" ? onFulfilled : value => value;\n onRejected = typeof onRejected === \"function\" ? onRejected : reason => {\n throw reason;\n };\n if (that.status === FULFILLED) { // \u6210\u529f\u6001\n return newPromise = new Promise((resolve, reject) => {\n setTimeout(() => {\n try{\n let x = onFulfilled(that.value);\n resolvePromise(newPromise, x, resolve, reject); //\u65b0\u7684promise resolve \u4e0a\u2f00\u4e00\u4e2aonFulfilled\u7684\u8fd4\u56de\u503c\n } catch(e) {\n reject(e); // \u6355\u83b7\u524d\u2faf\u9762onFulfilled\u4e2d\u629b\u51fa\u7684\u5f02\u5e38then(onFulfilled, onRejected);\n }\n });\n })\n }\n if (that.status === REJECTED) { // \u5931\u8d25\u6001\n return newPromise = new Promise((resolve, reject) => {\n setTimeout(() => {\n try {\n let x = onRejected(that.reason);\n resolvePromise(newPromise, x, resolve, reject);\n } catch(e) {\n reject(e);\n }\n });\n });\n }\n if (that.status === PENDING) { // \u7b49\u5f85\u6001\n// \u5f53\u5f02\u6b65\u8c03\u2f64\u7528resolve/rejected\u65f6 \u5c06onFulfilled/onRejected\u6536\u96c6\u6682\u5b58\u5230\u96c6\u5408\u4e2d\n return newPromise = new Promise((resolve, reject) => {\n that.onFulfilledCallbacks.push((value) => {\n try {\n let x = onFulfilled(value);\n resolvePromise(newPromise, x, resolve, reject);\n } catch(e) {\n reject(e);\n }\n });\n that.onRejectedCallbacks.push((reason) => {\n try {\n let x = onRejected(reason);\n resolvePromise(newPromise, x, resolve, reject);\n } catch(e) {\n reject(e);\n }\n });\n });\n }\n};\n```", "answer": "", "biz_type": 1, "qid": 23, "subject": "", "title": "\u4ecb\u7ecd\u4e0b promise \u7684\u7279\u6027\u3001\u4f18\u7f3a\u70b9\uff0c\u5185\u90e8\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u52a8\u624b\u5b9e\u73b0 Promise"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4", "\u5934\u6761", "\u6709\u8d5e", "\u5fae\u533b"], "date": "Fri, 19 Jun 2020 16:21:40 GMT", "favorite_num": 27, "qid": 24, "qtype": "short_answer", "short_answer": {"analysis": "**1) \u6838\u5fc3\u601d\u8def**\n\n- \u2460\u63a5\u6536\u4e00\u4e2a Promise \u5b9e\u4f8b\u7684\u6570\u7ec4\u6216\u5177\u6709 Iterator \u63a5\u53e3\u7684\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\n- \u2461\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 promise \u5bf9\u8c61\uff0c\n- \u2462\u904d\u5386\u4f20\u5165\u7684\u53c2\u6570\uff0c\u7528Promise.resolve()\u5c06\u53c2\u6570\"\u5305\u4e00\u5c42\"\uff0c\u4f7f\u5176\u53d8\u6210\u4e00\u4e2apromise\u5bf9\u8c61\n- \u2463\u53c2\u6570\u6240\u6709\u56de\u8c03\u6210\u529f\u624d\u662f\u6210\u529f\uff0c\u8fd4\u56de\u503c\u6570\u7ec4\u4e0e\u53c2\u6570\u987a\u5e8f\u4e00\u81f4\n- \u2464\u53c2\u6570\u6570\u7ec4\u5176\u4e2d\u4e00\u4e2a\u5931\u8d25\uff0c\u5219\u89e6\u53d1\u5931\u8d25\u72b6\u6001\uff0c\u7b2c\u4e00\u4e2a\u89e6\u53d1\u5931\u8d25\u7684 Promise \u9519\u8bef\u4fe1\u606f\u4f5c\u4e3a Promise.all \u7684\u9519\u8bef\u4fe1\u606f\u3002\n\n**2\uff09\u5b9e\u73b0\u4ee3\u7801**\n\u4e00\u822c\u6765\u8bf4\uff0cPromise.all \u7528\u6765\u5904\u7406\u591a\u4e2a\u5e76\u53d1\u8bf7\u6c42\uff0c\u4e5f\u662f\u4e3a\u4e86\u9875\u9762\u6570\u636e\u6784\u9020\u7684\u65b9\u4fbf\uff0c\u5c06\u4e00\u4e2a\u9875\u9762\u6240\u7528\u5230\u7684\u5728\u4e0d\u540c\u63a5\u53e3\u7684\u6570\u636e\u4e00\u8d77\u8bf7\u6c42\u8fc7\u6765\uff0c\u4e0d\u8fc7\uff0c\u5982\u679c\u5176\u4e2d\u4e00\u4e2a\u63a5\u53e3\u5931\u8d25\u4e86\uff0c\u591a\u4e2a\u8bf7\u6c42\u4e5f\u5c31\u5931\u8d25\u4e86\uff0c\u9875\u9762\u53ef\u80fd\u5565\u4e5f\u51fa\u4e0d\u6765\uff0c\u8fd9\u5c31\u770b\u5f53\u524d\u9875\u9762\u7684\u8026\u5408\u7a0b\u5ea6\u4e86\uff5e\n\n```js\nfunction promiseAll(promises) {\n return new Promise(function(resolve, reject) {\n if(!Array.isArray(promises)){\n throw new TypeError(`argument must be a array`)\n }\n var resolvedCounter = 0;\n var promiseNum = promises.length;\n var resolvedResult = [];\n for (let i = 0; i < promiseNum; i++) {\n Promise.resolve(promises[i]).then(value=>{\n resolvedCounter++;\n resolvedResult[i] = value;\n if (resolvedCounter == promiseNum) {\n return resolve(resolvedResult)\n }\n },error=>{\n return reject(error)\n })\n }\n })\n}\n\n// test\nlet p1 = new Promise(function (resolve, reject) {\n setTimeout(function () {\n resolve(1)\n }, 1000)\n})\nlet p2 = new Promise(function (resolve, reject) {\n setTimeout(function () {\n resolve(2)\n }, 2000)\n})\nlet p3 = new Promise(function (resolve, reject) {\n setTimeout(function () {\n resolve(3)\n }, 3000)\n})\npromiseAll([p3, p1, p2]).then(res => {\n console.log(res) // [3, 1, 2]\n})\n```", "answer": "", "biz_type": 1, "qid": 24, "subject": "```js\nPromise.all = function (arr) {\n // \u5b9e\u73b0\u4ee3\u7801\n};\n```\n", "title": "\u5b9e\u73b0 Promise.all"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4", "\u4f34\u9c7c", "\u9ad8\u5fb7", "\u81ea\u5982", "\u864e\u6251", "58"], "date": "Fri, 19 Jun 2020 16:21:40 GMT", "favorite_num": 44, "qid": 20, "qtype": "short_answer", "short_answer": {"analysis": "#### 1\uff09\u4e3a\u4ec0\u4e48\u4f1a\u6709Event Loop\n\n\u200b JavaScript\u7684\u4efb\u52a1\u5206\u4e3a\u4e24\u79cd`\u540c\u6b65`\u548c`\u5f02\u6b65`\uff0c\u5b83\u4eec\u7684\u5904\u7406\u65b9\u5f0f\u4e5f\u5404\u81ea\u4e0d\u540c\uff0c**\u540c\u6b65\u4efb\u52a1**\u662f\u76f4\u63a5\u653e\u5728\u4e3b\u7ebf\u7a0b\u4e0a\u6392\u961f\u4f9d\u6b21\u6267\u884c\uff0c**\u5f02\u6b65\u4efb\u52a1**\u4f1a\u653e\u5728\u4efb\u52a1\u961f\u5217\u4e2d\uff0c\u82e5\u6709\u591a\u4e2a\u5f02\u6b65\u4efb\u52a1\u5219\u9700\u8981\u5728\u4efb\u52a1\u961f\u5217\u4e2d\u6392\u961f\u7b49\u5f85\uff0c\u4efb\u52a1\u961f\u5217\u7c7b\u4f3c\u4e8e\u7f13\u51b2\u533a\uff0c\u4efb\u52a1\u4e0b\u4e00\u6b65\u4f1a\u88ab\u79fb\u5230**\u8c03\u7528\u6808**\u7136\u540e\u4e3b\u7ebf\u7a0b\u6267\u884c\u8c03\u7528\u6808\u7684\u4efb\u52a1\u3002\n\n> **\u8c03\u7528\u6808**\uff1a\u8c03\u7528\u6808\u662f\u4e00\u4e2a\u6808\u7ed3\u6784\uff0c\u51fd\u6570\u8c03\u7528\u4f1a\u5f62\u6210\u4e00\u4e2a\u6808\u5e27\uff0c\u5e27\u4e2d\u5305\u542b\u4e86\u5f53\u524d\u6267\u884c\u51fd\u6570\u7684\u53c2\u6570\u548c\u5c40\u90e8\u53d8\u91cf\u7b49\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u51fd\u6570\u6267\u884c\u5b8c\u540e\uff0c\u5b83\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u4f1a\u4ece\u6808\u4e2d\u5f39\u51fa\u3002\n\n\u200b JavaScript\u662f`\u5355\u7ebf\u7a0b`\u7684\uff0c\u5355\u7ebf\u7a0b\u662f\u6307 js\u5f15\u64ce\u4e2d\u89e3\u6790\u548c\u6267\u884cjs\u4ee3\u7801\u7684\u7ebf\u7a0b\u53ea\u6709\u4e00\u4e2a\uff08\u4e3b\u7ebf\u7a0b\uff09\uff0c\u6bcf\u6b21\u53ea\u80fd\u505a\u4e00\u4ef6\u4e8b\u60c5\uff0c\u7136\u800c`ajax`\u8bf7\u6c42\u4e2d\uff0c\u4e3b\u7ebf\u7a0b\u5728\u7b49\u5f85\u54cd\u5e94\u7684\u8fc7\u7a0b\u4e2d\u56de\u53bb\u505a\u5176\u4ed6\u4e8b\u60c5\uff0c\u6d4f\u89c8\u5668\u5148\u5728\u4e8b\u4ef6\u8868\u6ce8\u518cajax\u7684\u56de\u8c03\u51fd\u6570\uff0c\u54cd\u5e94\u56de\u6765\u540e\u56de\u8c03\u51fd\u6570\u88ab\u6dfb\u52a0\u5230\u4efb\u52a1\u961f\u5217\u4e2d\u7b49\u5f85\u6267\u884c\uff0c\u4e0d\u4f1a\u9020\u6210\u7ebf\u7a0b\u963b\u585e\uff0c\u6240\u4ee5\u8bf4js\u5904\u7406ajax\u8bf7\u6c42\u7684\u65b9\u5f0f\u662f\u5f02\u6b65\u7684\u3002\n\n\u200b \u7efc\u4e0a\u6240\u8ff0\uff0c\u68c0\u67e5\u8c03\u7528\u6808\u662f\u5426\u4e3a\u7a7a\u4ee5\u53ca\u8bb2\u67d0\u4e2a\u4efb\u52a1\u6dfb\u52a0\u5230\u8c03\u7528\u6808\u4e2d\u7684\u4e2a\u8fc7\u7a0b\u5c31\u662fevent loop\uff0c\u8fd9\u5c31\u662fJavaScript\u5b9e\u73b0\u5f02\u6b65\u7684\u6838\u5fc3\u3002\n\n\n\n#### 2\uff09\u6d4f\u89c8\u5668\u4e2d\u7684 Event Loop\n\n##### Micro-Task \u4e0e Macro-Task\n\n\u6d4f\u89c8\u5668\u7aef\u4e8b\u4ef6\u5faa\u73af\u4e2d\u7684\u5f02\u6b65\u961f\u5217\u6709\u4e24\u79cd\uff1amacro\uff08\u5b8f\u4efb\u52a1\uff09\u961f\u5217\u548c micro\uff08\u5fae\u4efb\u52a1\uff09\u961f\u5217\u3002\n\n\u5e38\u89c1\u7684 macro-task\uff1a`setTimeout`\u3001`setInterval`\u3001`script\uff08\u6574\u4f53\u4ee3\u7801\uff09`\u3001` I/O \u64cd\u4f5c`\u3001`UI \u6e32\u67d3`\u7b49\u3002\n\n\u5e38\u89c1\u7684 micro-task: `new Promise().then(\u56de\u8c03)`\u3001`MutationObserve `\u7b49\u3002\n\n\n\n##### requestAnimationFrame\n\nrequestAnimationFrame\u4e5f\u5c5e\u4e8e\u5f02\u6b65\u6267\u884c\u7684\u65b9\u6cd5\uff0c\u4f46\u8be5\u65b9\u6cd5\u65e2\u4e0d\u5c5e\u4e8e\u5b8f\u4efb\u52a1\uff0c\u4e5f\u4e0d\u5c5e\u4e8e\u5fae\u4efb\u52a1\u3002\u6309\u7167MDN\u4e2d\u7684\u5b9a\u4e49\uff1a\n\n> `window.requestAnimationFrame()` \u544a\u8bc9\u6d4f\u89c8\u5668\u2014\u2014\u4f60\u5e0c\u671b\u6267\u884c\u4e00\u4e2a\u52a8\u753b\uff0c\u5e76\u4e14\u8981\u6c42\u6d4f\u89c8\u5668\u5728\u4e0b\u6b21\u91cd\u7ed8\u4e4b\u524d\u8c03\u7528\u6307\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u66f4\u65b0\u52a8\u753b\u3002\u8be5\u65b9\u6cd5\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u8be5\u56de\u8c03\u51fd\u6570\u4f1a\u5728\u6d4f\u89c8\u5668\u4e0b\u4e00\u6b21\u91cd\u7ed8\u4e4b\u524d\u6267\u884c\n\nrequestAnimationFrame\u662fGUI\u6e32\u67d3\u4e4b\u524d\u6267\u884c\uff0c\u4f46\u5728`Micro-Task`\u4e4b\u540e\uff0c\u4e0d\u8fc7requestAnimationFrame\u4e0d\u4e00\u5b9a\u4f1a\u5728\u5f53\u524d\u5e27\u5fc5\u987b\u6267\u884c\uff0c\u7531\u6d4f\u89c8\u5668\u6839\u636e\u5f53\u524d\u7684\u7b56\u7565\u81ea\u884c\u51b3\u5b9a\u5728\u54ea\u4e00\u5e27\u6267\u884c\u3002\n\n\n\n##### event loop\u8fc7\u7a0b\n\n![](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-20-1.jpg)\n\n1. \u68c0\u67e5macrotask\u961f\u5217\u662f\u5426\u4e3a\u7a7a\uff0c\u975e\u7a7a\u5219\u52302\uff0c\u4e3a\u7a7a\u5219\u52303\n2. \u6267\u884cmacrotask\u4e2d\u7684\u4e00\u4e2a\u4efb\u52a1\n3. \u7ee7\u7eed\u68c0\u67e5microtask\u961f\u5217\u662f\u5426\u4e3a\u7a7a\uff0c\u82e5\u6709\u5219\u52304\uff0c\u5426\u5219\u52305\n4. \u53d6\u51famicrotask\u4e2d\u7684\u4efb\u52a1\u6267\u884c\uff0c\u6267\u884c\u5b8c\u6210\u8fd4\u56de\u5230\u6b65\u9aa43\n5. \u6267\u884c\u89c6\u56fe\u66f4\u65b0\n\n> \u5f53\u67d0\u4e2a\u5b8f\u4efb\u52a1\u6267\u884c\u5b8c\u540e,\u4f1a\u67e5\u770b\u662f\u5426\u6709\u5fae\u4efb\u52a1\u961f\u5217\u3002\u5982\u679c\u6709\uff0c\u5148\u6267\u884c\u5fae\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u6240\u6709\u4efb\u52a1\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u4f1a\u8bfb\u53d6\u5b8f\u4efb\u52a1\u961f\u5217\u4e2d\u6392\u5728\u6700\u524d\u7684\u4efb\u52a1\uff0c\u6267\u884c\u5b8f\u4efb\u52a1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9047\u5230\u5fae\u4efb\u52a1\uff0c\u4f9d\u6b21\u52a0\u5165\u5fae\u4efb\u52a1\u961f\u5217\u3002\u6808\u7a7a\u540e\uff0c\u518d\u6b21\u8bfb\u53d6\u5fae\u4efb\u52a1\u961f\u5217\u91cc\u7684\u4efb\u52a1\uff0c\u4f9d\u6b21\u7c7b\u63a8\u3002\n\n\n\n#### 3\uff09node\u4e2d\u7684 Event Loop\n\nNode \u4e2d\u7684 Event Loop \u548c\u6d4f\u89c8\u5668\u4e2d\u7684\u662f\u5b8c\u5168\u4e0d\u76f8\u540c\u7684\u4e1c\u897f\u3002Node.js\u91c7\u7528V8\u4f5c\u4e3ajs\u7684\u89e3\u6790\u5f15\u64ce\uff0c\u800cI/O\u5904\u7406\u65b9\u9762\u4f7f\u7528\u4e86\u81ea\u5df1\u8bbe\u8ba1\u7684libuv\uff0clibuv\u662f\u4e00\u4e2a\u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u7684\u8de8\u5e73\u53f0\u62bd\u8c61\u5c42\uff0c\u5c01\u88c5\u4e86\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e00\u4e9b\u5e95\u5c42\u7279\u6027\uff0c\u5bf9\u5916\u63d0\u4f9b\u7edf\u4e00\u7684API\uff0c\u4e8b\u4ef6\u5faa\u73af\u673a\u5236\u4e5f\u662f\u5b83\u91cc\u9762\u7684\u5b9e\u73b0\n\n![](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-20-2.png)\n\n\u6839\u636e\u4e0a\u56fenode\u7684\u8fd0\u884c\u673a\u5236\u5982\u4e0b\n\n1. V8\u5f15\u64ce\u89e3\u6790JavaScript\u811a\u672c\u3002\n2. \u89e3\u6790\u540e\u7684\u4ee3\u7801\uff0c\u8c03\u7528Node API\u3002\n3. libuv\u5e93\u8d1f\u8d23Node API\u7684\u6267\u884c\u3002\u5b83\u5c06\u4e0d\u540c\u7684\u4efb\u52a1\u5206\u914d\u7ed9\u4e0d\u540c\u7684\u7ebf\u7a0b\uff0c\u5f62\u6210\u4e00\u4e2aEvent Loop\uff08\u4e8b\u4ef6\u5faa\u73af\uff09\uff0c\u4ee5\u5f02\u6b65\u7684\u65b9\u5f0f\u5c06\u4efb\u52a1\u7684\u6267\u884c\u7ed3\u679c\u8fd4\u56de\u7ed9V8\u5f15\u64ce\u3002\n4. V8\u5f15\u64ce\u518d\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u7528\u6237\u3002\n\n\n\n##### \u516d\u5927\u9636\u6bb5\n\n\u5176\u4e2dlibuv\u5f15\u64ce\u4e2d\u7684\u4e8b\u4ef6\u5faa\u73af\u5206\u4e3a 6 \u4e2a\u9636\u6bb5\uff0c\u5b83\u4eec\u4f1a\u6309\u7167\u987a\u5e8f\u53cd\u590d\u8fd0\u884c\u3002\u6bcf\u5f53\u8fdb\u5165\u67d0\u4e00\u4e2a\u9636\u6bb5\u7684\u65f6\u5019\uff0c\u90fd\u4f1a\u4ece\u5bf9\u5e94\u7684\u56de\u8c03\u961f\u5217\u4e2d\u53d6\u51fa\u51fd\u6570\u53bb\u6267\u884c\u3002\u5f53\u961f\u5217\u4e3a\u7a7a\u6216\u8005\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570\u6570\u91cf\u5230\u8fbe\u7cfb\u7edf\u8bbe\u5b9a\u7684\u9608\u503c\uff0c\u5c31\u4f1a\u8fdb\u5165\u4e0b\u4e00\u9636\u6bb5\u3002\n\n![](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-20-3.png)\n\n1. `timers` \u9636\u6bb5\uff1a\u8fd9\u4e2a\u9636\u6bb5\u6267\u884ctimer\uff08setTimeout\u3001setInterval\uff09\u7684\u56de\u8c03\uff0c\u5e76\u4e14\u662f\u7531 poll \u9636\u6bb5\u63a7\u5236\u7684\u3002\n2. `I/O callbacks` \u9636\u6bb5\uff1a\u5904\u7406\u4e00\u4e9b\u4e0a\u4e00\u8f6e\u5faa\u73af\u4e2d\u7684\u5c11\u6570\u672a\u6267\u884c\u7684 I/O \u56de\u8c03\n3. `idle, prepare` \u9636\u6bb5\uff1a\u4ec5node\u5185\u90e8\u4f7f\u7528\n4. `poll` \u9636\u6bb5\uff1a\u83b7\u53d6\u65b0\u7684I/O\u4e8b\u4ef6, \u9002\u5f53\u7684\u6761\u4ef6\u4e0bnode\u5c06\u963b\u585e\u5728\u8fd9\u91cc\n5. `check` \u9636\u6bb5\uff1a\u6267\u884c setImmediate() \u7684\u56de\u8c03\n6. `close callbacks` \u9636\u6bb5\uff1a\u6267\u884c socket \u7684 close \u4e8b\u4ef6\u56de\u8c03\n\n\n\n##### poll\u9636\u6bb5\n\npoll \u662f\u4e00\u4e2a\u81f3\u5173\u91cd\u8981\u7684\u9636\u6bb5\uff0c\u8fd9\u4e00\u9636\u6bb5\u4e2d\uff0c\u7cfb\u7edf\u4f1a\u505a\u4e24\u4ef6\u4e8b\u60c5\n\n1.\u56de\u5230 timer \u9636\u6bb5\u6267\u884c\u56de\u8c03\n\n2.\u6267\u884c I/O \u56de\u8c03\n\n\u5e76\u4e14\u5728\u8fdb\u5165\u8be5\u9636\u6bb5\u65f6\u5982\u679c\u6ca1\u6709\u8bbe\u5b9a\u4e86 timer \u7684\u8bdd\uff0c\u4f1a\u53d1\u751f\u4ee5\u4e0b\u4e24\u4ef6\u4e8b\u60c5\n\n- \u5982\u679c poll \u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u4f1a\u904d\u5386\u56de\u8c03\u961f\u5217\u5e76\u540c\u6b65\u6267\u884c\uff0c\u76f4\u5230\u961f\u5217\u4e3a\u7a7a\u6216\u8005\u8fbe\u5230\u7cfb\u7edf\u9650\u5236\n- \u5982\u679c poll \u961f\u5217\u4e3a\u7a7a\u65f6\uff0c\u4f1a\u6709\u4e24\u4ef6\u4e8b\u53d1\u751f\n - \u5982\u679c\u6709 setImmediate \u56de\u8c03\u9700\u8981\u6267\u884c\uff0cpoll \u9636\u6bb5\u4f1a\u505c\u6b62\u5e76\u4e14\u8fdb\u5165\u5230 check \u9636\u6bb5\u6267\u884c\u56de\u8c03\n - \u5982\u679c\u6ca1\u6709 setImmediate \u56de\u8c03\u9700\u8981\u6267\u884c\uff0c\u4f1a\u7b49\u5f85\u56de\u8c03\u88ab\u52a0\u5165\u5230\u961f\u5217\u4e2d\u5e76\u7acb\u5373\u6267\u884c\u56de\u8c03\uff0c\u8fd9\u91cc\u540c\u6837\u4f1a\u6709\u4e2a\u8d85\u65f6\u65f6\u95f4\u8bbe\u7f6e\u9632\u6b62\u4e00\u76f4\u7b49\u5f85\u4e0b\u53bb\n\n\u5f53\u7136\u8bbe\u5b9a\u4e86 timer \u7684\u8bdd\u4e14 poll \u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4f1a\u5224\u65ad\u662f\u5426\u6709 timer \u8d85\u65f6\uff0c\u5982\u679c\u6709\u7684\u8bdd\u4f1a\u56de\u5230 timer \u9636\u6bb5\u6267\u884c\u56de\u8c03\u3002\n\n\n\n##### Micro-Task \u4e0e Macro-Task\n\nNode\u7aef\u4e8b\u4ef6\u5faa\u73af\u4e2d\u7684\u5f02\u6b65\u961f\u5217\u4e5f\u662f\u8fd9\u4e24\u79cd\uff1amacro\uff08\u5b8f\u4efb\u52a1\uff09\u961f\u5217\u548c micro\uff08\u5fae\u4efb\u52a1\uff09\u961f\u5217\u3002\n\n- \u5e38\u89c1\u7684 macro-task \u6bd4\u5982\uff1a`setTimeout`\u3001`setInterval`\u3001 `setImmediate`\u3001`script\uff08\u6574\u4f53\u4ee3\u7801`\uff09\u3001` I/O \u64cd\u4f5c\u7b49`\u3002\n- \u5e38\u89c1\u7684 micro-task \u6bd4\u5982: `process.nextTick`\u3001`new Promise().then(\u56de\u8c03)`\u7b49\u3002\n\n\n\n##### setTimeout \u548c setImmediate\n\n\u4e8c\u8005\u975e\u5e38\u76f8\u4f3c\uff0c\u533a\u522b\u4e3b\u8981\u5728\u4e8e\u8c03\u7528\u65f6\u673a\u4e0d\u540c\u3002\n\n- setImmediate \u8bbe\u8ba1\u5728poll\u9636\u6bb5\u5b8c\u6210\u65f6\u6267\u884c\uff0c\u5373check\u9636\u6bb5\uff1b\n- setTimeout \u8bbe\u8ba1\u5728poll\u9636\u6bb5\u4e3a\u7a7a\u95f2\u65f6\uff0c\u4e14\u8bbe\u5b9a\u65f6\u95f4\u5230\u8fbe\u540e\u6267\u884c\uff0c\u4f46\u5b83\u5728timer\u9636\u6bb5\u6267\u884c\n\n```javascript\nsetTimeout(function timeout () {\n console.log('timeout');\n},0);\nsetImmediate(function immediate () {\n console.log('immediate');\n});\n```\n\n1. \u5bf9\u4e8e\u4ee5\u4e0a\u4ee3\u7801\u6765\u8bf4\uff0csetTimeout \u53ef\u80fd\u6267\u884c\u5728\u524d\uff0c\u4e5f\u53ef\u80fd\u6267\u884c\u5728\u540e\u3002\n2. \u9996\u5148 setTimeout(fn, 0) === setTimeout(fn, 1)\uff0c\u8fd9\u662f\u7531\u6e90\u7801\u51b3\u5b9a\u7684 \u8fdb\u5165\u4e8b\u4ef6\u5faa\u73af\u4e5f\u662f\u9700\u8981\u6210\u672c\u7684\uff0c\u5982\u679c\u5728\u51c6\u5907\u65f6\u5019\u82b1\u8d39\u4e86\u5927\u4e8e 1ms \u7684\u65f6\u95f4\uff0c\u90a3\u4e48\u5728 timer \u9636\u6bb5\u5c31\u4f1a\u76f4\u63a5\u6267\u884c setTimeout \u56de\u8c03\n3. \u5982\u679c\u51c6\u5907\u65f6\u95f4\u82b1\u8d39\u5c0f\u4e8e 1ms\uff0c\u90a3\u4e48\u5c31\u662f setImmediate \u56de\u8c03\u5148\u6267\u884c\u4e86\n\n\n\n##### process.nextTick\n\n\u8fd9\u4e2a\u51fd\u6570\u5176\u5b9e\u662f\u72ec\u7acb\u4e8e Event Loop \u4e4b\u5916\u7684\uff0c\u5b83\u6709\u4e00\u4e2a\u81ea\u5df1\u7684\u961f\u5217\uff0c\u5f53\u6bcf\u4e2a\u9636\u6bb5\u5b8c\u6210\u540e\uff0c\u5982\u679c\u5b58\u5728 nextTick \u961f\u5217\uff0c\u5c31\u4f1a\u6e05\u7a7a\u961f\u5217\u4e2d\u7684\u6240\u6709\u56de\u8c03\u51fd\u6570\uff0c\u5e76\u4e14\u4f18\u5148\u4e8e\u5176\u4ed6 microtask \u6267\u884c\n\n\n\n#### 4\uff09Node\u4e0e\u6d4f\u89c8\u5668\u7684 Event Loop \u5dee\u5f02\n\n![](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-20-4.png)\n\n- Node\u7aef\uff0cmicrotask \u5728\u4e8b\u4ef6\u5faa\u73af\u7684\u5404\u4e2a\u9636\u6bb5\u4e4b\u95f4\u6267\u884c\n- \u6d4f\u89c8\u5668\u7aef\uff0cmicrotask \u5728\u4e8b\u4ef6\u5faa\u73af\u7684 macrotask \u6267\u884c\u5b8c\u4e4b\u540e\u6267\u884c\n", "answer": "", "biz_type": 1, "qid": 20, "subject": "", "title": "\u8bf4\u4e00\u4e0b\u4e8b\u4ef6\u5faa\u73af\u673a\u5236(node\u3001\u6d4f\u89c8\u5668)"}, "tech_tag": ["Node", "JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4", "\u643a\u7a0b", "\u559c\u9a6c\u62c9\u96c5", "\u5fae\u533b", "\u8611\u83c7\u8857", "\u9177\u5bb6\u4e50", "\u817e\u8baf\u5e94\u7528\u5b9d", "\u5b89\u5c45\u5ba2"], "date": "Fri, 19 Jun 2020 16:21:40 GMT", "favorite_num": 50, "qid": 11, "qtype": "short_answer", "short_answer": {"analysis": "### \u7b54\u6848\n\n#### 1\uff09\u4ec0\u4e48\u662f\u95ed\u5305\n\n\u51fd\u6570\u6267\u884c\u540e\u8fd4\u56de\u7ed3\u679c\u662f\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\uff0c\u5e76\u88ab\u5916\u90e8\u53d8\u91cf\u6240\u5f15\u7528\uff0c\u5982\u679c\u5185\u90e8\u51fd\u6570\u6301\u6709\u88ab\u6267\u884c\u51fd\u6570\u4f5c\u7528\u57df\u7684\u53d8\u91cf\uff0c\u5373\u5f62\u6210\u4e86\u95ed\u5305\u3002\n\n\u53ef\u4ee5\u5728\u5185\u90e8\u51fd\u6570\u8bbf\u95ee\u5230\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u3002\u4f7f\u7528\u95ed\u5305\uff0c\u4e00\u53ef\u4ee5\u8bfb\u53d6\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\uff0c\u4e8c\u53ef\u4ee5\u5c06\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u4fdd\u62a4\u53d8\u91cf\u4e0d\u88ab\u6c61\u67d3\u3002\u800c\u6b63\u56e0\u95ed\u5305\u4f1a\u628a\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u503c\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u4f1a\u5bf9\u5185\u5b58\u6709\u6d88\u8017\uff0c\u6240\u4ee5\u4e0d\u80fd\u6ee5\u7528\u95ed\u5305\uff0c\u5426\u5219\u4f1a\u5f71\u54cd\u7f51\u9875\u6027\u80fd\uff0c\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u3002\u5f53\u4e0d\u9700\u8981\u4f7f\u7528\u95ed\u5305\u65f6\uff0c\u8981\u53ca\u65f6\u91ca\u653e\u5185\u5b58\uff0c\u53ef\u5c06\u5185\u5c42\u51fd\u6570\u5bf9\u8c61\u7684\u53d8\u91cf\u8d4b\u503c\u4e3anull\u3002\n\n\n#### 2\uff09\u95ed\u5305\u539f\u7406\n\n\u51fd\u6570\u6267\u884c\u5206\u6210\u4e24\u4e2a\u9636\u6bb5(\u9884\u7f16\u8bd1\u9636\u6bb5\u548c\u6267\u884c\u9636\u6bb5)\u3002\n\n- \u5728\u9884\u7f16\u8bd1\u9636\u6bb5\uff0c\u5982\u679c\u53d1\u73b0\u5185\u90e8\u51fd\u6570\u4f7f\u7528\u4e86\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u5219\u4f1a\u5728\u5185\u5b58\u4e2d\u521b\u5efa\u4e00\u4e2a\u201c\u95ed\u5305\u201d\u5bf9\u8c61\u5e76\u4fdd\u5b58\u5bf9\u5e94\u53d8\u91cf\u503c\uff0c\u5982\u679c\u5df2\u5b58\u5728\u201c\u95ed\u5305\u201d\uff0c\u5219\u53ea\u9700\u8981\u589e\u52a0\u5bf9\u5e94\u5c5e\u6027\u503c\u5373\u53ef\u3002\n- \u6267\u884c\u5b8c\u540e\uff0c\u51fd\u6570\u6267\u884c\u4e0a\u4e0b\u6587\u4f1a\u88ab\u9500\u6bc1\uff0c\u51fd\u6570\u5bf9\u201c\u95ed\u5305\u201d\u5bf9\u8c61\u7684\u5f15\u7528\u4e5f\u4f1a\u88ab\u9500\u6bc1\uff0c\u4f46\u5176\u5185\u90e8\u51fd\u6570\u8fd8\u6301\u7528\u8be5\u201c\u95ed\u5305\u201d\u7684\u5f15\u7528\uff0c\u6240\u4ee5\u5185\u90e8\u51fd\u6570\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528\u201c\u5916\u90e8\u51fd\u6570\u201d\u4e2d\u7684\u53d8\u91cf\n\n\u5229\u7528\u4e86\u51fd\u6570\u4f5c\u7528\u57df\u94fe\u7684\u7279\u6027\uff0c\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u51fd\u6570\u4f1a\u5c06\u5305\u542b\u5916\u90e8\u51fd\u6570\u7684\u6d3b\u52a8\u5bf9\u8c61\u6dfb\u52a0\u5230\u5b83\u7684\u4f5c\u7528\u57df\u94fe\u4e2d\uff0c\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\uff0c\u5176\u6267\u884c\u4f5c\u7528\u57df\u94fe\u9500\u6bc1\uff0c\u4f46\u56e0\u5185\u90e8\u51fd\u6570\u7684\u4f5c\u7528\u57df\u94fe\u4ecd\u7136\u5728\u5f15\u7528\u8fd9\u4e2a\u6d3b\u52a8\u5bf9\u8c61\uff0c\u6240\u4ee5\u5176\u6d3b\u52a8\u5bf9\u8c61\u4e0d\u4f1a\u88ab\u9500\u6bc1\uff0c\u76f4\u5230\u5185\u90e8\u51fd\u6570\u88ab\u70e7\u6bc1\u540e\u624d\u88ab\u9500\u6bc1\u3002\n\n#### 3\uff09\u4f18\u70b9\n\n1. \u53ef\u4ee5\u4ece\u5185\u90e8\u51fd\u6570\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\uff0c\u4e14\u8bbf\u95ee\u5230\u7684\u53d8\u91cf\u957f\u671f\u9a7b\u624e\u5728\u5185\u5b58\u4e2d\uff0c\u53ef\u4f9b\u4e4b\u540e\u4f7f\u7528\n2. \u907f\u514d\u53d8\u91cf\u6c61\u67d3\u5168\u5c40\n3. \u628a\u53d8\u91cf\u5b58\u5230\u72ec\u7acb\u7684\u4f5c\u7528\u57df\uff0c\u4f5c\u4e3a\u79c1\u6709\u6210\u5458\u5b58\u5728\n\n#### 4\uff09\u7f3a\u70b9\n\n1. \u5bf9\u5185\u5b58\u6d88\u8017\u6709\u8d1f\u9762\u5f71\u54cd\u3002\u56e0\u5185\u90e8\u51fd\u6570\u4fdd\u5b58\u4e86\u5bf9\u5916\u90e8\u53d8\u91cf\u7684\u5f15\u7528\uff0c\u5bfc\u81f4\u65e0\u6cd5\u88ab\u5783\u573e\u56de\u6536\uff0c\u589e\u5927\u5185\u5b58\u4f7f\u7528\u91cf\uff0c\u6240\u4ee5\u4f7f\u7528\u4e0d\u5f53\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\n2. \u5bf9\u5904\u7406\u901f\u5ea6\u5177\u6709\u8d1f\u9762\u5f71\u54cd\u3002\u95ed\u5305\u7684\u5c42\u7ea7\u51b3\u5b9a\u4e86\u5f15\u7528\u7684\u5916\u90e8\u53d8\u91cf\u5728\u67e5\u627e\u65f6\u7ecf\u8fc7\u7684\u4f5c\u7528\u57df\u94fe\u957f\u5ea6\n3. \u53ef\u80fd\u83b7\u53d6\u5230\u610f\u5916\u7684\u503c(captured value)\n\n#### 4\uff09\u5e94\u7528\u573a\u666f\n\n**\u5e94\u7528\u573a\u666f\u4e00\uff1a** \u5178\u578b\u5e94\u7528\u662f\u6a21\u5757\u5c01\u88c5\uff0c\u5728\u5404\u6a21\u5757\u89c4\u8303\u51fa\u73b0\u4e4b\u524d\uff0c\u90fd\u662f\u7528\u8fd9\u6837\u7684\u65b9\u5f0f\u9632\u6b62\u53d8\u91cf\u6c61\u67d3\u5168\u5c40\u3002\n\n```js\nvar Yideng = (function () {\n // \u8fd9\u6837\u58f0\u660e\u4e3a\u6a21\u5757\u79c1\u6709\u53d8\u91cf\uff0c\u5916\u754c\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\n var foo = 0;\n\n function Yideng() {}\n Yideng.prototype.bar = function bar() {\n return foo;\n };\n return Yideng;\n}());\n```\n\n**\u5e94\u7528\u573a\u666f\u4e8c\uff1a** \u5728\u5faa\u73af\u4e2d\u521b\u5efa\u95ed\u5305\uff0c\u9632\u6b62\u53d6\u5230\u610f\u5916\u7684\u503c\u3002\n\n\u5982\u4e0b\u4ee3\u7801\uff0c\u65e0\u8bba\u54ea\u4e2a\u5143\u7d20\u89e6\u53d1\u4e8b\u4ef6\uff0c\u90fd\u4f1a\u5f39\u51fa 3\u3002\u56e0\u4e3a\u51fd\u6570\u6267\u884c\u540e\u5f15\u7528\u7684 i \u662f\u540c\u4e00\u4e2a\uff0c\u800c i \u5728\u5faa\u73af\u7ed3\u675f\u540e\u5c31\u662f 3\n\n```js\nfor (var i = 0; i < 3; i++) {\n document.getElementById('id' + i).onfocus = function() {\n alert(i);\n };\n}\n//\u53ef\u7528\u95ed\u5305\u89e3\u51b3\nfunction makeCallback(num) {\n return function() {\n alert(num);\n };\n}\nfor (var i = 0; i < 3; i++) {\n document.getElementById('id' + i).onfocus = makeCallback(i);\n}\n```\n", "answer": "", "biz_type": 1, "qid": 11, "subject": "", "title": "\u5bf9\u95ed\u5305\u7684\u770b\u6cd5\uff0c\u4e3a\u4ec0\u4e48\u8981\u7528\u95ed\u5305\uff1f\u8bf4\u4e00\u4e0b\u95ed\u5305\u539f\u7406\u4ee5\u53ca\u5e94\u7528\u573a\u666f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:21:39 GMT", "favorite_num": 7, "qid": 1, "qtype": "short_answer", "short_answer": {"analysis": "```js\nconst mySetInterval = (fn, a, b) => {\n let _interval = [a, a + b, a + 2 * b]\n let _currentInterval = null\n let _idx = 0\n\n let _mySetInterval = _t => {\n if(_idx < _interval.length) {\n clearInterval(_currentInterval)\n _currentInterval = setInterval(() => {\n fn()\n _idx++\n _mySetInterval(_interval[_idx])\n }, _t)\n return _currentInterval\n } else {\n _idx = 0\n _mySetInterval(_interval[_idx])\n }\n }\n _mySetInterval(_interval[_idx])\n return _currentInterval\n}\n\nconst myClear = (_interval) => {\n clearInterval(_interval)\n}\nlet interval = mySetInterval(() => {\n console.timeEnd('\u5b9a\u65f6\u5668')\n console.time('\u5b9a\u65f6\u5668')\n}, 1000, 1000)\n```", "answer": "", "biz_type": 1, "qid": 1, "subject": "", "title": "\u5199\u4e00\u4e2a mySetInterVal(fn, a, b),\u6bcf\u6b21\u95f4\u9694 a,a+b,a+2b \u7684\u65f6\u95f4\uff0c\u7136\u540e\u5199\u4e00\u4e2a myClear\uff0c\u505c\u6b62\u4e0a\u9762\u7684 mySetInterVal"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7384\u6b66\u79d1\u6280"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 16, "qid": 715, "qtype": "short_answer", "short_answer": {"analysis": "\u76ee\u524d\u6d41\u884c\u7684js\u6a21\u5757\u5316\u89c4\u8303\u6709CommonJS\u3001AMD\u3001CMD\u4ee5\u53caES6\u7684\u6a21\u5757\u7cfb\u7edf\u3002\n\n### \u4e00\u3001CommonJS\n\n**CommonJS\u7684\u51fa\u53d1\u70b9:** JS\u6ca1\u6709\u5b8c\u5584\u7684\u6a21\u5757\u7cfb\u7edf\uff0c\u6807\u51c6\u5e93\u8f83\u5c11\uff0c\u7f3a\u5c11\u5305\u7ba1\u7406\u5de5\u5177\u3002\u4f34\u968f\u7740NodeJS\u7684\u5174\u8d77\uff0c\u80fd\u8ba9JS\u5728\u4efb\u4f55\u5730\u65b9\u8fd0\u884c\uff0c\u7279\u522b\u662f\u670d\u52a1\u7aef\uff0c\u4e5f\u8fbe\u5230\u4e86\u5177\u5907\u5f00\u53d1\u5927\u578b\u9879\u76ee\u7684\u80fd\u529b\uff0c\u6240\u4ee5CommonJS\u8425\u8fd0\u800c\u751f\u3002\n\nNode.js\u662fcommonJS\u89c4\u8303\u7684\u4e3b\u8981\u5b9e\u8df5\u8005\uff0c\u5b83\u6709\u56db\u4e2a\u91cd\u8981\u7684\u73af\u5883\u53d8\u91cf\u4e3a\u6a21\u5757\u5316\u7684\u5b9e\u73b0\u63d0\u4f9b\u652f\u6301\uff1a`module`\u3001`exports`\u3001`require`\u3001`global`\u3002\u5b9e\u9645\u4f7f\u7528\u65f6\uff0c\u7528`module.exports`\u5b9a\u4e49\u5f53\u524d\u6a21\u5757\u5bf9\u5916\u8f93\u51fa\u7684\u63a5\u53e3\uff08\u4e0d\u63a8\u8350\u76f4\u63a5\u7528`exports`\uff09\uff0c\u7528`require`\u52a0\u8f7d\u6a21\u5757\u3002\n\ncommonJS\u7528\u540c\u6b65\u7684\u65b9\u5f0f\u52a0\u8f7d\u6a21\u5757\u3002\u5728\u670d\u52a1\u7aef\uff0c\u6a21\u5757\u6587\u4ef6\u90fd\u5b58\u5728\u672c\u5730\u78c1\u76d8\uff0c\u8bfb\u53d6\u975e\u5e38\u5feb\uff0c\u6240\u4ee5\u8fd9\u6837\u505a\u4e0d\u4f1a\u6709\u95ee\u9898\u3002\u4f46\u662f\u5728\u6d4f\u89c8\u5668\u7aef\uff0c\u9650\u4e8e\u7f51\u7edc\u539f\u56e0\uff0c\u66f4\u5408\u7406\u7684\u65b9\u6848\u662f\u4f7f\u7528\u5f02\u6b65\u52a0\u8f7d\u3002\n\n- \u66b4\u9732\u6a21\u5757\uff1a`module.exports = value`\u6216`exports.xxx = value`\n- \u5f15\u5165\u6a21\u5757\uff1a`require(xxx)`\n\n#### 1.CommonJS\u89c4\u8303\n\n- \u4e00\u4e2a\u6587\u4ef6\u5c31\u662f\u4e00\u4e2a\u6a21\u5757\uff0c\u62e5\u6709\u5355\u72ec\u7684\u4f5c\u7528\u57df\n- \u666e\u901a\u65b9\u5f0f\u5b9a\u4e49\u7684\u53d8\u91cf\u3001\u51fd\u6570\u3001\u5bf9\u8c61\u90fd\u5c5e\u4e8e\u8be5\u6a21\u5757\u5185\n- \u901a\u8fc7require\u6765\u52a0\u8f7d\u6a21\u5757\n- \u901a\u8fc7exports\u548cmodule.exports\u6765\u66b4\u9732\u5757\u4e2d\u7684\u5185\u5bb9\n\n#### 2.\u6ce8\u610f\n\n1. \u5f53exports\u548cmodule.exports\u540c\u65f6\u5b58\u5728\u65f6\uff0cmodule.exports\u4f1a\u8986\u76d6exports\n2. \u5f53\u6a21\u5757\u5185\u5168\u662fexports\u65f6\uff0c\u5c31\u7b49\u540c\u4e8emodule.exports\n3. exports\u5c31\u662fmodule.exports\u7684\u5b50\u96c6\n4. \u6240\u6709\u4ee3\u7801\u90fd\u8fd0\u884c\u5728\u6a21\u5757\u4f5c\u7528\u57df\uff0c\u4e0d\u4f1a\u6c61\u67d3\u5168\u5c40\u4f5c\u7528\u57df\n5. \u6a21\u5757\u53ef\u4ee5\u591a\u6b21\u52a0\u8f7d\uff0c\u4f46\u53ea\u4f1a\u5728\u7b2c\u4e00\u6b21\u52a0\u8f7d\u65f6\u5019\u8fd0\u884c\uff0c\u7136\u540e\u8fd0\u884c\u7ed3\u679c\u5c31\u88ab\u7f13\u5b58\u4e86\uff0c\u4ee5\u540e\u518d\u52a0\u8f7d\uff0c\u5c31\u76f4\u63a5\u8bfb\u53d6\u7f13\u5b58\u7ed3\u679c\n6. \u6a21\u5757\u52a0\u8f7d\u987a\u8baf\uff0c\u6309\u7167\u4ee3\u7801\u51fa\u73b0\u7684\u987a\u5e8f\u540c\u6b65\u52a0\u8f7d\n7. __dirname\u4ee3\u8868\u5f53\u524d\u78e8\u5177\u7231\u6587\u4ef6\u6240\u5728\u7684\u6587\u4ef6\u5939\u8def\u5f84\n8. __filename\u4ee3\u8868\u5f53\u524d\u6a21\u5757\u6587\u4ef6\u6240\u5728\u7684\u6587\u4ef6\u5939\u8def\u5f84+\u6587\u4ef6\u540d\n\n### \u4e8c\u3001ES6\u6a21\u5757\u5316\n\nES6 \u5728\u8bed\u8a00\u6807\u51c6\u7684\u5c42\u9762\u4e0a\uff0c\u5b9e\u73b0\u4e86\u6a21\u5757\u529f\u80fd\uff0c\u800c\u4e14\u5b9e\u73b0\u5f97\u76f8\u5f53\u7b80\u5355\uff0c\u65e8\u5728\u6210\u4e3a\u6d4f\u89c8\u5668\u548c\u670d\u52a1\u5668\u901a\u7528\u7684\u6a21\u5757\u89e3\u51b3\u65b9\u6848\u3002\u5176\u6a21\u5757\u529f\u80fd\u4e3b\u8981\u7531\u4e24\u4e2a\u547d\u4ee4\u6784\u6210\uff1a`export`\u548c`import`\u3002`export`\u547d\u4ee4\u7528\u4e8e\u89c4\u5b9a\u6a21\u5757\u7684\u5bf9\u5916\u63a5\u53e3\uff0c`import`\u547d\u4ee4\u7528\u4e8e\u8f93\u5165\u5176\u4ed6\u6a21\u5757\u63d0\u4f9b\u7684\u529f\u80fd\u3002\n\n\u5176\u5b9eES6\u8fd8\u63d0\u4f9b\u4e86`export default`\u547d\u4ee4\uff0c\u4e3a\u6a21\u5757\u6307\u5b9a\u9ed8\u8ba4\u8f93\u51fa\uff0c\u5bf9\u5e94\u7684`import`\u8bed\u53e5\u4e0d\u9700\u8981\u4f7f\u7528\u5927\u62ec\u53f7\u3002\u8fd9\u4e5f\u66f4\u8d8b\u8fd1\u4e8eAMD\u7684\u5f15\u7528\u5199\u6cd5\u3002\n\nES6\u7684\u6a21\u5757\u4e0d\u662f\u5bf9\u8c61\uff0c`import`\u547d\u4ee4\u4f1a\u88ab JavaScript \u5f15\u64ce\u9759\u6001\u5206\u6790\uff0c\u5728\u7f16\u8bd1\u65f6\u5c31\u5f15\u5165\u6a21\u5757\u4ee3\u7801\uff0c\u800c\u4e0d\u662f\u5728\u4ee3\u7801\u8fd0\u884c\u65f6\u52a0\u8f7d\uff0c\u6240\u4ee5\u65e0\u6cd5\u5b9e\u73b0\u6761\u4ef6\u52a0\u8f7d\u3002\u4e5f\u6b63\u56e0\u4e3a\u8fd9\u4e2a\uff0c\u4f7f\u5f97\u9759\u6001\u5206\u6790\u6210\u4e3a\u53ef\u80fd\u3002\n\n#### 1.export\n\nexport\u53ef\u4ee5\u5bfc\u51fa\u7684\u662f\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u5305\u542b\u7684\u591a\u4e2a\u5c5e\u6027\u3001\u65b9\u6cd5\u3002export default\u53ea\u80fd\u5bfc\u51fa\u4e00\u4e2a\u53ef\u4ee5\u4e0d\u5177\u540d\u7684\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7import\u8fdb\u884c\u5f15\u7528\u3002\u540c\u65f6\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528require\u4f7f\u7528\uff0c\u539f\u56e0\u662fwebpack\u8d77\u4e86server\u76f8\u5173\u3002\n\n#### 2.import\n\n1. import {fn} from './xxx/xxx' ( export \u5bfc\u51fa\u65b9\u5f0f\u7684 \u5f15\u7528\u65b9\u5f0f )\n2. import fn from './xxx/xxx1' ( export default \u5bfc\u51fa\u65b9\u5f0f\u7684 \u5f15\u7528\u65b9\u5f0f )\n\n### \u4e09\u3001AMD\n\nAsynchronous Module Definition\uff0c\u5f02\u6b65\u52a0\u8f7d\u6a21\u5757\u3002\u5b83\u662f\u4e00\u4e2a\u5728\u6d4f\u89c8\u5668\u7aef\u6a21\u5757\u5316\u5f00\u53d1\u7684\u89c4\u8303\uff0c\u4e0d\u662f\u539f\u751fjs\u7684\u89c4\u8303\uff0c\u4f7f\u7528AMD\u89c4\u8303\u8fdb\u884c\u9875\u9762\u5f00\u53d1\u9700\u8981\u7528\u5230\u5bf9\u5e94\u7684\u51fd\u6570\u5e93\uff0cRequireJS\u3002\n\nAMD\u89c4\u8303\u91c7\u7528\u5f02\u6b65\u65b9\u5f0f\u52a0\u8f7d\u6a21\u5757\uff0c\u6a21\u5757\u7684\u52a0\u8f7d\u4e0d\u5f71\u54cd\u5b83\u540e\u9762\u8bed\u53e5\u7684\u8fd0\u884c\u3002\u6240\u6709\u4f9d\u8d56\u8fd9\u4e2a\u6a21\u5757\u7684\u8bed\u53e5\uff0c\u90fd\u5b9a\u4e49\u5728\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4e2d\uff0c\u7b49\u5230\u52a0\u8f7d\u5b8c\u6210\u4e4b\u540e\uff0c\u8fd9\u4e2a\u56de\u8c03\u51fd\u6570\u624d\u4f1a\u8fd0\u884c\u3002\n\n\u4f7f\u7528require.js\u5b9e\u73b0AMD\u89c4\u8303\u7684\u6a21\u5757\u5316\uff1a\u7528`require.config()`\u6307\u5b9a\u5f15\u7528\u8def\u5f84\u7b49\uff0c\u7528`define()`\u5b9a\u4e49\u6a21\u5757\uff0c\u7528`require()`\u52a0\u8f7d\u6a21\u5757\u3002\n\n```js\n//\u5b9a\u4e49\u6a21\u5757\ndefine('moduleName',['a','b'],function(ma,mb){\n return someExportValue;\n})\n//\u5f15\u5165\u6a21\u5757\nrequire(['a','b'],function(ma,mb){\n /*code*/\n})\n```\n\n#### 1.RequireJS\u4e3b\u8981\u89e3\u51b3\u7684\u95ee\u9898\n\n- \u6587\u4ef6\u53ef\u80fd\u6709\u4f9d\u8d56\u5173\u7cfb\uff0c\u88ab\u4f9d\u8d56\u7684\u6587\u4ef6\u9700\u8981\u65e9\u4e8e\u4f9d\u8d56\u5b83\u7684\u6587\u4ef6\u52a0\u8f7d\u5230\u6d4f\u89c8\u5668\n- js\u52a0\u8f7d\u7684\u65f6\u5019\u6d4f\u89c8\u5668\u4f1a\u505c\u6b62\u9875\u9762\u6e32\u67d3\uff0c\u52a0\u8f7d\u6587\u4ef6\u6108\u591a\uff0c\u9875\u9762\u76f8\u5e94\u4e8b\u4ef6\u5c31\u8d8a\u957f\n- \u5f02\u6b65\u524d\u7f6e\u52a0\u8f7d\n\n#### 2.\u8bed\u6cd5\n\n\n`define(id,dependencies,factory)`\n\n- id \u53ef\u9009\u53c2\u6570\uff0c\u7528\u6765\u5b9a\u4e49\u6a21\u5757\u7684\u6807\u8bc6\uff0c\u5982\u679c\u6ca1\u6709\u63d0\u4f9b\u8be5\u53c2\u6570\uff0c\u811a\u672c\u6587\u4ef6\u540d\uff08\u53bb\u6389\u62d3\u5c55\u540d\uff09\n- dependencies \u662f\u4e00\u4e2a\u5f53\u524d\u6a21\u5757\u7528\u6765\u7684\u6a21\u5757\u540d\u79f0\u6570\u7ec4\n- factory \u5de5\u5382\u65b9\u6cd5\uff0c\u6a21\u5757\u521d\u59cb\u5316\u8981\u6267\u884c\u7684\u51fd\u6570\u6216\u5bf9\u8c61\uff0c\u5982\u679c\u4e3a\u51fd\u6570\uff0c\u5b83\u5e94\u8be5\u53ea\u88ab\u6267\u884c\u4e00\u6b21\uff0c\u5982\u679c\u662f\u5bf9\u8c61\uff0c\u6b64\u5bf9\u8c61\u5e94\u8be5\u4e3a\u6a21\u5757\u7684\u8f93\u51fa\u503c\u3002\n\n\n### \u56db\u3001CMD\n\nCMD\u662f\u53e6\u4e00\u79cdjs\u6a21\u5757\u5316\u65b9\u6848\uff0c\u5b83\u4e0eAMD\u5f88\u7c7b\u4f3c\uff0c\u4e0d\u540c\u70b9\u5728\u4e8e\uff1aAMD \u63a8\u5d07\u4f9d\u8d56\u524d\u7f6e\u3001\u63d0\u524d\u6267\u884c\uff0cCMD\u63a8\u5d07\u4f9d\u8d56\u5c31\u8fd1\u3001\u5ef6\u8fdf\u6267\u884c\u3002\u6b64\u89c4\u8303\u5176\u5b9e\u662f\u5728sea.js\u63a8\u5e7f\u8fc7\u7a0b\u4e2d\u4ea7\u751f\u7684\u3002\n\n\u56e0\u4e3aCMD\u63a8\u5d07\u4e00\u4e2a\u6587\u4ef6\u4e00\u4e2a\u6a21\u5757\uff0c\u6240\u4ee5\u7ecf\u5e38\u5c31\u7528\u6587\u4ef6\u540d\u4f5c\u4e3a\u6a21\u5757id\uff1b CMD\u63a8\u5d07\u4f9d\u8d56\u5c31\u8fd1\uff0c\u6240\u4ee5\u4e00\u822c\u4e0d\u5728define\u7684\u53c2\u6570\u4e2d\u5199\u4f9d\u8d56\uff0c\u800c\u662f\u5728factory\u4e2d\u5199\u3002\n\n`define(id, deps, factory)`\n\nfactory\u6709\u4e09\u4e2a\u53c2\u6570\uff1a `function(require, exports, module){}`\n\n1. require require \u662f factory \u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0crequire \u662f\u4e00\u4e2a\u65b9\u6cd5\uff0c\u63a5\u53d7 \u6a21\u5757\u6807\u8bc6 \u4f5c\u4e3a\u552f\u4e00\u53c2\u6570\uff0c\u7528\u6765\u83b7\u53d6\u5176\u4ed6\u6a21\u5757\u63d0\u4f9b\u7684\u63a5\u53e3\uff1b\n2. exports exports \u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u7528\u6765\u5411\u5916\u63d0\u4f9b\u6a21\u5757\u63a5\u53e3\uff1b\n3. module module\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4e0a\u9762\u5b58\u50a8\u4e86\u4e0e\u5f53\u524d\u6a21\u5757\u76f8\u5173\u8054\u7684\u4e00\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\n\n```js\n//\u5b9a\u4e49\u6ca1\u6709\u4f9d\u8d56\u7684\u6a21\u5757\ndefine(function(require,exports,module){\n exports.xxx = vaule;\n module.exports = value;\n})\n//\u5b9a\u4e49\u6709\u4f9d\u8d56\u7684\u6a21\u5757\ndefine(function(require,exports,module){\n //\u540c\u6b65\u5f15\u5165\u6a21\u5757\n var module1 = require(\"./module1.js\");\n //\u5f02\u6b65\u5f15\u5165\u6a21\u5757\n require.async(\"./module2.js\",function(m2){\n /***/\n })\n //\u66b4\u9732\u63a5\u53e3\n exports.xxx = value;\n})\n\n//\u5f15\u5165\u6a21\u5757\ndefine(function(require){\n const m1 = require(\"./module1.js\");\n m1.show();\n})\n```\n\n### \u4e94\u3001UMD\u901a\u7528\u6a21\u5757\u89c4\u8303\n\n\u4e00\u79cd\u6574\u5408\u4e86CommonJS\u548cAMD\u89c4\u8303\u7684\u65b9\u6cd5\uff0c\u5e0c\u671b\u80fd\u89e3\u51b3\u8de8\u5e73\u53f0\u6a21\u5757\u65b9\u6848\u3002\n\n**\u8fd0\u884c\u539f\u7406**\n\n- UMD\u5148\u5224\u65ad\u662f\u5426\u652f\u6301Node.js\u7684\u6a21\u5757\uff08exports\uff09\u662f\u5426\u5b58\u5728\uff0c\u5b58\u5728\u5219\u4f7f\u7528Node.js\u6a21\u5757\u6a21\u5f0f\u3002\n- \u518d\u5224\u65ad\u662f\u5426\u652f\u6301AMD\uff08define\u662f\u5426\u5b58\u5728\uff09\uff0c\u5b58\u5728\u5219\u4f7f\u7528AMD\u65b9\u5f0f\u52a0\u8f7d\u6a21\u5757\u3002\n\n```js\n(function (window, factory) {\n if (typeof exports === 'object') { \n module.exports = factory();\n } else if (typeof define === 'function' && define.amd) {\n define(factory);\n } else { \n window.eventUtil = factory();\n }\n})(this, function () {\n //module ...\n});\n```\n\n### \u516d\u3001\u603b\u7ed3\n\ncommonjs\u662f\u540c\u6b65\u52a0\u8f7d\u7684\u3002\u4e3b\u8981\u662f\u5728nodejs \u4e5f\u5c31\u662f\u670d\u52a1\u7aef\u5e94\u7528\u7684\u6a21\u5757\u5316\u673a\u5236\uff0c\u901a\u8fc7 `module.export` \u5bfc\u51fa\u58f0\u660e\uff0c\u901a\u8fc7 `require('')` \u52a0\u8f7d\u3002\u6bcf\u4e2a\u6587\u4ef6\u90fd\u662f\u4e00\u4e2a\u6a21\u5757\u3002\u4ed6\u6709\u81ea\u5df1\u7684\u4f5c\u7528\u57df\uff0c\u6587\u4ef6\u5185\u7684\u53d8\u91cf\uff0c\u5c5e\u6027\u51fd\u6570\u7b49\u4e0d\u80fd\u88ab\u5916\u754c\u8bbf\u95ee\u3002node\u4f1a\u5c06\u6a21\u5757\u7f13\u5b58\uff0c\u7b2c\u4e8c\u6b21\u52a0\u8f7d\u4f1a\u76f4\u63a5\u5728\u7f13\u5b58\u4e2d\u83b7\u53d6\u3002\n\nAMD\u662f\u5f02\u6b65\u52a0\u8f7d\u7684\u3002\u4e3b\u8981\u5e94\u7528\u5728\u6d4f\u89c8\u5668\u73af\u5883\u4e0b\u3002requireJS\u662f\u9075\u5faaAMD\u89c4\u8303\u7684\u6a21\u5757\u5316\u5de5\u5177\u3002\u4ed6\u662f\u901a\u8fc7 `define()` \u5b9a\u4e49\u58f0\u660e\uff0c\u901a\u8fc7 `require('',function(){})` \u52a0\u8f7d\u3002\n\nES6\u7684\u6a21\u5757\u5316\u52a0\u8f7d\u65f6\u901a\u8fc7 `export default` \u5bfc\u51fa,\u7528import\u5bfc\u5165 \u53ef\u901a\u8fc7 `{}` \u5bf9\u5bfc\u51fa\u7684\u5185\u5bb9\u8fdb\u884c\u89e3\u6784\u3002\n\nES6\u7684\u6a21\u5757\u7684\u8fd0\u884c\u673a\u5236\u4e0ecommon\u4e0d\u4e00\u6837\uff0cjs\u5f15\u64ce\u5bf9\u811a\u672c\u9759\u6001\u5206\u6790\u7684\u65f6\u5019\uff0c\u9047\u5230\u6a21\u5757\u52a0\u8f7d\u6307\u4ee4\u540e\u4f1a\u751f\u6210\u4e00\u4e2a\u53ea\u8bfb\u5f15\u7528\uff0c\u7b49\u5230\u811a\u672c\u771f\u6b63\u6267\u884c\u7684\u65f6\u5019\u624d\u4f1a\u901a\u8fc7\u5f15\u7528\u53bb\u6a21\u5757\u4e2d\u83b7\u53d6\u503c\uff0c\u5728\u5f15\u7528\u5230\u6267\u884c\u7684\u8fc7\u7a0b\u4e2d \u6a21\u5757\u4e2d\u7684\u503c\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u5bfc\u5165\u7684\u8fd9\u91cc\u4e5f\u4f1a\u8ddf\u7740\u53d8\uff0cES6\u6a21\u5757\u662f\u52a8\u6001\u5f15\u7528\uff0c\u5e76\u4e0d\u4f1a\u7f13\u5b58\u503c\uff0c\u6a21\u5757\u91cc\u603b\u662f\u7ed1\u5b9a\u5176\u6240\u5728\u7684\u6a21\u5757\u3002", "answer": "", "biz_type": 1, "qid": 715, "subject": "", "title": "\u8bf7\u5217\u51fa\u76ee\u524d\u4e3b\u6d41\u7684 JavaScript \u6a21\u5757\u5316\u5b9e\u73b0\u7684\u6280\u672f\u6709\u54ea\u4e9b\uff1f\u8bf4\u51fa\u5b83\u4eec\u7684\u533a\u522b\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7231\u8303\u513f", "\u9053\u4e00\u4e91"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 13, "qid": 697, "qtype": "short_answer", "short_answer": {"analysis": "#### \u4ee3\u7801\u5b9e\u73b0\n\n```js\n// \u76f4\u63a5\u5b9e\u73b0\ndocument.onclick = function(e){\n let e = e || window.event;//\u5904\u7406\u517c\u5bb9\uff0c\u83b7\u53d6\u4e8b\u4ef6\u7684\u5bf9\u8c61\n let o = e[\"target\"] || e[\"srcElement\"];//\u5904\u7406\u517c\u5bb9\uff0c\u83b7\u53d6\u4e8b\u4ef6\u76ee\u6807\n alert(o.tagName.toLowerCase()); \n}\n\n// \u4f18\u96c5\u5b9e\u73b0\nfunction elementName(evt) {\n evt = evt || window.event;\n var selected = evt.target || evt.srcElement;\n var eleName =\n selected && selected.tagName\n ? selected.tagName.toLowerCase()\n : \"no tagName\";\n alert(eleName);\n}\n\nwindow.onload = function() {\n var el = document.getElementsByTagName(\"body\");\n el[0].onclick = elementName;\n};\n```", "answer": "", "biz_type": 1, "qid": 697, "subject": "", "title": "\u8bf7\u5b9e\u73b0\u9f20\u6807\u70b9\u51fb\u9875\u9762\u4e2d\u7684\u4efb\u610f\u6807\u7b7e\uff0calert \u8be5\u6807\u7b7e\u7684\u540d\u79f0(\u6ce8\u610f\u517c\u5bb9\u6027)"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7231\u8303\u513f"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 13, "qid": 699, "qtype": "short_answer", "short_answer": {"analysis": "#### Object.create()\n\n- Object.create()\u65b9\u6cd5\u4f7f\u7528\u6307\u5b9a\u7684\u539f\u578b\u5bf9\u8c61\u548c\u5176\u5c5e\u6027\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\u3002\n- Object.create(proto,[propertiesObject])\n- Object.create\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u7a7a\u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u7a7a\u5bf9\u8c61\u7684__proto__ = proto\uff0c\u5982\u679c\u8fd8\u6709propertiesObject\u53c2\u6570\u7684\u8bdd\uff0c\u5c31\u8fdb\u884cobject.assign\u7c7b\u4f3c\u7684\u64cd\u4f5c\uff0c\u628a\u5c5e\u6027\u8d4b\u4e0a\u53bb\u3002\n\n#### \u4ee3\u7801\u5b9e\u73b0\n\n**1\uff09\u7b80\u5355\u7c97\u66b4**\n\n```js\n// create \u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4f7f\u5176 __proto__ \u6307\u5411\u4f20\u5165\u7684\u539f\u578b\nfuction creat(obj){\n // \u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u6784\u9020\u51fd\u6570\n function F(){}\n // \u5c06\u6784\u9020\u51fd\u6570\u7684 prototype \u6307\u5411\u4f20\u5165\u7684\u5bf9\u8c61\n F.prototype = obj\n // \u8fd4\u56de\u65b0\u6784\u9020\u51fd\u6570\u7684\u5b9e\u4f8b\n retrun new F()\n}\n```\n\n**2\uff09\u5b9e\u73b0propertiesObject\u53c2\u6570**\n\n```js\nObject.create = function (prototype, properties) {\n if (typeof prototype !== \"object\") { \n throw TypeError(); }\n function Ctor() {}\n Ctor.prototype = prototype;\n var o = new Ctor();\n if (prototype) { \n o.constructor = Ctor; \n }\n if (properties !== undefined) {\n if (properties !== Object(properties)) { \n throw TypeError(); \n }\n Object.defineProperties(o, properties);\n }\n return o;\n};\n```", "answer": "", "biz_type": 1, "qid": 699, "subject": "", "title": "\u539f\u751f\u5b9e\u73b0 ES5 \u7684 Object.create()\u65b9\u6cd5"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 4, "qid": 767, "qtype": "short_answer", "short_answer": {"analysis": "#### 1.\u5b9e\u73b0\u65b9\u5f0f\u4e00\n\n- classList\n\n```js\ndocument.getElementById(\"vipemail\").classList.add(\"btn-active\");\n```\n\n#### 2.\u5b9e\u73b0\u65b9\u5f0f\u4e8c\n\n- className\n\n```js\nfunction isHasClassName(target,arr){\n for(var i in arr){\n if(target === arr[i]){\n return true;\n }\n }\n} \nfunction addClass(ele,addname){\n if(!ele.className){\n // class\u975e\u7a7a\u7684\u65f6\u5019,\u518d\u5224\u65ad\u8981\u6dfb\u52a0\u7684\u7c7b\u76ee\u662f\u4e0d\u662f\u5df2\u7ecf\u5b58\u5728\n ele.className = addname;\n //class\u540d\u4e3a\u7a7a\u7684\u65f6\u5019,\u76f4\u63a5\u8d4b\u503c \n }else{\n //\u975e\u7a7a\n if(!isHasClassName(addname,ele.className.split(\" \"))){\n //\u4e0d\u5b58\u5728\u8981\u6dfb\u52a0\u7684class\u540d\n ele.className += \" \" + addname;\n }\n\n }\n}\n```\n\n#### 2.\u5b9e\u73b0\u65b9\u5f0f\u4e09\n\n- className\n\n```js\nfunction hasClass(obj, cls) {\n return obj.className.match(new RegExp('(\\\\s|^)' + cls + '(\\\\s|$)'));\n}\n\nfunction addClass(obj, cls) {\n if (!this.hasClass(obj, cls)) obj.className += \" \" + cls;\n}\n```\n\n", "answer": "", "biz_type": 1, "qid": 767, "subject": "", "title": "\u539f\u751f\u5b9e\u73b0addClass,\u7528\u591a\u79cd\u65b9\u6cd5"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5b89\u5c45\u5ba2"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 5, "qid": 742, "qtype": "short_answer", "short_answer": {"analysis": "#### 1.\u56de\u8c03\u51fd\u6570\n\n\u56de\u8c03\u51fd\u6570\u662f\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u4f1a\u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u6267\u884c\u5b8c\u6210\u540e\u6267\u884c\u3002\n\n#### 2.\u4efb\u52a1\u961f\u5217\n\n\u4efb\u52a1\u961f\u5217\u662f\u4e00\u4e2a\u4e8b\u4ef6\u7684\u961f\u5217\uff0cIO\u8bbe\u5907\u5b8c\u6210\u4e00\u9879\u4efb\u52a1\u540e\uff0c\u5c31\u5728\u961f\u5217\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u8868\u793a\u76f8\u5173\u7684\u5f02\u6b65\u4efb\u52a1\u53ef\u4ee5\u8fdb\u5165\u6267\u884c\u6808\u4e86\u3002\n\n**\u540c\u6b65\u4efb\u52a1:** \u4e3b\u7ebf\u7a0b\u4e0a\u6392\u961f\u6267\u884c\u7684\u4efb\u52a1\uff0c\u524d\u4e00\u4e2a\u4efb\u52a1\u6267\u884c\u5b8c\u6210\u540e\u624d\u80fd\u6267\u884c\u4e0b\u4e00\u4e2a\u4efb\u52a1\u3002\n\n**\u5f02\u6b65\u4efb\u52a1:** \u4e0d\u8fdb\u5165\u4e3b\u7ebf\u7a0b\uff0c\u8fdb\u5165\u4efb\u52a1\u961f\u5217\u7684\u4efb\u52a1\u3002\u53ea\u6709\u5f53\u4e3b\u7ebf\u7a0b\u4e0a\u7684\u540c\u6b65\u4efb\u52a1\u6267\u884c\u5b8c\u6210\u540e\uff0c\u4e3b\u7ebf\u7a0b\u4f1a\u8bfb\u53d6\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u4efb\u52a1\uff0c\u5f00\u59cb\u5f02\u6b65\u6267\u884c\u3002\n\n\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u4e8b\u4ef6\u5305\u62ecIO\u8bbe\u5907\u7684\u4e8b\u4ef6\u3001\u7528\u6237\u4ea7\u751f\u7684\u4e8b\u4ef6\u3002\u53ea\u8981\u6307\u5b9a\u8fc7\u56de\u8c03\u51fd\u6570\uff0c\u8fd9\u4e9b\u4e8b\u4ef6\u53d1\u751f\u65f6\u5c31\u4f1a\u8fdb\u5165\u4efb\u52a1\u961f\u5217\uff0c\u7b49\u5f85\u4e3b\u7ebf\u7a0b\u8bfb\u53d6\u3002\n\n\u5f02\u6b65\u4efb\u52a1\u5fc5\u987b\u6307\u5b9a\u56de\u8c03\u51fd\u6570\uff0c\u5f53\u4e3b\u7ebf\u7a0b\u5f00\u59cb\u6267\u884c\u5f02\u6b65\u4efb\u52a1\uff0c\u5c31\u662f\u6267\u884c\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570\u3002", "answer": "", "biz_type": 1, "qid": 742, "subject": "", "title": "\u56de\u8c03\u51fd\u6570\u548c\u4efb\u52a1\u961f\u5217\u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7231\u8303\u513f"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 45, "qid": 703, "qtype": "short_answer", "short_answer": {"analysis": "#### 1.\u539f\u751f\u65b9\u5f0f\u5b9e\u73b0\n\n- **html**\n\n```html\n<body>\n <button id=\"expire1\">\u8fc7\u671f\u8bbe\u7f6e(\u66b4\u529b\u6cd5)</button>\n <button id=\"expire2\">\u8fc7\u671f\u8bbe\u7f6e(innerHTMl)</button>\n <ul id=\"wrap\"></ul>\n</body>\n```\n\n- **JavaScript**\n\n```js\n//\u751f\u6210\u5927\u91cfdom \nlet start = new Date().getTime()\nlet $ul = document.getElementById(\"wrap\");\n\nlet el = document.createDocumentFragment()\nlet allKeys = []\nfor(var i = 0; i < 1000; i++){\n let li = document.createElement('li');\n li.dataset.key = i //key\n li.innerHTML = i\n el.appendChild(li)\n allKeys.push(i)\n}\n$ul.appendChild(el)\n\n\n// \u751f\u6210\u8fc7\u671f\u9879 \u6a21\u62df\u670d\u52a1\u7aef\u751f\u6210\u7684\u6570\u636e\nfunction getExpireKeys(){\n let keys = []\n while(keys.length < 100){\n let randomKey = Math.floor(Math.random() * 1000)\n if(keys.indexOf(randomKey) === -1){\n keys.push(randomKey)\n }else{\n continue\n }\n }\n return keys\n}\n\n// \u66b4\u529b\u9879 \u9010\u9879\u904d\u5386\ndocument.getElementById('expire1').onclick = function(){\n let expireKeys = getExpireKeys()\n let children = $ul.children;\n let start = Date.now()\n for (let i = 0; i < expireKeys.length; i++) {\n const element = document.querySelector(`[data-key=\"${expireKeys[i]}\"]`);\n element.innerHTML = element.innerHTML + '\u5df2\u8fc7\u671f'\n }\n}\n\n//\u6a21\u677f\u5b57\u7b26\u4e32 innerHtml\u66ff\u6362\ndocument.getElementById('expire2').onclick = function(){\n let expireKeys = getExpireKeys()\n const item = []\n for (let i = 0; i < allKeys.length; i++) {\n item.push( `<li>${allKeys[i]} ${expireKeys.indexOf(allKeys[i]) !== -1 ? '\u5df2\u8fc7\u671f' : ''}</li>`)\n }\n $ul.innerHTML = item.join('')\n}\n```\n\n#### 2.Vue\u65b9\u5f0f\u5904\u7406\n\n```js\n// template\n<button @click=setExpire>\u8fc7\u671f</button>\n<ul>\n <li v-for=\"item in allKeys\" :key=\"item.value\">\n {{item.value}}\n {{item.expire ? '\u5df2\u8fc7\u671f' : ''}}\n </li>\n</ul>\n\n// script\n<script>\nexport default {\n data() {\n return {\n allKeys: [], //\u6240\u6709\u9879\n expireKeys: [] //\u8fc7\u671f\u9879\n }\n },\n created(){\n for(var i = 0; i < 1000; i++){\n this.allKeys.push({\n value: i,\n expire: false\n })\n }\n },\n methods: {\n setExpire(){\n let keys = this.getExpireKeys()\n for (let i = 0; i < this.allKeys.length; i++) {\n if(keys.indexOf(this.allKeys[i].value) !== -1){\n this.allKeys[i].expire = true\n }\n }\n },\n // \u751f\u6210\u8fc7\u671f\u9879 \u6a21\u62df\u670d\u52a1\u7aef\u751f\u6210\u7684\u6570\u636e\n getExpireKeys(){\n let keys = []\n while(keys.length < 100){\n let randomKey = Math.floor(Math.random() * 1000)\n if(keys.indexOf(randomKey) === -1){\n keys.push(randomKey)\n }else{\n continue\n }\n }\n return keys\n }\n },\n}\n</script>\n```\n", "answer": "", "biz_type": 1, "qid": 703, "subject": "```js\n/* \n a)\u5728\u4e0d\u4f7f\u7528vue\u3001react\u7684\u524d\u63d0\u4e0b\u5199\u4ee3\u7801\u89e3\u51b3\u4e00\u4e0b\u95ee\u9898\n \u4e00\u4e2aList\u9875\u9762\u4e0a\uff0c\u542b\u67091000\u4e2a\u6761\u76ee\u7684\u5f85\u529e\u5217\u8868\uff0c\u73b0\u5176\u4e2d100\u9879\u5728\u540c\u4e00\u65f6\u95f4\u8fbe\u5230\u4e86\u8fc7\u671f\u65f6\u95f4\uff0c\u9700\u8981\u5728\u5bf9\u5e94\u9879\u7684text-node\u91cc\u6dfb\u52a0\u201c\u5df2\u8fc7\u671f\u201d\u6587\u5b57\u3002\u9700\u8981\u5c3d\u53ef\u80fd\u51cf\u5c11dom\u91cd\u7ed8\u6b21\u6570\u4ee5\u63d0\u5347\u6027\u80fd\u3002\n b)\u5c1d\u8bd5\u4f7f\u7528vue\u6216react\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\n*/\n```\n", "title": "\u6309\u8981\u6c42\u5b8c\u6210\u9898\u76ee"}, "tech_tag": ["JavaScript", "Vue", "React", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 5, "qid": 768, "qtype": "short_answer", "short_answer": {"analysis": "### \u89e3\u51b3\u601d\u8def\n\n\u53ef\u4ee5\u4f7f\u7528setTimeout \u9012\u5f52\u81ea\u5faa\u73af\u8c03\u7528\u89e3\u51b3\u95ee\u9898\n\n\u5b9e\u73b0\u4ee3\u7801\n\n```js\ncountDown();\nfunction addZero(i){\n return i < 10 ? \"0\" + i : i + \"\";\n}\nfunction countDown(){\n var nowtime = new Date()\n var endtime = new Date(\"2020/10/25,17:57:00\");\n var lefttime = parseInt((endtime.getTime() - nowtime.getTime()) / 1000);\n var d = parseInt(lefttime / (24*60*60));\n var h = parseInt(lefttime / (60*60)%24);\n var m = parseInt(lefttime / 60%60);\n var s = parseInt(lefttime % 60);\n d = addZero(d);\n h = addZero(h);\n m = addZero(m);\n s = addZero(s);\n document.querySelector(\".count\").innerText = `\u6d3b\u52a8\u5012\u8ba1\u65f6 ${d} \u5929 ${h} \u65f6 ${m} \u5206 ${s} \u79d2 `;\n if(lefttime <= 0){\n document.querySelector('.count').innerText = \"\u6d3b\u52a8\u7ed3\u675f\";\n }\n setTimeout(countDown,1000);\n}\n```", "answer": "", "biz_type": 1, "qid": 768, "subject": "", "title": "\u5b9e\u73b0\u4e00\u4e2a\u5012\u8ba1\u65f6,setInterval\u5b9e\u73b0\u7684\u8bdd\uff0c\u5982\u4f55\u6d88\u9664\u65f6\u95f4\u8bef\u5dee"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7231\u8303\u513f"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 6, "qid": 704, "qtype": "short_answer", "short_answer": {"analysis": "### \u7ec4\u7ec7JavaScript\u4ee3\u7801\n\n\u7cbe\u5fc3\u8bbe\u8ba1\u7684\u4ee3\u7801\u66f4\u6613\u4e8e\u7ef4\u62a4\uff0c\u4f18\u5316\u548c\u6269\u5c55\uff0c\u80fd\u4f7f\u5f00\u53d1\u8005\u66f4\u9ad8\u6548\u3002\u8fd9\u610f\u5473\u7740\u66f4\u591a\u7684\u6ce8\u610f\u529b\u548c\u7cbe\u529b\u53ef\u4ee5\u82b1\u5728\u6784\u5efa\u4f1f\u5927\u7684\u4e8b\u60c5\u4e0a\uff0c\u6bcf\u4e2a\u4eba\u90fd\u5f88\u6109\u5feb\u2014\u2014\u7528\u6237\uff0c\u5f00\u53d1\u8005\u548c\u5229\u76ca\u76f8\u5173\u8005\u3002\n\n\u6bd4\u8f83\u5bbd\u677e\u7684\u8bed\u8a00\uff0c\u7279\u522b\u662f JavaScript \uff0c\u9700\u8981\u4e00\u4e9b\u89c4\u77e9\u624d\u80fd\u5199\u597d\n\nJavaScript \u73af\u5883\u975e\u5e38\u5bbd\u677e\uff0c\u968f\u5904\u6254\u4e9b\u4ee3\u7801\u7247\u6bb5\uff0c\u5c31\u53ef\u80fd\u8d77\u4f5c\u7528\u3002\u65e9\u70b9\u786e\u7acb\u7cfb\u7edf\u67b6\u6784\uff08\u7136\u540e\u9075\u5b88\u5b83\uff01\uff09\u5bf9\u4f60\u7684\u4ee3\u7801\u5e93\u63d0\u4f9b\u5236\u7ea6\uff0c\u786e\u4fdd\u81ea\u59cb\u81f3\u7ec8\u7684\u4e00\u81f4\u6027\u3002\n\n\u67093\u4e2a\u9ad8\u7ea7\u7684\uff0c\u8ddf\u8bed\u8a00\u65e0\u5173\u7684\u70b9\uff0c\u5bf9\u4ee3\u7801\u8bbe\u8ba1\u5341\u5206\u91cd\u8981\u3002\n\n#### 1.\u7cfb\u7edf\u67b6\u6784\n\n\u4ee3\u7801\u5e93\u7684\u57fa\u7840\u8bbe\u8ba1\u3002\u63a7\u5236\u5404\u79cd\u7ec4\u4ef6\u7684\u89c4\u5219\uff0c\u4f8b\u5982\u6a21\u5757\uff08models\uff09\uff0c\u89c6\u56fe\uff08views\uff09\u548c\u63a7\u5236\u5668\uff08controllers\uff09\uff0c\u4ee5\u53ca\u4e4b\u95f4\u7684\u76f8\u4e92\u4f5c\u7528\u3002\n\n#### 2.\u53ef\u7ef4\u62a4\u6027\n\n\u5982\u4f55\u66f4\u597d\u5730\u6539\u8fdb\u548c\u6269\u5c55\u4ee3\u7801\uff1f\n\n#### 3.\u590d\u7528\u6027\n\n\u5e94\u7528\u7ec4\u4ef6\u5982\u4f55\u590d\u7528\uff1f\u6bcf\u4e2a\u7ec4\u4ef6\u7684\u5b9e\u4f8b\u5982\u4f55\u7b80\u4fbf\u5730\u4e2a\u6027\u5b9a\u5236\uff1f\n\n### \u4e8c\u3001\u6a21\u5757\u6a21\u5f0f\n\n**\u6a21\u5757\u6a21\u5f0f** \u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7ed3\u6784\u57fa\u7840\uff0c\u5b83\u53ef\u4ee5\u8ba9\u4f60\u7684\u4ee3\u7801\u4fdd\u6301\u5e72\u51c0\u548c\u6761\u4f8b\u6e05\u6670\u3002\u4e00\u4e2a\u201c\u6a21\u5757\u201d\u5c31\u662f\u4e2a\u6807\u51c6\u7684\u5305\u542b\u65b9\u6cd5\u548c\u5c5e\u6027\u7684\u5bf9\u8c61\u5b57\u9762\u91cf\uff0c\u7b80\u5355\u662f\u8fd9\u4e2a\u6a21\u5f0f\u7684\u6700\u5927\u4eae\u70b9\uff1a\u751a\u81f3\u4e00\u4e9b\u4e0d\u719f\u6089\u4f20\u7edf\u7684\u8f6f\u4ef6\u8bbe\u8ba1\u6a21\u5f0f\u7684\u4eba\uff0c\u4e00\u770b\u5c31\u80fd\u7acb\u523b\u660e\u767d\u4ee3\u7801\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\n\n\u7528\u6b64\u6a21\u5f0f\u7684\u5e94\u7528\uff0c\u6bcf\u4e2a\u7ec4\u4ef6\u6709\u5b83\u72ec\u7acb\u7684\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u81ea\u52a8\u5b8c\u6210\u529f\u80fd\uff0c\u4f60\u8981\u5199\u4e2a\u6a21\u5757\u7528\u4e8e\u6587\u672c\u533a\u57df\uff0c\u4e00\u4e2a\u6a21\u5757\u7528\u4e8e\u7ed3\u679c\u5217\u8868\u3002\u4e24\u4e2a\u6a21\u5757\u76f8\u4e92\u5de5\u4f5c\uff0c\u4f46\u662f\u6587\u672c\u533a\u57df\u4ee3\u7801\u4e0d\u4f1a\u89e6\u53ca\u7ed3\u679c\u5217\u8868\u4ee3\u7801\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\n\n**\u6a21\u5757\u89e3\u8026** \u662f\u6a21\u5757\u6a21\u5f0f\u975e\u5e38\u9002\u4e8e\u6784\u5efa\u53ef\u9760\u7684\u7cfb\u7edf\u67b6\u6784\u7684\u539f\u56e0\u3002\u5e94\u7528\u95f4\u7684\u5173\u7cfb\u662f\u660e\u786e\u5b9a\u4e49\u7684\uff1b\u4efb\u4f55\u5173\u7cfb\u5230\u6587\u672c\u533a\u57df\u7684\u4e8b\u60c5\u88ab\u6587\u672c\u533a\u57df\u6a21\u5757\u7ba1\u7406\uff0c\u5e76\u4e0d\u662f\u6563\u843d\u5728\u4ee3\u7801\u5e93\u4e2d\u2014\u2014\u4ee3\u7801\u6574\u6d01\u3002\n\n\u6a21\u5757\u5316\u7ec4\u7ec7\u7684\u53e6\u4e00\u4e2a\u597d\u5904\u662f\u56fa\u6709\u7684\u53ef\u7ef4\u62a4\u6027\u3002\u6a21\u5757\u53ef\u4ee5\u72ec\u7acb\u5730\u6539\u8fdb\u548c\u4f18\u5316\uff0c\u4e0d\u4f1a\u5f71\u54cd\u5e94\u7528\u7684\u4efb\u4f55\u5176\u5b83\u90e8\u5206\u3002\n\n\u770b\u4e0b\u4e0b\u8fb9\u7684\u4f8b\u5b50\n\n#### 1.\u57fa\u7840 - \u51fd\u6570\u7248\uff1a\n\n```js\nfunction a(){};\nfunction b(){};\n```\n\n#### 2.\u5165\u95e8 - \u5b57\u9762\u91cf\u7248\uff1a\n\n```js\nvar obj = {\n init : function(){\n this.a();\n this.b();\n },\n a : function(){},\n b : function(){}\n}\n// \u5728\u9875\u9762\u4e2d\u8c03\u7528obj.init();\n```\n\n#### 3.\u8fdb\u9636 - \u547d\u540d\u7a7a\u95f4\u7248\uff1a\n\n```js\nvar hogo = {\n ns : function(){};\n}\n\nhogo.ns('hogo.wx', {\n init : function(){\n this.a();\n this.b();\n },\n a : function(){},\n b : function(){}\n});\nhogo.wx.init();\n```\n\n#### 4.\u63d0\u9ad8 - \u6a21\u5757\u5316\u7248\uff1a\n\n```js\ndefine();\nrequire();\n```", "answer": "", "biz_type": 1, "qid": 704, "subject": "", "title": "\u4f60\u662f\u5982\u4f55\u7ec4\u7ec7 JavaScript \u4ee3\u7801\u7684\uff1f\uff08\u53ef\u4ee5\u4ece\u6a21\u5757\u3001\u7ec4\u4ef6\u3001\u6a21\u5f0f\u3001\u7f16\u7a0b\u601d\u60f3\u7b49\u65b9\u9762\u56de\u7b54\uff09"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 9, "qid": 759, "qtype": "short_answer", "short_answer": {"analysis": "### \u7b54\u6848\n\n```js\nundefined \nUncaught TypeError: yideng is not a function\n```\n\n### \u89e3\u6790\n\n\u76f4\u63a5\u5728\u51fd\u6570\u4f53\u5185\u5b9a\u4e49\u7684\u51fd\u6570\u58f0\u660e\uff0c\u6574\u4e2a\u90fd\u4f1a\u63d0\u524d, \u4f46\u662f\u5728\u5757\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u58f0\u660e\uff0c\u53ea\u4f1a\u63d0\u5347\u5176\u58f0\u660e\u90e8\u5206\uff0c\u4e0d\u5206\u914d\u5b9e\u9645\u7684\u5185\u5b58\u7a7a\u95f4\u3002\n\n\u6240\u4ee5yideng\u88ab\u63d0\u5347\u7684\u53ea\u662f\u58f0\u660e\u7684\u51fd\u6570\u540d\u79f0\u53d8\u91cf\uff0c\u5e76\u672a\u5b9e\u9645\u8d4b\u503c\u3002\n\n\u4ee3\u7801\u7b49\u4ef7\u4e8e\n\n```js\nfunction yideng() {\n console.log(1);\n}\n(function () {\n var yideng;\n if (false) {\n function yideng() {\n console.log(2);\n }\n }\n console.log(typeof yideng);//undefined\n yideng();//is not a function\n})();\n```\n\n### \u77e5\u8bc6\u70b9\n\n\n#### 1.\u53d8\u91cf\u63d0\u5347\n\n\u53d8\u91cf\u7684\u63d0\u5347\u662f\u4ee5\u53d8\u91cf\u4f5c\u7528\u57df\u6765\u51b3\u5b9a\u7684\n\n- \u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u58f0\u660e\u7684\u53d8\u91cf\u4f1a\u63d0\u5347\u81f3\u5168\u5c40\u6700\u9876\u5c42\n- \u51fd\u6570\u5185\u58f0\u660e\u7684\u53d8\u91cf\u53ea\u4f1a\u63d0\u5347\u81f3\u8be5\u51fd\u6570\u4f5c\u7528\u57df\u6700\u9876\u5c42\u3002\n\n```js\nconsole.log(a);\nvar a = 10;\n// \u7b49\u4ef7\u4e8e\nvar a;\nconsole.log(a);\na = 10;\n```\n\n#### 2.\u51fd\u6570\u63d0\u5347\n\n\u51fd\u6570\u63d0\u5347\uff0c\u7c7b\u4f3c\u53d8\u91cf\u63d0\u5347\uff0c\u4f46\u662f\u786e\u6709\u4e9b\u8bb8\u4e0d\u540c\u3002\n\n**1\uff09\u51fd\u6570\u8868\u8fbe\u5f0f**\n\n```js\nconsole.log(a);// undefined\nvar a = function(){};\n```\n\n\u51fd\u6570\u8868\u8fbe\u5f0f\u4e0d\u4f1a\u58f0\u660e\u63d0\u5347\uff0c\u6240\u4ee5\u8fd9\u91cc\u8f93\u51faundefined,\u662fvar a\u53d8\u91cf\u58f0\u660e\u7684\u63d0\u5347\n\n**2\uff09\u51fd\u6570\u58f0\u660e**\n\n\u51fd\u6570\u58f0\u660e\u4f1a\u8986\u76d6\u53d8\u91cf\u58f0\u660e\uff0c\u56e0\u4e3a\u51fd\u6570\u662f\u4e00\u7b49\u516c\u6c11,\u4e0e\u5176\u4ed6\u503c\u5730\u4f4d\u76f8\u540c\uff0c\u6240\u4ee5\u51fd\u6570\u58f0\u660e\u4f1a\u8986\u76d6\u53d8\u91cf\u58f0\u660e\n\n\u5982\u679c\u5b58\u5728\u51fd\u6570\u58f0\u660e\u548c\u53d8\u91cf\u58f0\u660e\uff08\u6ce8\u610f\uff1a\u4ec5\u4ec5\u662f\u58f0\u660e\uff0c\u8fd8\u6ca1\u6709\u88ab\u8d4b\u503c\uff09\uff0c\u800c\u4e14\u53d8\u91cf\u540d\u8ddf\u51fd\u6570\u540d\u662f\u76f8\u540c\u7684\uff0c\u90a3\u4e48\uff0c\u5b83\u4eec\u90fd\u4f1a\u88ab\u63d0\u793a\u5230\u5916\u90e8\u4f5c\u7528\u57df\u7684\u5f00\u5934\uff0c\u4f46\u662f\uff0c\u51fd\u6570\u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\uff0c\u6240\u4ee5\u53d8\u91cf\u7684\u503c\u4f1a\u88ab\u51fd\u6570\u8986\u76d6\u6389\u3002\n\n- \u672a\u8d4b\u503c\u7684\u60c5\u51b5\n\n```js\nvar company;\nfunction company () {\nconsole.log (\"yideng\");\n}\nconsole.log(typeof company);// function\n// \u51fd\u6570\u58f0\u660e\u5c06\u53d8\u91cf\u58f0\u660e\u8986\u76d6\u4e86\n```\n\n- \u8d4b\u503c\u60c5\u51b5\n\n\u5982\u679c\u8fd9\u4e2a\u53d8\u91cf\u6216\u8005\u51fd\u6570\u5176\u4e2d\u662f\u8d4b\u503c\u4e86\u7684\uff0c\u90a3\u4e48\u53e6\u5916\u4e00\u4e2a\u5c06\u65e0\u6cd5\u8986\u76d6\u5b83\uff1a\n\n```js\nvar company = \"yideng\"; // \u53d8\u91cf\u58f0\u660e\u5e76\u8d4b\u503c\nfunction company () {\n console.log (\"yideng\");\n}\nconsole.log(typeof company); // string\n\n// \u4e0a\u8fb9\u7684\u4ee3\u7801\u7b49\u4ef7\u4e8e\nvar company;\nfunction company(){};\ncompany = 'yideng'; // \u88ab\u91cd\u65b0\u8d4b\u503c\nconsole.log(typeof company); // \u6240\u4ee5\u662fstring\n```\n\n#### 3.\u5757\u7ea7\u4f5c\u7528\u57df\u7684\u51fd\u6570\u58f0\u660e\n\n\u5728\u5757\u7ea7\u4f5c\u7528\u57df\u4e2d\u7684\u51fd\u6570\u58f0\u660e\u548c\u53d8\u91cf\u662f\u4e0d\u540c\u7684\n\n**1\uff09\u7ea7\u4f5c\u7528\u57df\u4e2d\u53d8\u91cf\u58f0\u660e**\n\n```js\nconsole.log(a); //ReferenceError: a is not defined\nif(true){\n a = 10;\n console.log(a);\n}\nconsole.log(a);\n// \u4f1a\u62a5\u9519\uff0c\n```\n\n**2\uff09\u5757\u7ea7\u4f5c\u7528\u57df\u51fd\u6570\u58f0\u660e**\n\n```js\nconsole.log(a); // \u8fd9\u91cc\u4e0d\u62a5\u9519\uff0c\u662fundefined\nif(true){\n console.log(a); // function a\n function a(){};\n}\n\n// \u4e0a\u8fb9\u7684\u4ee3\u7801\u7b49\u4ef7\u4e8e\nvar a; // \u51fd\u6570a\u7684\u58f0\u660e\nconsole.log(a); // undefined\nif(true){\n function a(){} // \u51fd\u6570a\u7684\u5b9a\u4e49\n console.log(a); // function a\n}\n```\n\n\u8fd9\u91cc\u5176\u5b9e\u5c31\u662f\u51fd\u6570 `function a(){}` \u7ecf\u8fc7\u9884\u89e3\u6790\u4e4b\u540e,\u5c06\u51fd\u6570\u58f0\u660e\u63d0\u5230\u51fd\u6570\u7ea7\u4f5c\u7528\u57df\u6700\u524d\u9762\uff0c`var a;// \u51fd\u6570a\u7684\u58f0\u660e` ,\u7136\u540e\u5c06\u51fd\u6570\u5b9a\u4e49\u63d0\u5347\u5230\u5757\u7ea7\u4f5c\u7528\u57df\u6700\u524d\u9762\uff0c `function a(){}` \u51fd\u6570a\u7684\u5b9a\u4e49\n\n**3\uff09\u6ce8\u610f**\n\n- \u5982\u679c\u6539\u53d8\u4e86\u4f5c\u7528\u57df\u5185\u58f0\u660e\u7684\u51fd\u6570\u7684\u5904\u7406\u89c4\u5219\uff0c\u663e\u7136\u4f1a\u5bf9\u8001\u4ee3\u7801\u4ea7\u751f\u5f88\u5927\u5f71\u54cd\u3002\u4e3a\u4e86\u51cf\u8f7b\u56e0\u6b64\u4ea7\u751f\u7684\u4e0d\u517c\u5bb9\u95ee\u9898\uff0ces6\u5728\u9644\u5f55B\u91cc\u9762\u89c4\u5b9a\uff0c\u6d4f\u89c8\u5668\u7684\u5b9e\u73b0\u53ef\u4ee5\u4e0d\u9075\u5b88\u4e0a\u9762\u89c4\u5b9a\uff0c\u6709\u81ea\u5df1\u7684\u884c\u4e3a\u65b9\u5f0f\n - \u5141\u8bb8\u5728\u5757\u7ea7\u4f5c\u7528\u57df\u5185\u58f0\u660e\u51fd\u6570\n - \u51fd\u6570\u58f0\u660e\u7c7b\u4f3c\u4e8evar,\u5373\u4f1a\u63d0\u5347\u5230\u5168\u5c40\u4f5c\u7528\u57df\u6216\u51fd\u6570\u4f5c\u7528\u57df\u7684\u5934\u90e8\n - \u540c\u65f6\uff0c\u51fd\u6570\u58f0\u660e\u8fd8\u4f1a\u63d0\u5347\u5230\u6240\u5728\u7684\u5757\u7ea7\u4f5c\u7528\u57df\u7684\u5934\u90e8\u3002\n- \u6ce8\u610f\uff0c\u4e0a\u9762\u4e09\u6761\u89c4\u5219\u53ea\u5bf9ES6\u7684\u6d4f\u89c8\u5668\u5b9e\u73b0\u6709\u6548\uff0c\u5176\u5b83\u73af\u5883\u7684\u5b9e\u73b0\u4e0d\u7528\u9075\u5b88\uff0c\u8fd8\u662f\u5c06\u5757\u7ea7\u4f5c\u7528\u57df\u7684\u51fd\u6570\u58f0\u660e\u5f53\u505alet\u5904\u7406\n- \u5757\u7ea7\u4f5c\u7528\u57df\u51fd\u6570\uff0c\u5c31\u50cf\u9884\u5148\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u4f7f\u7528`var`\u58f0\u660e\u4e86\u4e00\u4e2a\u53d8\u91cf\uff0c\u4e14\u9ed8\u8ba4\u503c\u4e3a`undefined`\u3002\n\n```js\nconsole.log(a,window.a); // undefined undefined\n{\n console.log(a,window.a); // function a(){} undefined\n function a(){}\n console.log(a,window.a); // function a(){} function a(){}\n}\nconsole.log(a,window.a);\t// function a(){} function a(){}\n```\n\n**4\uff09\u603b\u7ed3**\n\n- \u5757\u7ea7\u4f5c\u7528\u57df\u51fd\u6570\u5728\u7f16\u8bd1\u9636\u6bb5\u5c06\u51fd\u6570\u58f0\u660e\u63d0\u5347\u5230\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u5e76\u4e14\u4f1a\u5728\u5168\u5c40\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\uff0c\u503c\u4e3aundefined\u3002\u540c\u65f6\uff0c\u4e5f\u4f1a\u88ab\u63d0\u5347\u5230\u5bf9\u5e94\u7684\u5757\u7ea7\u4f5c\u7528\u57df\u9876\u5c42\u3002\n- \u5757\u7ea7\u4f5c\u7528\u57df\u51fd\u6570\u53ea\u6709\u5b9a\u4e49\u58f0\u660e\u51fd\u6570\u7684\u90a3\u884c\u4ee3\u7801\u6267\u884c\u8fc7\u540e\uff0c\u624d\u4f1a\u88ab\u6620\u5c04\u5230\u5168\u5c40\u4f5c\u7528\u57df\u3002\n\n#### 5.\u5757\u7ea7\u4f5c\u7528\u57df\u4e2d\u6709\u540c\u540d\u7684\u53d8\u91cf\u548c\u51fd\u6570\u58f0\u660e\n\n```js\nconsole.log(window.a,a);//undefined undefined\n{\n console.log(window.a,a);//undefined function a(){}\n function a() {};\n a = 10;\n console.log(window.a,a); //function a(){} 10\n};\nconsole.log(window.a,a); //function a(){} function a(){}\n```\n\n1. \u7b2c\u4e00\u4e2alog,\u5757\u7ea7\u4f5c\u7528\u57df\u51fd\u6570a\u7684\u58f0\u660e\u4f1a\u88ab\u63d0\u5347\u5230\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u4e0d\u62a5\u9519\uff0c\u662f `undefined undefined`\n2. \u7b2c\u4e8c\u4e2alog,\u5728\u5757\u7ea7\u4f5c\u7528\u57df\u4e2d\uff0c\u7531\u4e8e\u58f0\u660e\u51fd\u6570a\u63d0\u5347\u5230\u5757\u7ea7\u4f5c\u7528\u57df\u9876\u7aef,\u6240\u4ee5\u6253\u5370 `a = function a(){}`\uff0c\u800c `window.a`\u7531\u4e8e\u5e76\u6ca1\u6709\u6267\u884c\u51fd\u6570\u5b9a\u4e49\u7684\u90a3\u4e00\u884c\u4ee3\u7801\uff0c\u6240\u4ee5\u4ecd\u7136\u4e3a `undefined`\u3002\n3. \u7b2c\u4e09\u4e2alog,\u8fd9\u65f6\u5df2\u7ecf\u6267\u884c\u4e86\u58f0\u660e\u51fd\u6570\u5b9a\u4e49\uff0c\u6240\u4ee5\u4f1a\u628a\u51fd\u6570a\u6620\u5c04\u5230\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u3002\u6240\u4ee5\u8f93\u51fa `function a(){}`,\n4. \u7b2c\u56db\u4e2alog,\u5c31\u662f `function a(){} function a(){}`\uff0c\u56e0\u4e3a\u5728\u5757\u7ea7\u4f5c\u7528\u57df\u4e2dwindow.a\u7684\u503c\u5df2\u7ecf\u88ab\u6539\u53d8\u4e86\uff0c\u53d8\u6210\u4e86`function a(){}`\n\n**\u5757\u7ea7\u4f5c\u7528\u57df\u51fd\u6570\u53ea\u6709\u6267\u884c\u51fd\u6570\u58f0\u660e\u8bed\u53e5\u7684\u65f6\u5019\uff0c\u624d\u4f1a\u91cd\u5199\u5bf9\u5e94\u7684\u5168\u5c40\u4f5c\u7528\u57df\u4e0a\u7684\u540c\u540d\u53d8\u91cf\u3002**", "answer": "", "biz_type": 1, "qid": 759, "subject": "```js\nfunction yideng() {\n console.log(1);\n}\n(function () {\n if (false) {\n function yideng() {\n console.log(2);\n }\n }\n console.log(typeof yideng);\n yideng();\n})();\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6267\u2f8f\u7ed3\u679c\uff0c\u5e76\u89e3\u91ca\u4e3a\u4ec0\u4e48"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u65b0\u4e1c\u65b9"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 9, "qid": 751, "qtype": "short_answer", "short_answer": {"analysis": "### \u7b54\u6848\n\n```js\nstart\nchildren4\nchildren2\nchildren3\nchildren5\nchildren7\nchildren6\n```\n\n### \u89e3\u6790\n\n1. \u5148\u6267\u884c\u5b8f\u4efb\u52a1script\u811a\u672c\n2. console.log(\"start\")\u6267\u884c\n3. \u9047\u5230\u5b9a\u65f6\u5668\u4ea4\u7531\u5b9a\u65f6\u5668\u7ebf\u7a0b\uff0c\u7b49\u5f85\u65f6\u95f4\u5230\u4e86\u52a0\u5165\u961f\u5217\n4. \u9047\u5230Promise\u76f4\u63a5\u6267\u884cexecutor\uff0c\u6253\u5370console.log(\"children4\");\u9047\u5230\u7b2c\u4e8c\u5b9a\u65f6\u5668\uff0c\u53c8\u4ea4\u7531\u5b9a\u65f6\u5668\u7ebf\u7a0b\u7ba1\u7406\uff0c\u7b49\u5f85\u52a0\u5165\u961f\u5217\u3002Promise.then\u7b49resolve\u4e4b\u540e\u52a0\u5165\u5fae\u5bf9\u5217\u3002\u6b64\u65f6\u7b2c\u4e00\u8f6e\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\n5. \u7b2c\u4e00\u5b9a\u65f6\u5668\u5148\u8fdb\u5165\u961f\u5217\uff0c\u53d6\u51fa\u4efb\u52a1\u8fdb\u884c\u6267\u884cconsole.log(\"children2\");\u6b64\u65f6\u9047\u5230promise\u6267\u884c\uff0c\u5e76\u5c06promise.then\u653e\u5165\u5f53\u524d\u5b8f\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u5fae\u4efb\u52a1\u961f\u5217\u3002\u5f53\u524d\u4efb\u52a1\u8fd8\u884c\u5b8c\u6bd5\u3002\u6267\u884cthen\uff0c\u6253\u5370 console.log(\"children3\");\n6. \u53d6\u51fa\u7b2c\u4e8c\u5b9a\u65f6\u5668\uff0c\u6253\u5370console.log(\"children5\");\u5e76\u5c06then\u653e\u5165\u5fae\u4efb\u52a1\u4e2d\uff0c\u5f53\u524d\u5b8f\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\uff0c\u53d6\u51fathen\u6267\u884cchildren7\u3002\n7. \u53c8\u9047\u5230\u5b9a\u65f6\u5668\uff0c\u7531\u5b9a\u65f6\u5668\u7ebf\u7a0b\u7ba1\u7406\u7b49\u5f85\u65f6\u95f4\u5230\u4e86\u6dfb\u52a0\u5230\u5b8f\u4efb\u52a1\u4e2d\uff0c\n8. \u53d6\u51fa\u5b9a\u65f6\u5668\u4efb\u52a1\uff0c\u6253\u5370children6\n\n\n**\u539f\u7406**\n\n1. js\u662f\u5355\u7ebf\u7a0b\uff0c\u6700\u5927\u7279\u70b9\u5c31\u7ef4\u6301\u4e86\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af\n2. \u4e8b\u4ef6\u5faa\u73af\u7684\u7ec4\u6210\u7531\u4e3b\u7ebf\u7a0b\u548c\u4efb\u52a1\u961f\u5217\n3. \u6267\u884c\u65b9\u5f0f\u5c31\u662f\u4e3b\u7ebf\u7a0b\u4e0d\u505c\u4ece\u4efb\u52a1\u961f\u5217\u4e00\u4e2a\u4e00\u4e2a\u53d6\u51fa\u4efb\u52a1\u8fdb\u884c\u6267\u884c\n4. \u4efb\u52a1\u5206\u4e3a\u5b8f\u4efb\u52a1\u548c\u5fae\u4efb\u52a1\n5. \u6bcf\u4e2a\u5b8f\u4efb\u52a1\u5185\u90fd\u7ef4\u6301\u4e86\u4e00\u4e2a\u5fae\u4efb\u52a1\u961f\u5217\uff0c\u4e3a\u4e86\u8ba9\u9ad8\u4f18\u5148\u53ca\u4efb\u52a1\u53ca\u65f6\u6267\u884c\u3002\u4e5f\u5373\u662f\u6bcf\u53d6\u51fa\u4e00\u4e2a\u5b8f\u4efb\u52a1\uff0c\u6267\u884c\u5b8c\u6bd5\u4e4b\u540e\u3002\u68c0\u67e5\u5f53\u524d\u5b8f\u4efb\u52a1\u662f\u5426\u6709\u5fae\u4efb\u52a1\u53ef\u6267\u884c", "answer": "", "biz_type": 1, "qid": 751, "subject": "```js\nconsole.log(\"start\");\nsetTimeout(() => {\n console.log(\"children2\");\n Promise.resolve().then(() => {\n console.log(\"children3\");\n });\n}, 0);\nnew Promise(function (resolve, reject) {\n console.log(\"children4\");\n setTimeout(function () {\n console.log(\"children5\");\n resolve(\"children6\");\n }, 0);\n}).then((res) => {\n console.log(\"children7\");\n setTimeout(() => {\n console.log(res);\n }, 0);\n});\n```\n", "title": "\u5728\u6d4f\u89c8\u5668\u6267\u884c\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5199\u51fa\u6253\u5370\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 17, "qid": 757, "qtype": "short_answer", "short_answer": {"analysis": "### \u7b54\u6848\n\n```js\n'function a(){ alert(10) }''\n10\n3\ntypeError: a is not a function\n```\n\n### \u89e3\u6790\n\n**var\u3001function \u5747\u6709\u53d8\u91cf\u63d0\u5347, \u5b9e\u9645\u7f16\u8bd1\u540e\u4e3a\u5982\u4e0b\u4ee3\u7801\u6267\u884c:**\n\n```js\nfunction a(){alert(10)} // a: function...\nvar a; // a: undefined\nalert(a); // alert()\u5f39\u51faa\u4ee3\u8868function...\na(); // function a\u6267\u884c\uff1a alert(10); \na = 3; // a: 3\nalert(a); \na = 6; // a: 6\na(); // typeError\n```\n\n\u6240\u4ee5\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a\n\n- \u7b2c\u4e00\u6b65 alert\u8f93\u51fa\u7684\u662ffunction a\u7684\u51fd\u6570\u4f53\n- \u7b2c\u4e8c\u6b65 \u6267\u884cfunction a\uff0c\u5f39\u51fa10\n- \u7b2c\u4e09\u6b65 \u5c06a\u8d4b\u503c\u4e3a3\uff0c\u5219\u5f39\u51fa3\n- \u7b2c\u56db\u6b65 \u5c06a\u8d4b\u503c\u4e3a6\uff0c\u6267\u884c\u65f6\u5219\u4f1a\u62a5\u9519\uff0c\u8f93\u51fa `Uncaught TypeError: a is not a function`\n\n> \u6ce8\uff1a\n>\n>\u5982\u679c\u5b58\u5728\u51fd\u6570\u58f0\u660e\u548c\u53d8\u91cf\u58f0\u660e\uff08\u6ce8\u610f\uff1a\u4ec5\u4ec5\u662f\u58f0\u660e\uff0c\u8fd8\u6ca1\u6709\u88ab\u8d4b\u503c\uff09\uff0c\u800c\u4e14\u53d8\u91cf\u540d\u8ddf\u51fd\u6570\u540d\u662f\u76f8\u540c\u7684\uff0c\u90a3\u4e48\uff0c\u5b83\u4eec\u90fd\u4f1a\u88ab\u63d0\u793a\u5230\u5916\u90e8\u4f5c\u7528\u57df\u7684\u5f00\u5934\uff0c\u4f46\u662f\uff0c\u51fd\u6570\u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\uff0c\u6240\u4ee5\u53d8\u91cf\u7684\u503c\u4f1a\u88ab\u51fd\u6570\u8986\u76d6\u6389\u3002\n\n\u5728\u300a\u4f60\u4e0d\u77e5\u9053\u7684JavaScript\u300b\u4e0a\u518c\u7b2c\u4e00\u7ae0\u8bb2\u5230\uff1a\n\n> \u7f16\u8bd1\u5668\u4f1a\u8fdb\u884c\u5982\u4e0b\u5904\u7406\uff1a\n>\n> \u9047\u5230`var a`\uff0c\u7f16\u8bd1\u5668\u4f1a\u8be2\u95ee\u4f5c\u7528\u57df\u662f\u5426\u5df2\u7ecf\u6709\u4e00\u4e2a\u8be5\u540d\u79f0\u7684\u53d8\u91cf\u5b58\u5728\u4e8e\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u7684\u96c6\u5408\u4e2d\u3002\u5982\u679c\u662f\uff0c\u7f16\u8bd1\u5668\u4f1a\u5ffd\u7565\u8be5\u58f0\u660e\uff0c\u7ee7\u7eed\u8fdb\u884c\u7f16\u8bd1\uff1b\u5426\u5219\u4ed6\u4f1a\u8981\u6c42\u4f5c\u7528\u4e8e\u5728\u5f53\u524d\u4f5c\u7528\u57df\u7684\u96c6\u5408\u4e2d\u58f0\u660e\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\uff0c\u5e76\u547d\u540d\u4e3aa\u3002", "answer": "", "biz_type": 1, "qid": 757, "subject": "```js\nalert(a);\na();\nvar a = 3;\nfunction a() {\n alert(10);\n}\nalert(a);\na = 6;\na();\n```\n", "title": "\u8bf7\u5199\u51fa\u5f39\u51fa\u503c\uff0c\u5e76\u89e3\u91ca\u4e3a\u4ec0\u4e48\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5b89\u5c45\u5ba2"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 10, "qid": 741, "qtype": "short_answer", "short_answer": {"analysis": "### \u5b9e\u73b0\u65b9\u6848\n\n1. \u5728img\u5143\u7d20\u65f6\uff0c\u81ea\u5b9a\u4e49\u4e00\u4e2a\u5c5e\u6027data-src\uff0c\u7528\u4e8e\u5b58\u653e\u56fe\u7247\u7684\u5730\u5740\uff1b\n2. \u83b7\u53d6\u5c4f\u5e55\u53ef\u89c6\u533a\u57df\u7684\u5c3a\u5bf8\uff1b\n3. \u83b7\u53d6\u5143\u7d20\u5230\u7a97\u53e3\u8fb9\u7f18\u7684\u8ddd\u79bb\uff1b\n4. \u5224\u65ad\u5143\u7d20\u65f6\u5019\u5728\u53ef\u89c6\u533a\u57df\u5185\uff0c\u5728\u5219\u5c06data-src\u7684\u503c\u8d4b\u7ed9src,\u5426\u5219\uff0c\u4e0d\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\uff1b\n\n**\u5b9e\u8d28:** \u5f53\u56fe\u7247\u5728\u53ef\u89c6\u533a\u57df\u5185\u65f6\uff0c\u624d\u52a0\u8f7d\uff0c\u5426\u5219\u4e0d\u52a0\u8f7d\uff1b\u4e5f\u53ef\u4e00\u4e2a\u7ed9\u4e2a\u9ed8\u8ba4\u7684\u56fe\u7247\u5360\u4f4d\n\n**\u7528\u5230\u7684api:**\n\n- `IntersectionObserver` \u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f02\u6b65\u89c2\u5bdf\u76ee\u6807\u5143\u7d20\u4e0e\u9876\u7ea7\u6587\u6863viewport\u7684\u4ea4\u96c6\u4e2d\u7684\u53d8\u5316\u7684\u65b9\u6cd5\n- `window.requestIdleCallback()` \u65b9\u6cd5\u5c06\u5728\u6d4f\u89c8\u5668\u7684\u7a7a\u95f2\u65f6\u6bb5\u5185\u8c03\u7528\u7684\u51fd\u6570\u6392\u961f\u3002\u8fd9\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u5728\u4e3b\u4e8b\u4ef6\u5faa\u73af\u4e0a\u6267\u884c\u540e\u53f0\u548c\u4f4e\u4f18\u5148\u7ea7\u5de5\u4f5c\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u5ef6\u8fdf\u5173\u952e\u4e8b\u4ef6\uff0c\u5982\u52a8\u753b\u548c\u8f93\u5165\u54cd\u5e94\u3002\n\n**\u51e0\u4e2a\u7ec6\u8282:**\n\n- \u63d0\u524d\u52a0\u8f7d\uff0c\u53ef\u4ee5+100\u50cf\u7d20\n- \u6eda\u52a8\u65f6\u53ea\u5904\u7406\u672a\u52a0\u8f7d\u7684\u56fe\u7247\u5373\u53ef\uff1b\n- \u51fd\u6570\u8282\u6d41\n\n**\u7b80\u5355\u4ee3\u7801\u6f14\u793a**\n\n\u5224\u65ad\u662f\u5426\u662f\u5728\u53ef\u89c6\u533a\u57df\u7684\u4e09\u79cd\u65b9\u5f0f\n\n- \u5c4f\u5e55\u53ef\u89c6\u533a\u57df\u7684\u9ad8\u5ea6 + \u6eda\u52a8\u6761\u6eda\u52a8\u8ddd\u79bb > \u5143\u7d20\u5230\u6587\u6863\u9876\u90e8\u7684\u8ddd\u79bb\uff0c`document.documentElement.clientHeight + document.documentElement.scrollTop > element.offsetTop`\n- \u4f7f\u7528getBoundingClientRect()\u83b7\u53d6\u5143\u7d20\u5927\u5c0f\u548c\u4f4d\u7f6e\uff1b\n- IntersectionObserver \u81ea\u52a8\u89c2\u5bdf\u5143\u7d20\u662f\u5426\u5728\u89c6\u53e3\u5185\n\n```html\n<!DOCTYPE html>\n<html lang=\"en\">\n\n<head>\n <meta charset=\"UTF-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n <meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\" />\n <title>\u56fe\u7247\u61d2\u52a0\u8f7d</title>\n <style>\n img {\n display: block;\n height: 450px;\n margin-bottom: 20px;\n }\n </style>\n</head>\n\n<body>\n <img data-src=\"./images/1.png\" alt=\"\" />\n <img data-src=\"./images/2.png\" alt=\"\" />\n <img data-src=\"./images/3.png\" alt=\"\" />\n <img data-src=\"./images/4.png\" alt=\"\" />\n <img data-src=\"./images/5.png\" alt=\"\" />\n <img data-src=\"./images/6.png\" alt=\"\" />\n</body>\n<script>\n var imgs = document.querySelectorAll(\"img\");\n\n // \u8282\u6d41\u51fd\u6570,\u5b9a\u65f6\u5668\u7248\u672c\n function throttle(func, wait) {\n let timer = null;\n return function (...args) {\n if (!timer) {\n func(...args);\n timer = setTimeout(() => {\n timer = null;\n }, wait);\n }\n };\n }\n\n //\u65b9\u6cd51\uff1a H + S > offsetTop\n function lazyLoad1(imgs) {\n //offsetTop\u662f\u5143\u7d20\u4e0eoffsetParent\u7684\u8ddd\u79bb\uff0c\u5faa\u73af\u83b7\u53d6\u76f4\u5230\u9875\u9762\u9876\u90e8\n function getTop(e) {\n var T = e.offsetTop;\n while ((e = e.offsetParent)) {\n T += e.offsetTop;\n }\n return T;\n }\n var H = document.documentElement.clientHeight; //\u83b7\u53d6\u53ef\u89c6\u533a\u57df\u9ad8\u5ea6\n var S = document.documentElement.scrollTop || document.body.scrollTop;\n Array.from(imgs).forEach(function (img) {\n // +100 \u63d0\u524d100\u4e2a\u50cf\u7d20\u5c31\u5f00\u59cb\u52a0\u8f7d\n // \u5e76\u4e14\u53ea\u5904\u7406\u6ca1\u6709src\u5373\u6ca1\u6709\u52a0\u8f7d\u8fc7\u7684\u56fe\u7247\n if (H + S + 100 > getTop(img) && !img.src) {\n img.src = img.dataset.src;\n }\n });\n }\n const throttleLazyLoad1 = throttle(lazyLoad1, 200);\n\n // \u65b9\u6cd52\uff1ael.getBoundingClientRect().top <= window.innerHeight\n function lazyLoad2(imgs) {\n function isIn(el) {\n var bound = el.getBoundingClientRect();\n var clientHeight = window.innerHeight;\n return bound.top <= clientHeight + 100;\n }\n Array.from(imgs).forEach(function (img) {\n if (isIn(img) && !img.src) {\n img.src = img.dataset.src;\n }\n });\n }\n const throttleLazyLoad2 = throttle(lazyLoad2, 200);\n\n // \u6eda\u8f6e\u4e8b\u4ef6\u76d1\u542c\n // window.onload = window.onscroll = function () {\n // throttleLazyLoad1(imgs);\n // // throttleLazyLoad2(imgs);\n // };\n\n // \u65b9\u6cd53\uff1aIntersectionObserver\n function lazyLoad3(imgs) {\n const io = new IntersectionObserver((ioes) => {\n ioes.forEach((ioe) => {\n const img = ioe.target;\n const intersectionRatio = ioe.intersectionRatio;\n if (intersectionRatio > 0 && intersectionRatio <= 1) {\n if (!img.src) {\n img.src = img.dataset.src;\n }\n }\n img.onload = img.onerror = () => io.unobserve(img);\n });\n });\n imgs.forEach((img) => io.observe(img));\n }\n lazyLoad3(imgs);\n</script>\n\n</html>\n```", "answer": "", "biz_type": 1, "qid": 741, "subject": "", "title": "\u539f\u751f JavaScript \u5b9e\u73b0\u56fe\u7247\u61d2\u52a0\u8f7d\u7684\u601d\u8def"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 6, "qid": 776, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n```js\nfunction Animal(name, color) {\n this.name = name\n this.color = color\n}\nAnimal.prototype.say = function () {\n return `I'm a ${this.color}${this.name}`\n}\nFunction.prototype.bind = function (_this, arg) {\n const name = arg\n const callFn = this\n\n function fn(color) {\n callFn.call(this, name, color)\n }\n fn.prototype = Object.create(callFn.prototype)\n // \u5bc4\u751f\u7ec4\u5408\u5f0f\u7ee7\u627f\n fn.prototype.say = function () {\n return `I'm ${this.color} ${this.name}`\n }\n return fn\n}\nconst Cat = Animal.bind(null, 'cat')\nconst cat = new Cat('white')\nif (\n cat.say() === \"I'm white cat\" &&\n cat instanceof Cat &&\n cat instanceof Animal\n) {\n console.log('sunccess')\n}\n```", "answer": "", "biz_type": 1, "qid": 776, "subject": "```js\nfunction Animal(name,color){\n this.name = name;\n this.color = color;\n}\nAnimal.prototype.say = function(){\n return `I'm a ${this.color}${this.name}`;\n}\nconst cat = Animal.bind(null,'cat');\nconst cat = new Cat('white');\nif(cat.say() === \"I'm white cat\" && cat instanceof Cat && cat instanceof Animal){\n console.log('sunccess');\n}\n```\n", "title": "\u5b9e\u73b0Function \u539f\u578b\u7684bind\u65b9\u6cd5\uff0c\u4f7f\u5f97\u4ee5\u4e0b\u7a0b\u5e8f\u6700\u540e\u80fd\u8f93\u51fa\u201csuccess\u201d"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4e9a\u7f8e\u79d1\u6280"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 10, "qid": 721, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n```js\n// \u8bbe\u7f6ecookie\nfunction setCookie(name,value){\n var Days = 30;\n var exp = new Date();\n exp.setTime(exp.getTime() + Days*24*60*60*1000);\n document.cookie = name + \u201c=\u201d+ escape (value) + \u201c;expires=\u201d + exp.toGMTString();\n}\n\n// \u8bfb\u53d6cookie\nfunction getCookie(name){\n var arr,reg=new RegExp(\u201c(^| )\u201d+name+\u201d=([^;]*)(;|$)\u201d);\n if(arr=document.cookie.match(reg))\n return unescape(arr[2]);\n else\n return null;\n}\n\n// \u5220\u9664cookie\nfunction delCookie(name) {\n var exp = new Date();\n exp.setTime(exp.getTime() \u2013 1);\n var cval=getCookie(name);\n if(cval!=null){\n document.cookie= name + \u201c=\u201d+cval+\u201d;expires=\u201d+exp.toGMTString();\n }\n}\n```", "answer": "", "biz_type": 1, "qid": 721, "subject": "", "title": "\u4f7f\u7528 JavaScript \u5b9e\u73b0 cookie \u7684\u8bbe\u7f6e\u3001\u8bfb\u53d6\u3001\u5220\u9664"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 13, "qid": 707, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n- \u6b63\u5219\u5339\u914d\u7136\u540e\u8fdb\u884c\u66ff\u6362\n\n```js\nvar str = \"\u60a8\u597d\uff0c<%=name%>\u3002\u6b22\u8fce\u6765\u5230<%=location%>\";\n\nfunction template(str) {\n return data => str.replace(/<%=(\\w+)%>/g, (match, p) => data[p] || '')\n}\nvar compiled = template(str);\n// compiled\u7684\u8f93\u51fa\u503c\u4e3a\uff1a\u201c\u60a8\u597d\uff0c\u5f20\u4e09\u3002\u6b22\u8fce\u6765\u5230\u7f51\u6613\u6e38\u620f\u201d \ncompiled({\n name: \"\u5f20\u4e09\",\n location: \"\u7f51\u6613\u6e38\u620f\"\n});\n```\n", "answer": "", "biz_type": 1, "qid": 707, "subject": "```js\nvar str = \"\u60a8\u597d\uff0c<%=name%>\u3002\u6b22\u8fce\u6765\u5230<%=location%>\";\nfunction template(str) {\n // your code\n}\nvar compiled = template(srt);\n// compiled\u7684\u8f93\u51fa\u503c\u4e3a\uff1a\u201c\u60a8\u597d\uff0c\u5f20\u4e09\u3002\u6b22\u8fce\u6765\u5230\u7f51\u6613\u6e38\u620f\u201d\ncompiled({ name: \"\u5f20\u4e09\", location: \"\u7f51\u6613\u6e38\u620f\" });\n```\n", "title": "\u586b\u5145\u4ee3\u7801\u5b9e\u73b0 template \u65b9\u6cd5"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9ad8\u601d\u6559\u80b2"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 10, "qid": 728, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n- \u6b63\u5219\n\n```js\nString.prototype.trim = function(){\n return this.replace(/^\\s*|\\s*$/g,'');\n} \nconsole.log(' yd yd '.trim())\n```\n", "answer": "", "biz_type": 1, "qid": 728, "subject": "", "title": "\u7ed9 JavaScript \u7684 String \u539f\u751f\u5bf9\u8c61\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a trim \u7684\u539f\u578b\u65b9\u6cd5\uff0c\u7528\u4e8e\u622a\u53d6\u5b57\u7b26\u4e32\u524d\u540e\u7684\u7a7a\u767d\u5b57\u7b26"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4e9a\u7f8e\u79d1\u6280"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 12, "qid": 720, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n#### 1.\u666e\u901a\u7248\n\n- \u4f18\u70b9\uff1a\u6bd4for\u5faa\u73af\uff0cif-else\u5224\u65ad\u7684\u903b\u8f91\u6e05\u6670\u76f4\u767d\u4e00\u4e9b\n- \u7f3a\u70b9\uff1a\u592a\u666e\u901a\n\n```js\nfunction formatNumber(num){\n let arr = [],\n str = num + '';\n count = str.length;\n while(count >= 3){\n // \u5c06\u5b57\u7b26\u4e323\u4e2a\u4e00\u7ec4\u5b58\u5165\u6570\u7ec4\n arr.unshift(str.slice(count-3,count));\n count -= 3;\n }\n // \u5982\u679c\u4e0d\u662f3\u7684\u500d\u6570\u5c31\u53e6\u5916\u8ffd\u52a0\u5230\u6570\u7ec4\n str.length % 3 && arr.unshift(str.slice(0,str.length % 3));\n return arr.toString();\n}\nconsole.log(formatNumber(1234567890));\n```\n\n#### 2.\u8fdb\u9636\u7248\n\n- \u4f18\u70b9\uff1aJS\u7684API\u73a9\u7684\u4e86\u5982\u4e4b\u638c\n- \u7f3a\u70b9\uff1a\u53ef\u80fd\u6ca1\u90a3\u4e48\u597d\u61c2\uff0c\u4f46\u662f\u8bfb\u61c2\u4e4b\u540e\u5c31\u4f1a\u53d1\u51fa\u600e\u4e48\u6ca1\u60f3\u5230\u7684\u611f\u89c9\n\n```js\nfunction formatNumber(num){\n //str.split('').reverse() => [\"0\", \"9\", \"8\", \"7\", \"6\", \"5\", \"4\", \"3\", \"2\", \"1\"]\n return num.toString().split('').reverse().reduce((prev,next,index) => {\n return ((index % 3) ? next : (next + ',')) + prev\n })\n}\nconsole.log(formatNumber(1234567890));\n```\n\n#### 3.\u6b63\u5219\u7248\n\n- \u4f18\u70b9\uff1a\u4ee3\u7801\u5c11\uff0c\u6d53\u7f29\u7684\u90fd\u662f\u7cbe\u534e\n- \u7f3a\u70b9\uff1a\u9700\u8981\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f4d\u7f6e\u5339\u914d\u6709\u4e00\u4e2a\u8f83\u6df1\u7684\u8ba4\u8bc6\uff0c\u95e8\u69db\u5927\u4e00\u70b9\n\n\n```js\nfunction formatNumber(num) {\n /*\n \t\u2460/\\B(?=(\\d{3})+(?!\\d))/g\uff1a\u6b63\u5219\u5339\u914d\u975e\u5355\u8bcd\u8fb9\u754c\\B\uff0c\u5373\u9664\u4e861\u4e4b\u524d\u7684\u4f4d\u7f6e\uff0c\u5176\u4ed6\u5b57\u7b26\u4e4b\u95f4\u7684\u8fb9\u754c\uff0c\u540e\u9762\u5fc5\u987b\u8ddf\u77403N\u4e2a\u6570\u5b57\u76f4\u5230\u5b57\u7b26\u4e32\u672b\u5c3e\n\t\u2461(\\d{3})+\uff1a\u5fc5\u987b\u662f1\u4e2a\u6216\u591a\u4e2a\u76843\u4e2a\u8fde\u7eed\u6570\u5b57;\n\t\u2462(?!\\d)\uff1a\u7b2c2\u6b65\u4e2d\u76843\u4e2a\u6570\u5b57\u4e0d\u5141\u8bb8\u540e\u9762\u8ddf\u7740\u6570\u5b57;\n */\n return (num+'').replace(/\\B(?=(\\d{3})+(?!\\d))/g, ',')\n}\nconsole.log(formatNumber(1234567890)) // 1,234,567,890\n```\n\n#### 4.Api\u7248\n\n- \u4f18\u70b9\uff1a\u7b80\u5355\u7c97\u66b4\uff0c\u76f4\u63a5\u8c03\u7528 API\n- \u7f3a\u70b9\uff1aIntl\u517c\u5bb9\u6027\u4e0d\u592a\u597d\uff0c\u4e0d\u8fc7 toLocaleString\u7684\u8bdd IE6 \u90fd\u652f\u6301\n\n**1\uff09toLocaleString**\n\n\u65b9\u6cd5\u8fd4\u56de\u8fd9\u4e2a\u6570\u5b57\u5728\u7279\u5b9a\u8bed\u8a00\u73af\u5883\u4e0b\u7684\u8868\u793a\u5b57\u7b26\u4e32\uff0c\u5177\u4f53\u53ef\u770bMDN\u63cf\u8ff0\n\n```js\nfunction formatNumber(num){\n return num.toLocaleString('en-US');\n}\nconsole.log(formatNumber(1234567890));\n```\n\n**2\uff09IntL\u5bf9\u8c61**\n\n Intl \u5bf9\u8c61\u662f ECMAScript \u56fd\u9645\u5316 API \u7684\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\uff0c\u5b83\u63d0\u4f9b\u4e86\u7cbe\u786e\u7684\u5b57\u7b26\u4e32\u5bf9\u6bd4\uff0c\u6570\u5b57\u683c\u5f0f\u5316\uff0c\u65e5\u671f\u548c\u65f6\u95f4\u683c\u5f0f\u5316\u3002Collator\uff0cNumberFormat \u548c DateTimeFormat \u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\u662f Intl \u5bf9\u8c61\u7684\u5c5e\u6027\u3002\n\n```js\nfunction formatNumber(num){\n return new Intl.NumberFormat().format(num);\n}\nconsole.log(formatNumber(1234567890));\n```\n", "answer": "", "biz_type": 1, "qid": 720, "subject": "", "title": "\u5b9e\u73b0\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u6bd4\u5982\u8f93\u5165 999999999\uff0c\u8f93\u51fa 999,999,999"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7231\u8303\u513f"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 11, "qid": 698, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n#### 1.\u5e38\u89c4\u65b9\u6848\n\n**1\uff09\u89c4\u5219\u5b9a\u4e49**\n\n- \u4ee5\u5927\u5199\u5b57\u6bcd `[A-Z]`\u3001\u5c0f\u5199\u5b57\u6bcd `[a-z]` \u3001\u6570\u5b57 `[0-9]`\u3001\u4e0b\u6ed1\u7ebf `[_]`\u3001\u51cf\u53f7 `[-]`\u53ca\u70b9\u53f7 `[.]` \u5f00\u5934\uff0c\u5e76\u9700\u8981\u91cd\u590d\u4e00\u6b21\u81f3\u591a\u6b21`[+]`\u3002\n- \u4e2d\u95f4\u5fc5\u987b\u5305\u62ec `@` \u7b26\u53f7\u3002\n- `@` \u4e4b\u540e\u9700\u8981\u8fde\u63a5\u5927\u5199\u5b57\u6bcd `[A-Z]`\u3001\u5c0f\u5199\u5b57\u6bcd `[a-z]`\u3001\u6570\u5b57`[0-9]`\u3001\u4e0b\u6ed1\u7ebf`[_]`\u3001\u51cf\u53f7`[-]`\u53ca\u70b9\u53f7`[.]`\uff0c\u5e76\u9700\u8981\u91cd\u590d\u4e00\u6b21\u81f3\u591a\u6b21`[+]`\u3002\n- \u7ed3\u5c3e\u5fc5\u987b\u662f\u70b9\u53f7`[.]`\u8fde\u63a52\u81f34\u4f4d\u7684\u5927\u5c0f\u5199\u5b57\u6bcd`[A-Za-z]{2,4}`\u3002\n\n**2\uff09\u4ee3\u7801**\n\n```js\nvar pattern = /^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})$/;\n\npattern.test('cn42du@163.com') //true;\npattern.test('ifat3@sina.com.cn') // true;\npattern.test('ifat3.it@163.com') // true;\npattern.test('ifat3_-.@42du.cn') // true;\npattern.test('ifat3@42du.online') // false;\npattern.test('\u90ae\u7bb1@42du.cn') // false;\n\n```\n\n**3\uff09\u8bf4\u660e**\n\n\u8fd9\u662f\u6700\u5e38\u7528\u7684\u90ae\u4ef6\u6b63\u5219\u8868\u8fbe\u5f0f\u9a8c\u8bc1\u65b9\u6848\uff0c\u4e5f\u9002\u5408\u5927\u591a\u6570\u7684\u5e94\u7528\u573a\u666f\u3002\u4ece\u4ee5\u4e0a\u6d4b\u8bd5\u53ef\u4ee5\u770b\u51fa\uff0c\u8be5\u8868\u8fbe\u5f0f\u4e0d\u652f\u6301.online\u53ca.store\u7ed3\u5c3e\u7684\u57df\u540d\u3002\u5982\u9700\u517c\u5bb9\u8fd9\u7c7b\u57df\u540d\uff08\u5927\u4e8e4\u4f4d\uff09\uff0c\u8c03\u6574\u6b63\u5219\u7ed3\u5c3e\uff5b2,4\uff5d\u7684\u9650\u5236\u90e8\u5206\u5373\u53ef\uff08\u4f8b\uff1a{2,8}\uff09\u3002\u53e6\u4e00\u4e2a\u95ee\u9898\u662f\u90ae\u4ef6\u7528\u6237\u540d\u4e0d\u80fd\u5305\u62ec\u4e2d\u6587\u3002\n\n**4\uff09\u6dfb\u52a0\u89c4\u5219**\n\n- \u7528\u6237\u540d\u53ef\u4ee5\u5305\u62ec\u4e2d\u6587[\\u4e00-\\u9fa5]\n- \u57df\u540d\u7ed3\u5c3e\u6700\u957f\u53ef\u4e3a8\u4f4d{2,8}\n\n```js\nvar pattern = /^([A-Za-z0-9_\\-\\.\\u4e00-\\u9fa5])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,8})$/;\n\npattern.test('cn42du@163.com') //true;\npattern.test('ifat3@sina.com.cn') // true;\npattern.test('ifat3.it@163.com') // true;\npattern.test('ifat3_-.@42du.cn') // true;\npattern.test('ifat3@42du.online') // true;\npattern.test('\u90ae\u7bb1@42du.cn') // true;\n```\n\n#### 2.\u5b89\u5168\u65b9\u6848\n\n\u5728\u624b\u673a\u9a8c\u8bc1\u7801\u51fa\u73b0\u4e4b\u524d\uff0c\u5dee\u4e0d\u591a\u90ae\u7bb1\u9a8c\u8bc1\u662f\u4fdd\u8bc1\u7528\u6237\u552f\u4e00\u6027\u7684\u552f\u4e00\u6761\u4ef6\u3002\u800c\u4e34\u65f6\u90ae\u7bb1\uff08\u4e5f\u79f010\u5206\u949f\u90ae\u7bb1\u6216\u4e00\u6b21\u6027\u90ae\u7bb1\uff09\u7684\u51fa\u73b0\uff0c\u5219\u4f7f\u5f97\u90ae\u7bb1\u9a8c\u8bc1\u53ca\u5e10\u6237\u6fc0\u6d3b\u8fd9\u79cd\u673a\u5236\u5931\u53bb\u4e86\u610f\u4e49\u3002\u800c\u4e34\u65f6\u90ae\u7bb1\u7684\u5730\u5740\u662f\u4e0d\u53ef\u679a\u4e3e\u7684\uff0c\u6211\u4eec\u53ea\u80fd\u624d\u91c7\u53d6\u767d\u540d\u5355\u7684\u65b9\u5f0f\uff0c\u53ea\u5141\u8bb8\u6709\u9650\u7684\u90ae\u7bb1\u57df\u540d\u901a\u8fc7\u9a8c\u8bc1\u3002\n\n**1\uff09\u7ee7\u7eed\u6dfb\u52a0\u89c4\u5219**\n\n- \u90ae\u7bb1\u57df\u540d\u53ea\u80fd\u662f163.com\uff0cqq.com\u6216\u800542du.cn\u3002\n\n```js\nvar pattern = /^([A-Za-z0-9_\\-\\.])+\\@(163.com|qq.com|42du.cn)$/;\n```\n\n**2\uff09\u8bf4\u660e**\n\n\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u80fd\u4fdd\u8bc1\u5b89\u5168\u6027\uff0c\u4f46\u662f\u5982\u679c\u767d\u540d\u5355\u592a\u957f\u4f1a\u9020\u6210\u6a21\u5f0f\u5b57\u7b26\u4e32\u592a\u957f\u3002\u8fd9\u65f6\u53ef\u4ee5\u5c06\u90ae\u7bb1\u57df\u540d\u767d\u540d\u5355\u5199\u6210\u6570\u7ec4\uff0c\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u505a\u521d\u6b65\u9a8c\u8bc1\uff0c\u7528\u767d\u540d\u5355\u505a\u57df\u540d\u7684\u4e8c\u6b21\u9a8c\u8bc1\u3002\n\n**3\uff09\u4ee3\u7801\u6539\u5584**\n\n```js\nvar isEmail = function (val) {\n var pattern = /^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})$/;\n var domains= [\"qq.com\",\"163.com\",\"vip.163.com\",\"263.net\",\"yeah.net\",\"sohu.com\",\"sina.cn\",\"sina.com\",\"eyou.com\",\"gmail.com\",\"hotmail.com\",\"42du.cn\"];\n if(pattern.test(val)) {\n var domain = val.substring(val.indexOf(\"@\")+1);\n for(var i = 0; i< domains.length; i++) {\n if(domain == domains[i]) {\n return true;\n }\n }\n }\n return false;\n}\n// \u8f93\u51fa true\nisEmail(\"cn42du@163.com\");\n```\n\n", "answer": "", "biz_type": 1, "qid": 698, "subject": "", "title": "\u5b8c\u6210\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u662f\u5426\u662f\u7535\u5b50\u90ae\u7bb1"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u65b0\u4e1c\u65b9"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 13, "qid": 750, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n#### 1.\u5b9e\u73b0\u65b9\u5f0f\u4e00\n\n- \u88c5\u9970\u5668\u5199\u6cd5\n\n```js\nconst myDecorator = (fn, execute, obj=window) => {\n let old = obj[fn]\n obj[fn] = function() {\n return execute(old.bind(obj))\n }\n }\n \n function A() {\n console.log('\u8c03\u7528\u51fd\u6570A')\n }\n \n myDecorator('A', (fn) => {\n fn()\n console.log('hello world')\n })\n A();\n```\n\n#### 2.\u5b9e\u73b0\u65b9\u5f0f\u4e8c\n\n- \u6dfb\u52a0\u539f\u578b\u65b9\u6cd5\n\n```js\nFunction.prototype.before = function (beforeFN) {\n var _self = this;\n return function () {\n beforeFN.apply(_self, arguments)\n return _self.apply(this, arguments)\n }\n}\n\nFunction.prototype.after = function (afterFN) {\n var _self = this;\n return function () {\n var fn = _self.apply(this, arguments);\n afterFN.apply(_self, arguments)\n return fn\n }\n}\n\nvar A = function () {\n console.warn(\"\u8c03\u7528\u4e86\u51fd\u6570A\")\n}\n\nA = A.before(function () {\n console.warn(\"\u524d\u7f6e\u94a9\u5b50 HelloWorld\")\n}).after(function () {\n console.warn(\"\u540e\u7f6e\u94a9\u5b50 HelloWorld\")\n})\n\nA()\n```\n\n#### 3.\u5b9e\u73b0\u65b9\u5f0f\u4e09\n\n- \u7c97\u66b4\u76f4\u63a5\n\n```js\nfunction A() {\n console.log('\u8c03\u7528\u4e86\u51fd\u6570A');\n}\nconst nativeA = A;\nA = function () {\n console.log('HelloWorld');\n nativeA();\n}\nA()\n```\n\n", "answer": "", "biz_type": 1, "qid": 750, "subject": "```js\nfunction A() {\n console.log(\"\u8c03\u7528\u4e86\u51fd\u6570A\");\n}\n```\n", "title": "\u6709\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570 A,\u8981\u6c42\u5728\u4e0d\u6539\u53d8\u539f\u6709\u51fd\u6570 A \u529f\u80fd\u4ee5\u53ca\u8c03\u7528\u65b9\u5f0f\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u5f97\u6bcf\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u90fd\u80fd\u5728\u63a7\u5236\u53f0\u6253\u5370\u51fa\u201cHelloWorld\u201d"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["CVTE"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 6, "qid": 691, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ec0\u4e48\u662f\u5c3e\u9012\u5f52\n\n\u5c3e\u8c03\u7528\u662f\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5f53\u4e00\u4e2a\u51fd\u6570\u6267\u884c\u65f6\u7684\u6700\u540e\u4e00\u4e2a\u6b65\u9aa4\u662f\u8fd4\u56de\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u8c03\u7528\uff0c\u8fd9\u5c31\u53eb\u505a\u5c3e\u8c03\u7528\u3002\u5f53\u4e00\u4e2a\u51fd\u6570\u5c3e\u8c03\u7528\u81ea\u8eab\uff0c\u5c31\u53eb\u505a\u5c3e\u9012\u5f52\u3002\n\n**\u5c3e\u8c03\u7528\u4f18\u5316**\uff1a\u51fd\u6570\u5728\u8c03\u7528\u7684\u65f6\u5019\u4f1a\u5728\u8c03\u7528\u6808\uff08call stack\uff09\u4e2d\u5b58\u6709\u8bb0\u5f55\uff0c\u6bcf\u4e00\u6761\u8bb0\u5f55\u53eb\u505a\u4e00\u4e2a\u8c03\u7528\u5e27\uff08call frame\uff09\uff0c\u6bcf\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u5c31\u5411\u6808\u4e2dpush\u4e00\u6761\u8bb0\u5f55\uff0c\u51fd\u6570\u6267\u884c\u7ed3\u675f\u540e\u4f9d\u6b21\u5411\u5916\u5f39\u51fa\uff0c\u76f4\u5230\u6e05\u7a7a\u8c03\u7528\u6808\u3002\n\n> \u5c3e\u8c03\u7528\u4f18\u5316\u53ea\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\u6709\u6548\u3002\n\n### \u5c3e\u9012\u5f52\u5e94\u7528\n\n\u5c3e\u9012\u5f52\u7684\u5b9e\u73b0\uff0c\u5f80\u5f80\u9700\u8981\u6539\u5199\u9012\u5f52\u51fd\u6570\uff0c\u786e\u4fdd\u6700\u540e\u4e00\u6b65\u53ea\u8c03\u7528\u81ea\u8eab\u3002\n\n#### 1.\u9636\u4e58\u51fd\u6570\n\n```js\n'use strict';\nfunction factorial(n, total = 1) {\n if (n === 1) return total;\n return factorial(n - 1, n * total);\n}\n\nfactorial(5, 1); // 120\nfactorial(10, 1); // 3628800\nfactorial(500000, 1); // \u5206\u60c5\u51b5\n```\n\n> \u6ce8\u610f\uff0c\u867d\u7136\u8bf4\u8fd9\u91cc\u542f\u7528\u4e86\u4e25\u683c\u6a21\u5f0f\uff0c\u4f46\u662f\u7ecf\u6d4b\u8bd5\uff0c\u5728Chrome\u548cFirefox\u4e0b\uff0c\u8fd8\u662f\u4f1a\u62a5\u6808\u6ea2\u51fa\u9519\u8bef\uff0c\u5e76\u6ca1\u6709\u8fdb\u884c\u5c3e\u8c03\u7528\u4f18\u5316\n> \n> Safari\u6d4f\u89c8\u5668\u8fdb\u884c\u4e86\u5c3e\u8c03\u7528\u4f18\u5316\uff0cfactorial(500000, 1)\u7ed3\u679c\u4e3aInfinity\uff0c\u56e0\u4e3a\u7ed3\u679c\u8d85\u51fa\u4e86JS\u53ef\u8868\u793a\u7684\u6570\u5b57\u8303\u56f4\n> \n> \u5982\u679c\u5728node v6\u7248\u672c\u4e0b\u6267\u884c\uff0c\u9700\u8981\u52a0--harmony_tailcalls\u53c2\u6570\uff0cnode --harmony_tailcalls test.js\n> \n> node\u6700\u65b0\u7248\u672c\u5df2\u7ecf\u79fb\u9664\u4e86--harmony_tailcalls\u529f\u80fd\n\n#### 2.\u6590\u6ce2\u90a3\u5951\u6570\u5217\n\n```js\n/**\n * @params {n : \u5e8f\u5217\u53f7, pre: \u4e0a\u6b21\u5e8f\u5217\u548c, current:\u672c\u6b21\u5e8f\u5217\u548c}\n */\nconst Fibonacci = (n, pre = 1, current = 1) => {\n if (n <= 1) return current;\n return Fibonacci(n - 1, pre, pre + current);\n}\n```\n\n\n\n", "answer": "", "biz_type": 1, "qid": 691, "subject": "", "title": "\u5c3e\u9012\u5f52\u5b9e\u73b0"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7384\u6b66\u79d1\u6280"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 9, "qid": 719, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001\u6982\u5ff5\u7406\u89e3\n\n#### 1.\u4ec0\u4e48\u662f\u4e8b\u4ef6\u4ee3\u7406\n\n\u4e8b\u4ef6\u59d4\u6258\u6216\u4e8b\u4ef6\u4ee3\u7406\uff1a\u6839\u636e\u300ajs\u9ad8\u7ea7\u7a0b\u5e8f\u8bbe\u8ba1\u300b\u4e00\u4e66\uff08\u524d\u7aef\u7ea2\u5b9d\u4e66\uff09\u6765\u8bf4\u5c31\u662f\u5229\u7528\u4e8b\u4ef6\u5192\u6ce1\uff0c\u53ea\u6307\u5b9a\u4e00\u4e2a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u5c31\u53ef\u4ee5\u7ba1\u7406\u67d0\u4e00\u7c7b\u578b\u7684\u6240\u6709\u65f6\u95f4\u3002\u4e3e\u4e00\u4e2a\u6817\u5b50\uff1adom\u9700\u8981\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u6211\u4eec\u90fd\u4f1a\u76f4\u63a5\u7ed9\u5b83\u8bbe\u7f6e\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u3002but\uff0c\u5982\u679c\u6709\u5728ul\u4e2d\u5168\u90e8100\u4e2ali\u9700\u8981\u6dfb\u52a0\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u5176\u5177\u6709\u76f8\u540c\u7684\u70b9\u51fb\u4e8b\u4ef6\uff0c\u90a3\u4e48\u53ef\u4ee5\u6839\u636efor\u6765\u8fdb\u884c\u904d\u5386\uff0c\u4e5f\u53ef\u4ee5\u6839\u636e\u4e0a\u5c42\u7684ul\u6765\u8fdb\u884c\u6dfb\u52a0\u3002\u5728\u6027\u80fd\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u628aul\u5efa\u7acb\u4e8b\u4ef6\u4f1a\u51cf\u5c11dom\u7684\u4ea4\u4e92\u6b21\u6570\uff0c\u63d0\u9ad8\u6027\u80fd\u3002\n\n#### 2.\u4e8b\u4ef6\u4ee3\u7406\u539f\u7406\n\n\u4e8b\u4ef6\u59d4\u6258\u662f\u5229\u7528\u4e8b\u4ef6\u7684\u5192\u6ce1\u539f\u7406\u6765\u5b9e\u73b0\u7684\uff0c\u5c31\u662f\u4e8b\u4ef6\u4ece\u6700\u6df1\u7684\u8282\u70b9\u5f00\u59cb\uff0c\u7136\u540e\u9010\u6b65\u5411\u4e0a\u4f20\u64ad\u4e8b\u4ef6\u3002\n\n\u4e3e\u4e2a\u4f8b\u5b50\uff1a\u9875\u9762\u4e0a\u6709\u8fd9\u4e48\u4e00\u4e2a\u8282\u70b9\u6811\uff0cdiv>ul>li>a;\u6bd4\u5982\u7ed9\u6700\u91cc\u9762\u7684a\u52a0\u4e00\u4e2aclick\u70b9\u51fb\u4e8b\u4ef6\uff0c\u90a3\u4e48\u8fd9\u4e2a\u4e8b\u4ef6\u5c31\u4f1a\u4e00\u5c42\u4e00\u5c42\u7684\u5f80\u5916\u6267\u884c\uff0c\u6267\u884c\u987a\u5e8fa>li>ul>div\uff0c\u6709\u8fd9\u6837\u4e00\u4e2a\u673a\u5236\uff0c\u90a3\u4e48\u6211\u4eec\u7ed9\u6700\u5916\u9762\u7684div\u52a0\u70b9\u51fb\u4e8b\u4ef6\uff0c\u90a3\u4e48\u91cc\u9762\u7684ul\uff0cli\uff0ca\u505a\u70b9\u51fb\u4e8b\u4ef6\u7684\u65f6\u5019\uff0c\u90fd\u4f1a\u5192\u6ce1\u5230\u6700\u5916\u5c42\u7684div\u4e0a\uff0c\u6240\u4ee5\u90fd\u4f1a\u89e6\u53d1\uff0c\u8fd9\u5c31\u662f\u4e8b\u4ef6\u59d4\u6258\uff0c\u59d4\u6258\u5b83\u4eec\u7236\u7ea7\u4ee3\u4e3a\u6267\u884c\u4e8b\u4ef6\u3002\n\n\n### \u4e8c\u3001\u4ee3\u7801\u5b9e\u73b0\n\n#### 1.\u6bd4\u5982\u5b9e\u73b0ul\u4e2dli\u7684\u4e8b\u4ef6\u4ee3\u7406\n\n```js\nwindow.onload = function () {\n var oBtn = document.getElementById(\"btn\");\n var oUl = document.getElementById(\"ul1\");\n var aLi = oUl.getElementsByTagName('li');\n var num = 4;\n //\u4e8b\u4ef6\u59d4\u6258\uff0c\u6dfb\u52a0\u7684\u5b50\u5143\u7d20\u4e5f\u6709\u4e8b\u4ef6 \n oUl.onmouseover = function (ev) {\n var ev = ev || window.event;\n var target = ev.target || ev.srcElement;\n if (target.nodeName.toLowerCase() == 'li') {\n target.style.background = \"red\";\n }\n };\n oUl.onmouseout = function (ev) {\n var ev = ev || window.event;\n var target = ev.target || ev.srcElement;\n if (target.nodeName.toLowerCase() == 'li') {\n target.style.background = \"#fff\";\n }\n };\n //\u6dfb\u52a0\u65b0\u8282\u70b9 \n oBtn.onclick = function () {\n num++;\n var oLi = document.createElement('li');\n oLi.innerHTML = 111 * num;\n oUl.appendChild(oLi);\n };\n}\n}\n```\n\n\n#### 2.\u7b80\u5355\u5c01\u88c5\u4e00\u4e2a\u4e8b\u4ef6\u4ee3\u7406\u901a\u7528\u4ee3\u7801\n\n```js\n!function (root, doc) {\n class Delegator {\n constructor (selector) {\n this.root = document.querySelector(selector);//\u7236\u7ea7dom\n this.delegatorEvents = {};//\u4ee3\u7406\u5143\u7d20\u53ca\u4e8b\u4ef6\n //\u4ee3\u7406\u903b\u8f91\n this.delegator = e => { \n let currentNode = e.target;//\u76ee\u6807\u8282\u70b9\n const targetEventList = this.delegatorEvents[e.type];\n //\u5982\u679c\u5f53\u524d\u76ee\u6807\u8282\u70b9\u7b49\u4e8e\u4e8b\u4ef6\u5f53\u524d\u6240\u5728\u7684\u8282\u70b9\uff0c\u4e0d\u518d\u5411\u4e0a\u5192\u6ce1\n while (currentNode !== e.currentTarget) {\n targetEventList.forEach(target => {\n if (currentNode.matches(target.matcher)) {\n //\u5f00\u59cb\u59d4\u6258\u5e76\u628a\u5f53\u524d\u76ee\u6807\u8282\u70b9\u7684event\u5bf9\u8c61\u4f20\u8fc7\u53bb\n target.callback.call(currentNode, e);\n }\n })\n currentNode = currentNode.parentNode;\n }\n }\n }\n /*\n *\u7ed1\u5b9a\u4e8b\u4ef6\n *@param event \u7ed1\u5b9a\u4e8b\u4ef6\u7c7b\u578b\n *@param selector \u9700\u8981\u88ab\u4ee3\u7406\u7684\u9009\u62e9\u5668\n *@param fn \u89e6\u53d1\u51fd\u6570\n * */\n on (event, selector, fn) {\n //\u76f8\u540c\u4e8b\u4ef6\u53ea\u6dfb\u52a0\u4e00\u6b21\uff0c\u5982\u679c\u5b58\u5728\uff0c\u5219\u518d\u5bf9\u5e94\u7684\u4ee3\u7406\u4e8b\u4ef6\u91cc\u6dfb\u52a0\n if (!this.delegatorEvents[event]) {\n this.delegatorEvents[event] = [{\n matcher: selector,\n callback: fn\n }]\n this.root.addEventListener(event, this.delegator);\n }else{\n this.delegatorEvents[event].push({\n matcher: selector,\n callback: fn\n })\n }\n return this;\n }\n /*\n *\u79fb\u9664\u4e8b\u4ef6\n */\n destroy () {\n Object.keys(this.delegatorEvents).forEach(eventName => {\n this.root.removeEventListener(eventName, this.delegator)\n });\n }\n }\n\n root.Delegator = Delegator\n}(window, document)\n```", "answer": "", "biz_type": 1, "qid": 719, "subject": "", "title": "\u8bf7\u7528 JavaScript \u4ee3\u7801\u5b9e\u73b0\u4e8b\u4ef6\u4ee3\u7406"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7231\u8303\u513f"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 6, "qid": 694, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001\u5206\u6790\n\n\u56e0\u4e3a\u6bcf\u4e2a\u8bf7\u6c42\u5904\u7406\u65f6\u957f\u4e0d\u4e00\u81f4\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5148\u53d1\u9001\u7684\u8bf7\u6c42\u540e\u54cd\u5e94\uff0c\u5373\u8bf7\u6c42\u54cd\u5e94\u987a\u5e8f\u548c\u8bf7\u6c42\u53d1\u9001\u987a\u5e8f\u4e0d\u4e00\u81f4\uff0c\u4ece\u800c\u5bfc\u81f4\u6570\u636e\u663e\u793a\u4e0d\u6b63\u786e\u3002\n\n\u5373\u53ef\u4ee5\u7406\u89e3\u4e3a\u8fde\u7eed\u89e6\u53d1\u591a\u4e2a\u8bf7\u6c42\uff0c\u5982\u4f55\u4fdd\u8bc1\u8bf7\u6c42\u54cd\u5e94\u987a\u5e8f\u548c\u8bf7\u6c42\u53d1\u9001\u987a\u5e8f\u4e00\u81f4\u3002\u5bf9\u4e8e\u95ee\u9898\u6240\u5728\u573a\u666f\uff0c\u7528\u6237\u53ea\u5173\u5fc3\u6700\u540e\u6570\u636e\u662f\u5426\u663e\u793a\u6b63\u786e\uff0c\u5373\u53ef\u4ee5\u7b80\u5316\u4e3a\uff1a\u8fde\u7eed\u89e6\u53d1\u591a\u4e2a\u8bf7\u6c42\uff0c\u5982\u4f55\u4fdd\u8bc1\u6700\u540e\u54cd\u5e94\u7684\u7ed3\u679c\u662f\u6700\u540e\u53d1\u9001\u7684\u8bf7\u6c42\uff08\u4e0d\u5173\u6ce8\u4e4b\u524d\u7684\u8bf7\u6c42\u662f\u5426\u53d1\u9001\u6216\u8005\u54cd\u5e94\u6210\u529f\uff09\n\n\u7c7b\u4f3c\u573a\u666f\uff1ainput\u8f93\u5165\u6846\u5373\u65f6\u641c\u7d22\uff0c\u8868\u683c\u5feb\u901f\u5207\u6362\u9875\u7801\n\n### \u4e8c\u3001\u89e3\u51b3\u65b9\u6848\n\n\u9632\u6296\uff08\u8fc7\u6ee4\u6389\u4e00\u4e9b\u975e\u5fc5\u8981\u7684\u8bf7\u6c42\uff09 + \u53d6\u6d88\u4e0a\u6b21\u672a\u5b8c\u6210\u7684\u8bf7\u6c42\uff08\u4fdd\u8bc1\u6700\u540e\u4e00\u6b21\u8bf7\u6c42\u7684\u54cd\u5e94\u987a\u5e8f\uff09\n\n\u53d6\u6d88\u8bf7\u6c42\u65b9\u6cd5\uff1a\n\n- `XMLHttpRequest` \u4f7f\u7528 `abort` `api` \u53d6\u6d88\u8bf7\u6c42\n- `axios` \u4f7f\u7528 `cancel token` \u53d6\u6d88\u8bf7\u6c42\n\n\u4f2a\u4ee3\u7801\uff08\u4ee5 setTimeout \u6a21\u62df\u8bf7\u6c42\uff0cclearTimeout \u53d6\u6d88\u8bf7\u6c42\uff09\n\n```js\n/**\n * \u51fd\u6570\u9632\u6296\uff0c\u4e00\u5b9a\u65f6\u95f4\u5185\u8fde\u7eed\u89e6\u53d1\u4e8b\u4ef6\u53ea\u6267\u884c\u4e00\u6b21\n * @param {*} func \u9700\u8981\u9632\u6296\u7684\u51fd\u6570\n * @param {*} delay \u9632\u6296\u5ef6\u8fdf\n * @param {*} immediate \u662f\u5426\u7acb\u5373\u6267\u884c\uff0c\u4e3atrue\u8868\u793a\u8fde\u7eed\u89e6\u53d1\u65f6\u7acb\u5373\u6267\u884c\uff0c\u5373\u6267\u884c\u7b2c\u4e00\u6b21\uff0c\u4e3afalse\u8868\u793a\u8fde\u7eed\u89e6\u53d1\u540edelay ms\u540e\u6267\u884c\u4e00\u6b21\n */\nlet debounce = function(func, delay = 100, immediate = false) {\n let timeoutId, last, context, args, result\n\n function later() {\n const interval = Date.now() - last\n if (interval < delay && interval >= 0) {\n timeoutId = setTimeout(later, delay - interval)\n } else {\n timeoutId = null\n if (!immediate) {\n result = func.apply(context, args)\n context = args = null\n }\n }\n }\n\n return function() {\n context = this\n args = arguments\n last = Date.now()\n\n if (immediate && !timeoutId) {\n result = func.apply(context, args)\n context = args = null // \u89e3\u9664\u5f15\u7528\n }\n \n if (!timeoutId) {\n timeoutId = setTimeout(later, delay)\n }\n\n return result\n }\n}\n\n\nlet flag = false // \u6807\u5fd7\u4f4d\uff0c\u8868\u793a\u5f53\u524d\u662f\u5426\u6b63\u5728\u8bf7\u6c42\u6570\u636e\nlet xhr = null\n\nlet request = (i) => {\n if (flag) {\n clearTimeout(xhr)\n console.log(`\u53d6\u6d88\u7b2c${i - 1}\u6b21\u8bf7\u6c42`)\n }\n flag = true\n console.log(`\u5f00\u59cb\u7b2c${i}\u6b21\u8bf7\u6c42`)\n xhr = setTimeout(() => {\n console.log(`\u8bf7\u6c42${i}\u54cd\u5e94\u6210\u529f`)\n flag = false\n }, Math.random() * 200)\n}\n\nlet fetchData = debounce(request, 50) // \u9632\u6296\n\n// \u6a21\u62df\u8fde\u7eed\u89e6\u53d1\u7684\u8bf7\u6c42\nlet count = 1 \nlet getData = () => {\n setTimeout(() => {\n fetchData(count)\n count++\n if (count < 11) {\n getData()\n }\n }, Math.random() * 200)\n}\ngetData()\n\n/* \u67d0\u6b21\u6d4b\u8bd5\u8f93\u51fa\uff1a\n \u5f00\u59cb\u7b2c2\u6b21\u8bf7\u6c42\n \u8bf7\u6c422\u54cd\u5e94\u6210\u529f\n \u5f00\u59cb\u7b2c3\u6b21\u8bf7\u6c42\n \u53d6\u6d88\u7b2c3\u6b21\u8bf7\u6c42\n \u5f00\u59cb\u7b2c4\u6b21\u8bf7\u6c42\n \u8bf7\u6c424\u54cd\u5e94\u6210\u529f\n \u5f00\u59cb\u7b2c5\u6b21\u8bf7\u6c42\n \u8bf7\u6c425\u54cd\u5e94\u6210\u529f\n \u5f00\u59cb\u7b2c8\u6b21\u8bf7\u6c42\n \u53d6\u6d88\u7b2c8\u6b21\u8bf7\u6c42\n \u5f00\u59cb\u7b2c9\u6b21\u8bf7\u6c42\n \u8bf7\u6c429\u54cd\u5e94\u6210\u529f\n \u5f00\u59cb\u7b2c10\u6b21\u8bf7\u6c42\n \u8bf7\u6c4210\u54cd\u5e94\u6210\u529f\n*/\n```\n", "answer": "", "biz_type": 1, "qid": 694, "subject": "", "title": "\u591a\u4e2a tab \u53ea\u5bf9\u5e94\u4e00\u4e2a\u5185\u5bb9\u6846\uff0c\u70b9\u51fb\u6bcf\u4e2a tab \u90fd\u4f1a\u8bf7\u6c42\u63a5\u53e3\u5e76\u6e32\u67d3\u5230\u5185\u5bb9\u6846\uff0c\u600e\u4e48\u786e\u4fdd\u9891\u7e41\u70b9\u51fb tab \u4f46\u80fd\u591f\u786e\u4fdd\u6570\u636e\u6b63\u5e38\u663e\u793a\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7231\u8303\u513f"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 11, "qid": 693, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001\u4f18\u5316\u7b80\u8981\u7248\n\n**1\uff09\u61d2\u52a0\u8f7d:\u83b7\u53d6\u9996\u5c4f\u6570\u636e,\u540e\u8fb9\u7684\u6570\u636e\u8fdb\u884c\u6ed1\u52a8\u52a0\u8f7d\u8bf7\u6c42**\n\n1. \u9996\u5148\uff0c\u4e0d\u8981\u5c06\u56fe\u7247\u5730\u5740\u653e\u5230src\u5c5e\u6027\u4e2d\uff0c\u800c\u662f\u653e\u5230\u5176\u5b83\u5c5e\u6027(data-original)\u4e2d\u3002\n2. \u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u540e\uff0c\u6839\u636escrollTop\u5224\u65ad\u56fe\u7247\u662f\u5426\u5728\u7528\u6237\u7684\u89c6\u91ce\u5185\uff0c\u5982\u679c\u5728\uff0c\u5219\u5c06data-original\u5c5e\u6027\u4e2d\u7684\u503c\u53d6\u51fa\u5b58\u653e\u5230src\u5c5e\u6027\u4e2d\u3002\n3. \u5728\u6eda\u52a8\u4e8b\u4ef6\u4e2d\u91cd\u590d\u5224\u65ad\u56fe\u7247\u662f\u5426\u8fdb\u5165\u89c6\u91ce\uff0c\u5982\u679c\u8fdb\u5165\uff0c\u5219\u5c06data-original\u5c5e\u6027\u4e2d\u7684\u503c\u53d6\u51fa\u5b58\u653e\u5230src\u5c5e\u6027\u4e2d\n\n**2\uff09\u5229\u7528\u9aa8\u67b6\u5c4f\u63d0\u5347\u7528\u6237\u4f53\u9a8c**\n\n**3\uff09PreloadJS\u9884\u52a0\u8f7d**\n\n\u4f7f\u7528PreloadJS\u5e93\uff0cPreloadJS\u63d0\u4f9b\u4e86\u4e00\u79cd\u9884\u52a0\u8f7d\u5185\u5bb9\u7684\u4e00\u81f4\u65b9\u5f0f\uff0c\u4ee5\u4fbf\u5728HTML\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u3002\u9884\u52a0\u8f7d\u53ef\u4ee5\u4f7f\u7528HTML\u6807\u7b7e\u4ee5\u53caXHR\u6765\u5b8c\u6210\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPreloadJS\u4f1a\u5c1d\u8bd5\u4f7f\u7528XHR\u52a0\u8f7d\u5185\u5bb9\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5bf9\u8fdb\u5ea6\u548c\u5b8c\u6210\u4e8b\u4ef6\u7684\u66f4\u597d\u652f\u6301\uff0c\u4f46\u662f\u7531\u4e8e\u8de8\u57df\u95ee\u9898\uff0c\u4f7f\u7528\u57fa\u4e8e\u6807\u8bb0\u7684\u52a0\u8f7d\u53ef\u80fd\u66f4\u597d\u3002\n\n**4\uff09\u9664\u4e86\u6dfb\u52a0\u524d\u7aefloading\u548c\u8d85\u65f6404\u9875\u9762\u5916\uff0c\u63a5\u53e3\u90e8\u5206\u53ef\u4ee5\u6dfb\u52a0\u63a5\u53e3\u7f13\u5b58\u548c\u63a5\u53e3\u7684\u9884\u52a0\u8f7d**\n\n1. \u4f7f\u7528workbox\u5bf9\u6570\u636e\u8fdb\u884c\u7f13\u5b58 \u7f13\u5b58\u4f18\u5148\n2. \u4f7f\u7528orm\u5bf9\u672c\u5730\u79bb\u7ebf\u6570\u636e\u8fdb\u884c\u7f13\u5b58 \u4f18\u5148\u8bf7\u6c42\u672c\u5730\u3002\n3. \u91c7\u7528\u9884\u52a0\u8f7d \u518d\u8fdb\u5165\u5230\u8be6\u60c5\u9875\u9636\u6bb5\u4f7f\u7528quicklink\u9884\u52a0\u8f7d\u8be6\u60c5\u9875\n4. \u4f7f\u7528nodejs\u4f5c\u4e3a\u4e2d\u95f4\u5c42\u5c06\u8be6\u60c5\u9875\u6570\u636e\u7f13\u5b58\u81f3redis\u7b49\n\u4e0a\u9762\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u9009\u62e9\u7ec4\u5408\u4f7f\u7528\u3002\n\n\n\n\n\n### \u4e8c\u3001\u4f18\u5316\u8be6\u7ec6\u7248\n\n#### 1.\u6253\u5f00\u8c37\u6b4c\u641c\u7d22\u4e3a\u4f8b\n\n![load\u548cDOMContentLoad.png](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-693-devetool.png)\n\n\n- \u84dd\u8272\u7684\u5206\u754c\u7ebf\u5de6\u8fb9\u4ee3\u8868\u6d4f\u89c8\u5668\u7684 DOMContentLoaded\uff0c\u5f53\u521d\u59cb HTML \u6587\u6863\u5df2\u5b8c\u5168\u52a0\u8f7d\u548c\u89e3\u6790\u800c\u65e0\u9700\u7b49\u5f85\u6837\u5f0f\u8868\uff0c\u56fe\u50cf\u548c\u5b50\u5e27\u5b8c\u6210\u52a0\u8f7d\u65f6\u7684\u6807\u8bc6;\n- \u7ea2\u8272\u5206\u754c\u7ebf\u4ee3\u8868 load, \u5f53\u6574\u4e2a\u9875\u9762\u53ca\u6240\u6709\u4f9d\u8d56\u8d44\u6e90\u5982\u6837\u5f0f\u8868\u548c\u56fe\u7247\u90fd\u5df2\u5b8c\u6210\u52a0\u8f7d\u65f6\n\n\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u5927\u81f4\u5206\u4e3a\u5728 \n\n- **TTFB \u4e4b\u524d\u7684\u4f18\u5316**\n- **\u6d4f\u89c8\u5668\u4e0a\u9762\u6e32\u67d3\u7684\u4f18\u5316**\n\n#### 2.\u5f53\u7f51\u7edc\u8fc7\u6162\u65f6\u5728\u83b7\u53d6\u6570\u636e\u524d\u7684\u5904\u7406\n\n\u9996\u5148\u5148\u4e0a\u4e00\u5f20\u7ecf\u5178\u5230\u4e0d\u80fd\u518d\u7ecf\u5178\u7684\u56fe\n\n![timing-overview.png](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-693-performance.png)\n\n> \u5176\u4e2dcnd\u5728dns\u9636\u6bb5, dom\u6e32\u67d3\u5728processing onload\u9636\u6bb5\n\n\u4e0a\u56fe\u4ece promot for unload \u5230 onload \u7684\u8fc7\u7a0b\u8fd9\u4e48\u591a\u6b65\u9aa4, \u5728\u7528\u6237\u4f53\u9a8c\u6765\u8bf4, \u4e00\u4e2a\u9875\u9762\u4ece\u52a0\u8f7d\u5230\u5c55\u793a\u8d85\u8fc7 4 \u79d2, \u5c31\u4f1a\u6709\u4e00\u79cd\u975e\u5e38\u76f4\u89c2\u7684\u5361\u987f\u73b0\u8c61, \u5176\u4e2d load \u5bf9\u5e94\u7684\u4f4d\u7f6e\u662f onLoad \u4e8b\u4ef6\u7ed3\u675f\u540e, \u624d\u5f00\u59cb\u6784\u5efa dom \u6811, \u4f46\u662f\u7528\u6237\u4e0d\u4e00\u5b9a\u662f\u5173\u5fc3\u5f53\u524d\u9875\u9762\u662f\u5426\u662f\u5b8c\u6210\u4e86\u8d44\u6e90\u7684\u4e0b\u8f7d;\n\u5f80\u5f80\u662f\u4e00\u4e2a\u9875\u9762\u5f00\u59cb\u51fa\u73b0\u53ef\u89c1\u5143\u7d20\u5f00\u59cb**FCP \u9996\u6b21\u5185\u5bb9\u7ed8\u5236**\u6216\u8005\u662f**FC \u9996\u6b21\u7ed8\u5236** \u6b64\u65f6\u7528\u6237\u89c6\u89c9\u4f53\u9a8c\u5f00\u59cb, \u5230**TTI(\u53ef\u4ea4\u4e92\u65f6\u95f4)** , \u53ef\u4ea4\u4e92\u5143\u7d20\u7684\u51fa\u73b0, \u610f\u5473\u7740,\u7528\u6237\u4ea4\u4e92\u4f53\u9a8c\u5f00\u59cb, \u8fd9\u65f6\u5019\u7528\u6237\u5c31\u53ef\u4ee5\u6109\u5feb\u7684\u6d4f\u89c8\u4f7f\u7528\u6211\u4eec\u7684\u9875\u9762\u5566;\n\n\u6240\u4ee5\u8fd9\u4e2a\u95ee\u9898\u7684\u4e3b\u8981\u75db\u70b9\u662f\u9700\u8981\u7f29\u77ed\u5230\u8fbe **TTI** \u548c **FCP** \u7684\u65f6\u95f4\n\n\u4f46\u662f\u8fd9\u91cc\u5df2\u77e5\u8fdb\u5165\u6211\u4eec\u8be6\u60c5\u9875\u9762\u65f6, \u63a5\u53e3\u6570\u636e\u8fd4\u56de\u901f\u5ea6\u662f\u5f88\u6162\u7684, **FCP** \u548c **FC** , \u4ee5\u53ca\u52a0\u5feb\u5230\u8fbe **TTI** , \u5c31\u9700\u8981\u6211\u4eec\u9875\u9762\u9884\u5904\u7406\u4e86\n\n#### 3.\u9875\u9762\u6570\u636e\u7f13\u5b58\u5904\u7406(\u7f13\u5b58\u5927\u6cd5\u597d)\n\n**\u7b2c\u4e00\u6b21** \u8fdb\u5165\u8be6\u60c5\u9875\u9762, \u53ef\u4ee5\u4f7f\u7528\u9aa8\u67b6\u56fe\u8fdb\u884c\u6a21\u62df **FC** \u5c55\u793a, \u5e76\u4e14\u9aa8\u67b6\u56fe, \u53ef\u4f7f\u7528\u80cc\u666f\u56fe\u4e14\u884c\u5185\u6837\u5f0f\u7684\u65b9\u5f0f\u5bf9\u9996\u6b21\u8fdb\u5165\u8be6\u60c5\u9875\u9762\u8fdb\u884c\u5c55\u793a, \u5bf9\u4e8e\u8bf7\u6c42\u8fc7\u6162\u7684\u8be6\u60c5\u63a5\u53e3\u4f7f\u7528 **worker** \u8fdb\u7a0b, \u5bf9\u8be6\u60c5\u7684\u63a5\u53e3\u8bf7\u6c42\u4e22\u5230\u53e6\u5916\u4e00\u4e2a\u5de5\u4f5c\u7ebf\u7a0b\u8fdb\u884c\u8bf7\u6c42, \u9875\u9762\u6e32\u67d3\u5176\u4ed6\u5df2\u8fd4\u56de\u6570\u636e\u7684\u5143\u7d20; \u5f53\u5f88\u6162\u7684\u6570\u636e\u56de\u6765\u540e, \u9700\u8981\u5bf9\u9875\u9762\u6839\u636e\u5546\u54c1 id \u7b7e\u540d\u4e3a key \u8fdb\u884c webp \u6216\u8005\u662f\u7f29\u7565\u56fe\u5546\u54c1\u56fe\u7684 cnd \u8def\u5f84 localStorage \u7684\u7f13\u5b58, \u5546\u54c1 id \u7684\u7b7e\u540d\u7531\u653e\u5728 cookie \u5e76\u8bbe\u7f6e\u6210 httpOnly\n\n**\u975e\u7b2c\u4e00\u6b21** \u8fdb\u5165\u8be6\u60c5\u9875\u65f6, \u524d\u7aef\u53ef\u901a\u8fc7\u7279\u5b9a\u7684\u63a5\u53e3\u8bf7\u6c42\u56de\u6765\u5bf9\u5e94\u7684\u5546\u54c1 id \u7b7e\u540d\u7684 cookieid, \u8bfb\u53d6 localStorage \u7684\u5546\u54c1\u56fe\u7247\u7684\u7f13\u5b58\u6570\u636e, \u8fd9\u6837\u5bf9\u4e8e\u7b2c\u4e00\u6b21\u9aa8\u67b6\u56fe\u7684\u5c55\u793a\u65f6\u95f4\u5c31\u53ef\u4ee5\u7f29\u77ed, \u5feb\u901f\u5230\u8fbe **TTI** \u4e0e\u7528\u6237\u4ea4\u4e92\u7684\u65f6\u95f4, \u518d\u901a\u8fc7 worker \u6570\u636e, \u8fdb\u884c\u9ad8\u6e05\u56fe\u7247\u7684\u5207\u6362\n\n#### 4.\u8fc7\u671f\u7f13\u5b58\u6570\u636e\u7684\u5904\u7406(\u540e\u7aef\u63a7\u5236\u4e3a\u4e3b, LRU \u4e3a\u8f85)\n\n\u5bf9\u4e8e\u7f13\u5b58\u56fe\u7247\u5730\u5740\u7684\u5904\u7406, \u867d\u8bf4\u7f13\u5b58\u56fe\u7247\u662f\u653e\u5728 localStorage \u4e2d, \u4e0d\u4f1a\u7528\u5927\u5c0f\u9650\u5236, \u4f46\u662f\u592a\u591a\u4e5f\u662f\u4e0d\u597d\u7684, \u8fd9\u91cc\u4f7f\u7528 LRU \u7b97\u6cd5\u5bf9\u56fe\u7247\u4ee5\u53ca\u5176\u4ed6 localStorage \u8fdb\u884c\u6e05\u9664\u5904\u7406, \u5bf9\u4e8e\u8d85\u8fc7 7 \u5929\u7684\u6570\u636e\u8fdb\u884c\u6e05\u7406\nlocalStorage \u8be6\u60c5\u9875\u7684\u6570\u636e, \u6570\u636e\u7ed3\u6784\u5982\u4e0b:\n\n```js\n\"\u8bfb\u53d6\u540e\u7aef\u7684cookieID\": {\n \"path\": \"\u5bf9\u5e94cdn\u56fe\u7247\u7684\u5730\u5740\",\n \"time\": \"\u7f13\u5b58\u65f6\u95f4\u6233\",\n \"size\": \"\u5927\u5c0f\"\n}\n```\n\n#### 5.\u6570\u636e\u7f13\u5b58\u548c\u8fc7\u671f\u7f13\u5b58\u6570\u636e\u7684\u5904\u7406\u4e3b\u4f53\u6d41\u7a0b\n\n![\u8fdb\u5165\u5546\u54c1\u8be6\u60c5\u9875,\u63a5\u53e3\u6570\u636e\u5f88\u6162\u65f6,\u5bf9\u9875\u9762\u7684\u4f18\u5316](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-693-handle.png)\n\n#### 6.\u5bf9\u4e8e\u5927\u8bf7\u6c42\u91cf\u7684\u8bf7\u6c42(\u5982\u8be6\u60c5\u9875\u9762\u4e2d\u7684\u731c\u4f60\u559c\u6b22, \u63a8\u8350\u5546\u54c1\u7b49\u4e00\u4e9b\u5927\u6570\u636e\u91cf\u7684\u9759\u6001\u8d44\u6e90)\n\n1. \u7531\u4e8e\u8fd9\u4e9b\u4e0d\u5c5e\u4e8e\u7528\u6237\u8fdb\u5165\u8be6\u60c5\u60f3\u7b2c\u4e00\u65f6\u95f4\u83b7\u53d6\u7684\u4fe1\u606f, \u5373\u4e0d\u5c5e\u4e8e\u5f53\u524d\u9875\u9762\u7684\u76ee\u6807\u4e3b\u4f53, \u6240\u4ee5\u8fd9\u4e9b\u53ef\u4ee5\u4f7f\u7528 **Intersection Observer API** \u8fdb\u884c\u4e3b\u4f53\u5143\u7d20\u7684\u89c2\u5bdf, \u5f53\u5f53\u524d\u4e3b\u4f53\u5143\u7d20\u88ab\u52a0\u8f7d\u51fa\u6765\u540e, \u5728\u8fdb\u884c\u975e\u4e3b\u4f53\u5143\u7d20\u7684\u7f51\u7edc\u8d44\u6e90\u5206\u914d, \u5373\u7f51\u7edc\u7a7a\u95f2\u65f6\u518d\u8bf7\u6c42\u731c\u4f60\u559c\u6b22, \u63a8\u8350\u5546\u54c1\u7b49\u8d44\u6e90, \u5904\u7406\u8bf7\u6c42\u4f18\u5148\u7ea7\u7684\u95ee\u9898\n2. \u9700\u8981\u4fdd\u8bc1\u5f53\u524d\u8be6\u60c5\u9875\u7684\u8bf7\u6c42\u5217\u8868\u7684\u8bf7\u6c42\u6570 \u4e0d\u8d85\u8fc7\u5f53\u524d\u6d4f\u89c8\u5668\u7684\u8bf7\u6c42\u4e00\u4e2a tcp \u6700\u5927 http \u8bf7\u6c42\u6570\n\n#### 7.\u5f53 worker \u6570\u636e\u56de\u6765\u540e, \u51fa\u73b0 **\u5927\u91cf\u56fe\u7247** \u66ff\u6362\u5bf9\u5e94\u5143\u7d20\u7684\u7684 webp \u6216\u8005\u7f29\u7565\u56fe\u51fa\u73b0\u7684\u95ee\u9898(\u9759\u6001\u8d44\u6e90\u8fc7\u591a)\n\n\u8fd9\u91cc\u6709\u4e24\u79cd\u60c5\u666f\n\n1. \u79fb\u52a8\u7aef, \u5bf9\u4e8e\u79fb\u52a8\u7aef, \u4e00\u822c\u4e0d\u4f1a\u51fa\u73b0\u5927\u91cf\u56fe\u7247, \u4e00\u822c\u4e00\u4e2a\u5546\u54c1\u8be6\u60c5\u9875, \u4e0d\u4f1a\u8d85\u8fc7 100 \u5f20\u56fe\u7247\u8d44\u6e90; \u8fd9\u65f6\u5019, \u9009\u62e9\u61d2\u52a0\u8f7d\u65b9\u6848; \u6839\u636e GitHub \u73b0\u6709\u7684\u5f88\u591a\u65b9\u6848, \u5f53\u524d\u6ed1\u52a8\u5230\u53ef\u88ab\u89c2\u5bdf\u7684\u5143\u7d20\u540e\u624d\u52a0\u8f7d\u5f53\u524d\u53ef\u89c6\u533a\u57df\u7684\u56fe\u7247\u8d44\u6e90, \u540c\u6837\u4f7f\u7528\u7684\u662f **Intersection Observer API** ; \u6bd4\u5982 vue \u7684\u4e00\u4e2a\u5e93 **vue-lazy** , \u8fd9\u4e2a\u5e93\u5c31\u662f\u5bf9 Intersection_Observer_API \u8fdb\u884c\u5c01\u88c5, \u5bf9\u53ef\u89c6\u533a\u57df\u7684 img \u4fbf\u7b7e\u8fdb\u884c data-src \u548c src \u5c5e\u6027\u66ff\u6362\n\n2. \u7b2c\u4e8c\u4e2a\u60c5\u51b5, pc \u7aef, \u53ef\u80fd\u4f1a\u51fa\u73b0\u5927\u91cf\u7684 img \u6807\u7b7e, \u53ef\u80fd\u591a\u8fbe 300~400 \u5f20, \u8fd9\u65f6\u5019, \u4f7f\u7528\u61d2\u52a0\u8f7d, \u7528\u6237\u4f53\u9a8c\u5c31\u4e0d\u592a\u597d\u4e86; \u6bd4\u5982\u8bf4: \u5f53\u7528\u6237\u5728\u67e5\u770b\u5546\u54c1\u8bf4\u660e\u4ecb\u7ecd\u65f6, \u8fd9\u4e9b\u5546\u54c1\u8bf4\u660e\u548c\u4ecb\u7ecd\u6709\u53ef\u80fd\u53ea\u662f\u4e00\u5f20\u5f20\u56fe\u7247, \u5f53\u7528\u6237\u5f88\u5feb\u901f\u7684\u6ed1\u52a8\u65f6, \u9875\u9762\u8fd8\u6ca1\u61d2\u52a0\u8f7d\u5b8c, \u7528\u6237\u5c31\u6709\u53ef\u80fd\u770b\u4e0d\u5230\u60f3\u770b\u7684\u4fe1\u606f; \u9274\u4e8e\u4f1a\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5, \u8fd9\u91cc\u7ed9\u51fa\u4e00\u4e2a\u65b9\u6848\u5c31\u662f, img \u51fa\u73b0\u4e00\u5f20 load \u4e00\u5f20; \u5b9e\u73b0\u5982\u4e0b\uff1a\n\n```js\n// \u8fd9\u91cc\u9488\u5bf9\u975e\u7b2c\u4e00\u6b21\u8fdb\u5165\u8be6\u60c5\u9875,\n//\u5f53\u524dlocalStorage\u5df2\u7ecf\u6709\u4e86\u5f53\u524d\u8be6\u60c5\u9875\u5546\u54c1\u56fe\u7247\u7684\u7f29\u7565\u56fe\nfor(let i = 0; i < worker.img.length; i++) {\n // nodeList\u662f\u5bf9\u5e94img\u6807\u7b7e,\n // \u6ce8\u610f, \u8fd9\u91cc\u5bf9\u5e94\u7684nodeList\u4e00\u5b9a\u8981\u4f7f\u7528\u5185\u8054style\u628a\u4f4d\u7f6e\u5927\u5c0f\u8bbe\u7f6e\u597d, \u907f\u514d\u5927\u91cf\u7684\u91cd\u7ed8\u91cd\u6392\n const img = nodeList[i]\n img.src = worker.img['path'];\n img.onerror = () => {\n // \u5c06\u66ff\u6362\u5931\u8d25\u6216\u8005\u52a0\u8f7d\u5931\u8d25\u7684\u56fe\u7247\u964d\u7ea7\u5230\u7f29\u7565\u56fe, \n // \u5373\u7f13\u5b58\u5230localStorage\u7684\u7f29\u7565\u56fe\u6216\u8005webp\u56fe\n // \u517c\u5bb9\u5ba2\u6237\u7aef\u5904\u7406webp\u5931\u8d25\u7684\u60c5\u51b5\n }\n}\n```\n\n#### 8.\u9875\u9762\u91cd\u7ed8\u91cd\u6392\u5904\u7406\n\n![\u9875\u9762\u6e32\u67d3\u6d41\u7a0b](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-693-paint.png)\n\n\u89e6\u53d1\u91cd\u6392\u7684\u64cd\u4f5c\u4e3b\u8981\u662f\u51e0\u4f55\u56e0\u7d20\uff1a\n\n1. \u9875\u9762\u9996\u6b21\u8fdb\u5165\u7684\u6e32\u67d3\u3002\n2. \u6d4f\u89c8\u5668 resize\n3. \u5143\u7d20\u4f4d\u7f6e\u548c\u5c3a\u5bf8\u53d1\u751f\u6539\u53d8\u7684\u65f6\u5019\n4. \u53ef\u89c1\u5143\u7d20\u7684\u589e\u5220\n5. \u5185\u5bb9\u53d1\u751f\u6539\u53d8\n6. \u5b57\u4f53\u7684 font \u7684\u6539\u53d8\u3002\n7. css \u4f2a\u7c7b\u6fc0\u6d3b\u3002\n .....\n </br>\n \n\u5c3d\u91cf\u51cf\u5c11\u4e0a\u9762\u8fd9\u4e9b\u4ea7\u751f\u91cd\u7ed8\u91cd\u6392\u7684\u64cd\u4f5c\n\n\u6bd4\u5982\u8bf4\uff1a\n\n\u8fd9\u91cc\u4ea7\u751f\u5f88\u5927\u7684\u91cd\u7ed8\u91cd\u6392\u4e3b\u8981\u53d1\u751f\u5728 worker \u56de\u6765\u7684\u6570\u636e\u66ff\u6362\u9875\u9762\u4e2d\u7684\u56fe\u7247 src \u8fd9\u4e00\u6b65\n\n```js\n// \u8be5\u8282\u70b9\u4e3aimg\u6807\u7b7e\u7684\u7236\u8282\u70b9\nconst imgParent = docucment.getElementById('imgParent'); \n// \u514b\u9686\u5f53\u524d\u9700\u8981\u66ff\u6362img\u6807\u7b7e\u7684\u7236\u5143\u7d20\u4e0b\u6240\u6709\u7684\u6807\u7b7e\nconst newImgParent = imgParent.cloneNode(true); \nconst imgParentParent = docucment.getElementById('imgParentParent');\nfor(let i = 0; i < newImgParent.children.length; i++) { \n// \u6279\u91cf\u83b7\u53d6\u5b8c\u6240\u6709img\u6807\u7b7e\u540e, \u518d\u8fdb\u884c\u91cd\u7ed8\n newImgParent.children[i].src = worker.img[i].path;\n}\n// \u901a\u8fc7img\u7236\u8282\u70b9\u7684\u7236\u8282\u70b9, \u6765\u66ff\u6362\u6574\u4e2aimg\u7236\u8282\u70b9\n// \u5305\u62ec\u5bf9\u5e94\u7684\u6240\u6709\u5b50\u8282\u70b9, \u53ea\u8fdb\u884c\u4e00\u6b21\u91cd\u7ed8\u64cd\u4f5c\nimgParentParent.replaceChild(newImgParent, imgParent); \n```\n\n#### 9.css\u4ee3\u7801\u5904\u7406\n\n**\u6ce8\u610f\u88ab\u963b\u585e\u7684css\u8d44\u6e90**\n\n\u4f17\u6240\u5468\u77e5, css\u7684\u52a0\u8f7d\u4f1a\u963b\u585e\u6d4f\u89c8\u5668\u5176\u4ed6\u8d44\u6e90\u7684\u52a0\u8f7d, \u76f4\u81f3CSSOM **CSS OBJECT MODEL** \u6784\u5efa\u5b8c\u6210, \u7136\u540e\u518d\u6302\u5728DOM\u6811\u4e0a, \u6d4f\u89c8\u5668\u4f9d\u6b21\u4f7f\u7528\u6e32\u67d3\u6811\u6765\u5e03\u5c40\u548c\u7ed8\u5236\u7f51\u9875\u3002 \n\n\u5f88\u591a\u4eba\u90fd\u4e0b\u610f\u8bc6\u7684\u77e5\u9053, \u5c06css\u6587\u4ef6\u4e00\u5f8b\u653e\u5230head\u6807\u7b7e\u4e2d\u662f\u6bd4\u8f83\u597d\u7684, \u4f46\u662f\u4e3a\u4ec0\u4e48\u5c06css\u653e\u5728head\u6807\u7b7e\u662f\u6700\u540e\u4e86\u5462?\n\n\u6211\u4eec\u7528\u6dd8\u5b9d\u505a\u4f8b\u5b50\n\n![\u6ca1\u6709\u52a0\u8f7dcss\u7684\u6dd8\u5b9d\u9875\u9762](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-693-taobao.png)\n\u6bd4\u5982\u8fd9\u79cd\u6ca1\u6709css\u6837\u5f0f\u7684\u9875\u9762\u79f0\u4e4b\u4e3aFOUC(\u5185\u5bb9\u6837\u5f0f\u77ed\u6682\u5931\u6548), \u4f46\u662f\u8fd9\u79cd\u60c5\u51b5\u4e00\u822c\u51fa\u73b0\u5728ie\u7cfb\u5217\u4ee5\u53ca\u524d\u671f\u7684\u6d4f\u89c8\u5668\u8eab\u4e0a; \u5c31\u662f\u5f53cssom\u5728domtree\u751f\u6210\u540e, \u4f9d\u7136\u8fd8\u6ca1\u5b8c\u6210\u52a0\u8f7d\u51fa\u6765, \u5148\u5c55\u793a\u7eafhtml\u4ee3\u7801\u7684\u9875\u9762\u4e00\u4f1a\u518d\u51fa\u73b0\u6b63\u786e\u7684\u5e26css\u6837\u5f0f\u7684\u9875\u9762;\n\n**\u51cf\u5c11\u4e0d\u540c\u9875\u9762\u7684css\u4ee3\u7801\u52a0\u8f7d**\n\n\u5bf9\u4e8e\u7535\u5546\u9875\u9762, \u6709\u4e9b\u5728\u5934\u90e8\u7684css\u4ee3\u7801\u6709\u4e9b\u662f\u9996\u9875\u5c55\u793a\u7684\u6709\u4e9b\u662f\u7279\u5b9a\u60c5\u51b5\u624d\u5c55\u793a\u7684, \u6bd4\u5982\u5f53\u6211\u4eec\u9700\u8981\u51cf\u5c11\u4e00\u4e9bcss\u6587\u4ef6\u5927\u5c0f\u4f46\u662f\u5f53\u524d\u7f51\u7ad9\u53c8\u9700\u8981\u591a\u5c4f\u5c55\u793a, \u8fd9\u65f6\u5019, \u5f88\u591a\u4eba\u90fd\u4f1a\u60f3\u5230\u662f\u5a92\u4f53\u67e5\u8be2, \u6ca1\u9519\u65b9\u5411\u662f\u5bf9\u7684, \u4f46\u662f\u600e\u6837\u7684\u5a92\u4f53\u67e5\u8be2\u624d\u5bf9css\u6587\u4ef6\u4fdd\u6301\u8db3\u591f\u7684\u5c0f\u5462, \u53ef\u4ee5\u4f7f\u7528link\u6807\u7b7e\u5a92\u4f53\u67e5\u8be2,\u770b\u4e0b\u8fb9\u7684\u7684\u4f8b\u5b50\uff1a\n\n```html\n<link href=\"base.css\" rel=\"stylesheet\">\n<link href=\"other.css\" rel=\"stylesheet\" media=\"(min-width: 750px)\">\n```\n\n\u7b2c\u4e00\u4e2acss\u8d44\u6e90\u8868\u793a\u6240\u6709\u9875\u9762\u90fd\u4f1a\u52a0\u8f7d, \u7b2c\u4e8c\u4e2acss\u8d44\u6e90, \u5bbd\u5ea6\u5728750px\u624d\u4f1a\u52a0\u8f7d, \u9ed8\u8ba4media=\"all\"\n\n\u5728\u4e00\u4e9b\u9700\u6c42\u5199css\u5a92\u4f53\u67e5\u8be2\u7684\u7f51\u7ad9, \u4e0d\u8981\u5728css\u4ee3\u7801\u91cc\u9762\u5199, \u6700\u597d\u5199\u4e24\u5957css\u4ee3\u7801, \u901a\u8fc7link\u5a92\u4f53\u67e5\u8be2\u53bb\u52a8\u6001\u52a0\u8f7d, \u8fd9\u6837\u5c31\u80fd\u5f88\u597d\u7684\u51cf\u8f7b\u7f51\u7ad9\u52a0\u8f7dcss\u6587\u4ef6\u7684\u538b\u529b\n\n#### 10.\u9759\u6001js\u4ee3\u7801\u5904\u7406\n\n\u8fd9\u79cdjs\u4ee3\u7801, \u662f\u90a3\u4e9b\u5173\u4e8e\u57cb\u70b9, \u672c\u5730\u65e5\u8bb0, \u4ee5\u53ca\u52a8\u6001\u4fee\u6539css\u4ee3\u7801, \u8bfb\u53d6\u9875\u9762\u6210\u578b\u540e\u7684\u4fe1\u606f\u7684\u4e00\u4e9bjs\u4ee3\u7801, \u8fd9\u79cd\u4e00\u5f8b\u653e\u5728\u540c\u57df\u4e0b\u7684localStorage\u4e0a\u9762, \u4ec0\u4e48\u662f\u540c\u57df\u4e0b\u7684localStorage\n\n\u8fd9\u91cc\u8fd8\u662f\u4ee5\u5929\u732b\u4e3a\u4f8b\n\n![](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-693-tianmao.png)\n\n\n#### 11.\u5bb9\u9519\u5904\u7406\n\n1. \u9875\u9762\u5728\u83b7\u53d6\u5230 worker \u56de\u6765\u7684\u6570\u636e\u540e, \u901a\u8fc7\u62f7\u8d1d\u6574\u4e2ahtml\u7247\u6bb5, \u518d\u5c06worker\u7684img\u8def\u5f84\u5728\u66ff\u6362\u5bf9\u5e94\u7684 img \u8d44\u6e90\u540e\u518d\u8fdb\u884c\u8ffd\u52a0\u5230\u5bf9\u5e94\u7684dom\u8282\u70b9\n2. \u7f13\u5b58 css \u6587\u4ef6\u548c js \u6587\u4ef6\u5230 localStorage \u4e2d, \u82e5\u5f53\u524d\u6ca1\u6709\u5bf9\u5e94\u7684 css \u6587\u4ef6\u6216\u8005 js \u6587\u4ef6, \u6216\u8005\u88ab\u6076\u610f\u4fee\u6539\u8fc7\u7684 css \u6587\u4ef6\u6216\u8005 js \u6587\u4ef6(\u53ef\u4f7f\u7528\u7b7e\u540d\u8fdb\u884c\u5224\u65ad), \u5220\u9664\u518d\u83b7\u53d6\u5bf9\u5e94\u6587\u4ef6\u7684\u66f4\u65b0\n\n#### 12.\u63a8\u8350\u65b9\u6848\u7406\u7531\n\n1. \u4f7f\u7528\u4e86 worker \u7ebf\u7a0b\u8bf7\u6c42\u8be6\u60c5\u6570\u636e, \u4e0d\u5360\u7528\u6d4f\u89c8\u5668\u4e3b\u7ebf\u7a0b; \u8fdb\u800c\u51cf\u5c11\u4e3b\u8fdb\u7a0b\u6d88\u8017\u5728\u7f51\u7edc\u7684\u65f6\u95f4\n2. \u4f7f\u7528 localStorage \u7684\u7f13\u5b58\u673a\u5236, \u56e0\u4e3a\u5f53 worker \u56de\u6765\u7684\u6570\u636e\u540e, \u8bfb\u53d6 localStorage \u662f\u540c\u6b65\u8bfb\u53d6\u7684, \u57fa\u672c\u4e0d\u4f1a\u6709\u592a\u5927\u7684\u7b49\u5f85\u65f6\u95f4, \u5e76\u4e14\u8bfb\u53d6 localStorage \u65f6, \u4f7f\u7528\u7684\u662f\u540e\u7aef\u8fd4\u56de\u6765\u7684 cookieID \u8fdb\u884c\u8bfb\u53d6, \u4e14\u672c\u5730\u7684 cookID \u662f httpOnly \u907f\u514d\u4e86\u7b2c\u4e09\u65b9\u83b7\u53d6\u5230 cookieID \u8fdb\u884c\u8bfb\u53d6\u5546\u54c1\u4fe1\u606f\n3. \u4f7f\u7528 LRU \u6e05\u9664\u8fc7\u591a\u7684\u7f13\u5b58\u6570\u636e\n4. \u9996\u6b21\u8fdb\u5165\u9875\u9762\u65f6, \u4fdd\u8bc1\u5df2\u77e5\u9875\u9762\u5e03\u5c40\u60c5\u51b5\u4e0b\u7684\u5feb\u901f\u6e32\u67d3\u4ee5\u53ca\u914d\u7f6e\u9aa8\u67b6\u56fe, \u52a0\u5feb\u5230\u8fbe FCP \u548c FP \u7684\u65f6\u95f4\n5. \u5c31\u7b97 img \u9759\u6001\u8d44\u6e90\u8fc7\u5927, \u5728\u7b2c\u4e8c\u6b21\u8fdb\u5165\u8be5\u9875\u9762\u7684\u65f6\u5019, \u4e5f\u53ef\u4ee5\u505a\u5230\u4f4e\u6b21\u6570\u91cd\u7ed8\u91cd\u6392, \u52a0\u5feb\u5230\u5e95 TTI \u7684\u65f6\u95f4\n\n#### 13.\u65b9\u6848\u4e0d\u8db3\n\n1. \u5728\u7f51\u7edc\u4f9d\u7136\u5f88\u6162\u7684\u60c5\u51b5\u4e0b, \u9996\u6b21\u8fdb\u5165\u8be6\u60c5\u9875\u9762, \u5982\u679c\u957f\u65f6\u95f4\u7684\u9aa8\u67b6\u56fe\u548c\u5df2\u77e5\u5e03\u5c40\u4e0b, \u7528\u6237\u7684\u4f53\u9a8c\u4f9d\u7136\u662f\u4e0d\u597d\u7684, \u8fd9\u91cc\u53ef\u4ee5\u8003\u8651 PWA \u65b9\u6848, \u5bf9\u6700\u8fd1\u4e00\u6b21\u6210\u529f\u8bf7\u6c42\u7684\u5185\u5bb9\u8fdb\u884c\u52ab\u6301, \u5e76\u5728\u65e0\u7f51\u60c5\u51b5\u4e0b, \u505a\u51fa\u76f8\u5e94\u7684\u63d0\u793a\u548c\u5c55\u793a\u5904\u7406\n2. \u9700\u8981 UI \u90a3\u8fb9\u63d0\u4f9b\u4e09\u5957\u9759\u6001 img \u8d44\u6e90\n", "answer": "", "biz_type": 1, "qid": 693, "subject": "", "title": "\u5546\u57ce\u7684\u5217\u8868\u9875\u8df3\u8f6c\u5230\u5546\u54c1\u7684\u8be6\u60c5\u9875\uff0c\u8be6\u60c5\u9875\u6570\u636e\u63a5\u53e3\u5f88\u6162\uff0c\u524d\u7aef\u53ef\u4ee5\u600e\u4e48\u4f18\u5316\u7528\u6237\u4f53\u9a8c\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7231\u8303\u513f"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 6, "qid": 701, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001\u4ee3\u7801\u6267\u884c\u7684\u9519\u8bef\u6355\u83b7\n\n**1.try\u2026\u2026catch**\n\n\u4f7f\u7528try... catch \u5305\u88f9\uff0c\u5f71\u54cd\u4ee3\u7801\u53ef\u8bfb\u6027\u3002\u65e0\u6cd5\u5904\u7406\u5f02\u6b65\u4e2d\u7684\u9519\u8bef\u65e0\u6cd5\u5904\u7406\u8bed\u6cd5\u9519\u8bef\n\n**2.window.onerrorwindow.onerror**\n\n\u6bd4`try catch`\u8981\u5f3a\u90a3\u4e48\u4e00\u4e22\u4e22\u3002\u65e0\u8bba\u662f\u5f02\u6b65\u8fd8\u662f\u975e\u5f02\u6b65\u9519\u8bef\uff0conerror \u90fd\u80fd\u6355\u83b7\u5230\u8fd0\u884c\u65f6\u9519\u8bef\n\n**\u7f3a\u70b9:** \u76d1\u542c\u4e0d\u5230\u8d44\u6e90\u52a0\u8f7d\u7684\u62a5\u9519onerror,\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u53ea\u80fd\u58f0\u660e\u4e00\u6b21\uff0c\u4e0d\u4f1a\u91cd\u590d\u6267\u884c\u591a\u4e2a\u56de\u8c03\uff1a\n\n**3.window.addEventListener('error')**\n\n\u53ef\u4ee5\u76d1\u542c\u5230\u8d44\u6e90\u52a0\u8f7d\u62a5\u9519\uff0c\u4e5f\u53ef\u4ee5\u6ce8\u518c\u591a\u4e2a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002\n\n**4.window.addEventListener('unhandledrejection')**\n\n\u6355\u83b7Promise\u9519\u8bef\n\n### \u4e8c\u3001\u8d44\u6e90\u52a0\u8f7d\u7684\u9519\u8bef\u6355\u83b7\n\n1. `imgObj.onerror()`\n2. `performance.getEntries()`\uff0c\u83b7\u53d6\u5230\u6210\u529f\u52a0\u8f7d\u7684\u8d44\u6e90\uff0c\u5bf9\u6bd4\u53ef\u4ee5\u95f4\u63a5\u7684\u6355\u83b7\u9519\u8bef\n3. `window.addEventListener('error', fn, true)`, \u4f1a\u6355\u83b7\u4f46\u662f\u4e0d\u5192\u6ce1\uff0c\u6240\u4ee5window.onerror \u4e0d\u4f1a\u89e6\u53d1\uff0c\u6355\u83b7\u9636\u6bb5\u53ef\u4ee5\u89e6\u53d1\n\n### \u4e09\u3001\u9519\u8bef\u4e0a\u62a5\n\n\u4e00\u822c\u4f7f\u7528image\u6765\u4e0a\u62a5\uff0c\u5927\u5382\u90fd\u662f\u91c7\u7528\u5229\u7528image\u5bf9\u8c61\u7684\u65b9\u5f0f\u4e0a\u62a5\u9519\u8bef\u7684\uff1b\n\n\u4f7f\u7528\u56fe\u7247\u53d1\u9001get\u8bf7\u6c42\uff0c\u4e0a\u62a5\u4fe1\u606f\uff0c\u7531\u4e8e\u6d4f\u89c8\u5668\u5bf9\u56fe\u7247\u6709\u7f13\u5b58\uff0c\u540c\u6837\u7684\u8bf7\u6c42\uff0c\u56fe\u7247\u53ea\u4f1a\u53d1\u9001\u4e00\u6b21\uff0c\u907f\u514d\u91cd\u590d\u4e0a\u62a5\u3002\n\n### \u56db\u3001\u501f\u52a9\u7b2c\u4e09\u65b9\u5e93\n\n- sentry-javascript\n\n\n", "answer": "", "biz_type": 1, "qid": 701, "subject": "", "title": "\u5982\u4f55\u8bb0\u5f55\u524d\u7aef\u518d\u7528\u6237\u6d4f\u89c8\u5668\u4e0a\u53d1\u751f\u7684\u9519\u8bef\u5e76\u6c47\u62a5\u7ed9\u670d\u52a1\u5668\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["CVTE"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 8, "qid": 689, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001\u4ec0\u4e48\u662f\u5355\u70b9\u767b\u5f55\n\n\u5355\u70b9\u767b\u5f55SSO(Single Sign On),\u662f\u4e00\u4e2a\u591a\u7cfb\u7edf\u5171\u5b58\u7684\u73af\u5883\u4e0b\uff0c\u7528\u6237\u5728\u4e00\u5904\u767b\u5f55\u540e\uff0c\u5c31\u4e0d\u7528\u5728\u5176\u4ed6\u7cfb\u7edf\u4e2d\u767b\u5f55\uff0c\u4e5f\u5c31\u662f\u7528\u6237\u7684\u4e00\u6b21\u767b\u5f55\u5f97\u5230\u5176\u4ed6\u6240\u6709\u7cfb\u7edf\u7684\u4fe1\u4efb\n\n\u6bd4\u5982\u73b0\u6709\u4e1a\u52a1\u7cfb\u7edfA\u3001B\u3001C\u4ee5\u53caSSO\u7cfb\u7edf\uff0c\u7b2c\u4e00\u6b21\u8bbf\u95eeA\u7cfb\u7edf\u65f6\uff0c\u53d1\u73b0\u6ca1\u6709\u767b\u5f55\uff0c\u5f15\u5bfc\u7528\u6237\u5230SSO\u7cfb\u7edf\u767b\u5f55\uff0c\u6839\u636e\u7528\u6237\u7684\u767b\u5f55\u4fe1\u606f\uff0c\u751f\u6210\u552f\u4e00\u7684\u4e00\u4e2a\u51ed\u636etoken\uff0c\u8fd4\u56de\u7ed9\u7528\u6237\u3002\u540e\u671f\u7528\u6237\u8bbf\u95eeB\u3001C\u7cfb\u7edf\u7684\u65f6\u5019\uff0c\u643a\u5e26\u4e0a\u5bf9\u5e94\u7684\u51ed\u8bc1\u5230SSO\u7cfb\u7edf\u53bb\u6821\u9a8c\uff0c\u6821\u9a8c\u901a\u8fc7\u540e\uff0c\u5c31\u53ef\u4ee5\u5355\u70b9\u767b\u5f55\uff1b\n\n\u5355\u70b9\u767b\u5f55\u5728\u5927\u578b\u7f51\u7ad9\u4e2d\u4f7f\u7528\u7684\u975e\u5e38\u9891\u7e41\uff0c\u4f8b\u5982\uff0c\u963f\u91cc\u65d7\u4e0b\u6709\u6dd8\u5b9d\u3001\u5929\u732b\u3001\u652f\u4ed8\u5b9d\u7b49\u7f51\u7ad9\uff0c\u5176\u80cc\u540e\u7684\u6210\u767e\u4e0a\u5343\u7684\u5b50\u7cfb\u7edf\uff0c\u7528\u6237\u64cd\u4f5c\u4e00\u6b21\u6216\u8005\u4ea4\u6613\u53ef\u80fd\u6d89\u53ca\u5230\u5f88\u591a\u5b50\u7cfb\u7edf\uff0c\u6bcf\u4e2a\u5b50\u7cfb\u7edf\u90fd\u9700\u8981\u9a8c\u8bc1\uff0c\u6240\u4ee5\u63d0\u51fa\uff0c\u7528\u6237\u767b\u5f55\u4e00\u6b21\u5c31\u53ef\u4ee5\u8bbf\u95ee\u76f8\u4e92\u4fe1\u4efb\u7684\u5e94\u7528\u7cfb\u7edf\n\n\u5355\u70b9\u767b\u5f55\u6709\u4e00\u4e2a\u72ec\u7acb\u7684\u8ba4\u8bc1\u4e2d\u5fc3\uff0c\u53ea\u6709\u8ba4\u8bc1\u4e2d\u5fc3\u624d\u80fd\u63a5\u53d7\u7528\u6237\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u7b49\u4fe1\u606f\u8fdb\u884c\u8ba4\u8bc1\uff0c\u5176\u4ed6\u7cfb\u7edf\u4e0d\u63d0\u4f9b\u767b\u5f55\u5165\u53e3\uff0c\u53ea\u63a5\u53d7\u8ba4\u8bc1\u4e2d\u5fc3\u7684\u95f4\u63a5\u6388\u6743\u3002\u95f4\u63a5\u6388\u6743\u901a\u8fc7\u4ee4\u724c\u5b9e\u73b0\uff0c\u5f53\u7528\u6237\u63d0\u4f9b\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u901a\u8fc7\u8ba4\u8bc1\u4e2d\u5fc3\u8ba4\u8bc1\u540e\uff0c\u8ba4\u8bc1\u4e2d\u5fc3\u4f1a\u521b\u5efa\u6388\u6743\u4ee4\u724c\uff0c\u5728\u63a5\u4e0b\u6765\u7684\u8df3\u8f6c\u8fc7\u7a0b\u4e2d\uff0c\u6388\u6743\u4ee4\u724c\u4f5c\u4e3a\u53c2\u6570\u53d1\u9001\u7ed9\u5404\u4e2a\u5b50\u7cfb\u7edf\uff0c\u5b50\u7cfb\u7edf\u62ff\u5230\u4ee4\u724c\u5373\u5f97\u5230\u4e86\u6388\u6743\uff0c\u7136\u540e\u521b\u5efa\u5c40\u90e8\u4f1a\u8bdd\u3002\n\n### \u4e8c\u3001\u5355\u70b9\u767b\u5f55\u539f\u7406\n\n\u5355\u70b9\u767b\u5f55\u6709\u540c\u57df\u548c\u8de8\u57df\u4e24\u79cd\u573a\u666f\n\n#### 1\uff09\u540c\u57df\n\n\u9002\u7528\u573a\u666f\uff1a\u90fd\u662f\u4f01\u4e1a\u81ea\u5df1\u7684\u7cfb\u7edf\uff0c\u6240\u6709\u7cfb\u7edf\u90fd\u4f7f\u7528\u540c\u4e00\u4e2a\u4e00\u7ea7\u57df\u540d\u901a\u8fc7\u4e0d\u540c\u7684\u4e8c\u7ea7\u57df\u540d\u6765\u533a\u5206\u3002\n\n\u4e3e\u4e2a\u4f8b\u5b50\uff1a\u516c\u53f8\u6709\u4e00\u4e2a\u4e00\u7ea7\u57df\u540d\u4e3a zlt.com \uff0c\u6211\u4eec\u6709\u4e09\u4e2a\u7cfb\u7edf\u5206\u522b\u662f\uff1a\u95e8\u6237\u7cfb\u7edf(sso.zlt.com)\u3001\u5e94\u75281(app1.zlt.com)\u548c\u5e94\u75282(app2.zlt.com)\uff0c\u9700\u8981\u5b9e\u73b0\u7cfb\u7edf\u4e4b\u95f4\u7684\u5355\u70b9\u767b\u5f55\uff0c\u5b9e\u73b0\u67b6\u6784\u5982\u4e0b\n\n\u6838\u5fc3\u539f\u7406\uff1a\n\n1. \u95e8\u6237\u7cfb\u7edf\u8bbe\u7f6e\u7684cookie\u7684domain\u4e3a\u4e00\u7ea7\u57df\u540d\u4e5f\u662fzlt.com\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5171\u4eab\u95e8\u6237\u7684cookie\u7ed9\u6240\u6709\u7684\u4f7f\u7528\u8be5\u57df\u540dxxx.alt.com\u7684\u7cfb\u7edf\n2. \u4f7f\u7528Spring Session\u7b49\u6280\u672f\u8ba9\u6240\u6709\u7cfb\u7edf\u5171\u4eabSession\n3. \u8fd9\u6837\u53ea\u8981\u95e8\u6237\u7cfb\u7edf\u767b\u5f55\u4e4b\u540e\u65e0\u8bba\u8df3\u8f6c\u5e94\u75281\u6216\u8005\u5e94\u75282\uff0c\u90fd\u80fd\u901a\u8fc7\u95e8\u6237Cookie\u4e2d\u7684sessionId\u8bfb\u53d6\u5230Session\u4e2d\u7684\u767b\u5f55\u4fe1\u606f\u5b9e\u73b0\u5355\u70b9\u767b\u5f55\n\n#### 2\uff09\u8de8\u57df\n\n\u5355\u70b9\u767b\u5f55\u4e4b\u95f4\u7684\u7cfb\u7edf\u57df\u540d\u4e0d\u4e00\u6837\uff0c\u4f8b\u5982\u7b2c\u4e09\u65b9\u7cfb\u7edf\u3002\u7531\u4e8e\u57df\u540d\u4e0d\u4e00\u6837\u4e0d\u80fd\u5171\u4eabCookie\u4e86\uff0c\u9700\u8981\u7684\u4e00\u4e2a\u72ec\u7acb\u7684\u6388\u6743\u7cfb\u7edf\uff0c\u5373\u4e00\u4e2a\u72ec\u7acb\u7684\u8ba4\u8bc1\u4e2d\u5fc3(passport),\u5b50\u7cfb\u7edf\u7684\u767b\u5f55\u5747\u53ef\u4ee5\u901a\u8fc7passport\uff0c\u5b50\u7cfb\u7edf\u672c\u8eab\u5c06\u4e0d\u53c2\u4e0e\u767b\u5f55\u64cd\u4f5c\uff0c\u5f53\u4e00\u4e2a\u7cfb\u7edf\u767b\u5f55\u6210\u529f\u540e\uff0cpassprot\u5c06\u4f1a\u9881\u53d1\u4e00\u4e2a\u4ee4\u724c\u7ed9\u5b50\u7cfb\u7edf\uff0c\u5b50\u7cfb\u7edf\u53ef\u4ee5\u62ff\u7740\u4ee4\u724c\u53bb\u83b7\u53d6\u683c\u5b50\u7684\u4fdd\u62a4\u8d44\u6e90\uff0c\u4e3a\u4e86\u51cf\u5c11\u9891\u7e41\u8ba4\u8bc1\uff0c\u5404\u4e2a\u5b50\u7cfb\u7edf\u5728\u88abpassport\u6388\u6743\u4ee5\u540e\uff0c\u4f1a\u5efa\u7acb\u4e00\u4e2a\u5c40\u90e8\u4f1a\u8bdd\uff0c\u5728\u4e00\u5b9a\u65f6\u95f4\u5185\u65e0\u9700\u518d\u6b21\u5411passport\u53d1\u8d77\u8ba4\u8bc1\n\n**\u57fa\u672c\u539f\u7406**\n\n1. \u7528\u6237\u7b2c\u4e00\u6b21\u8bbf\u95ee\u5e94\u7528\u7cfb\u7edf\u7684\u65f6\u5019\uff0c\u56e0\u4e3a\u6ca1\u6709\u767b\u5f55\uff0c\u4f1a\u88ab\u5f15\u5bfc\u5230**\u8ba4\u8bc1\u7cfb\u7edf**\u4e2d\u8fdb\u884c\u767b\u5f55\uff1b\n2. \u6839\u636e\u7528\u6237\u63d0\u4f9b\u7684\u767b\u5f55\u4fe1\u606f\uff0c\u8ba4\u8bc1\u7cfb\u7edf\u8fdb\u884c\u8eab\u4efd\u6821\u9a8c\uff0c\u5982\u679c\u901a\u8fc7\uff0c\u8fd4\u56de\u7ed9\u7528\u6237\u4e00\u4e2a\u8ba4\u8bc1\u51ed\u636e-**\u4ee4\u724c**\uff1b\n3. \u7528\u6237\u518d\u6b21\u8bbf\u95ee\u522b\u7684\u5e94\u7528\u7684\u65f6\u5019\uff0c**\u5e26\u4e0a\u4ee4\u724c\u4f5c\u4e3a\u8ba4\u8bc1\u51ed\u8bc1**\uff1b\n4. \u5e94\u7528\u7cfb\u7edf\u63a5\u6536\u5230\u8bf7\u6c42\u540e\u4f1a\u628a\u4ee4\u724c\u9001\u5230\u8ba4\u8bc1\u670d\u52a1\u5668\u8fdb\u884c**\u6821\u9a8c**\uff0c\u5982\u679c\u901a\u8fc7\uff0c\u7528\u6237\u5c31\u53ef\u4ee5\u5728\u4e0d\u7528\u767b\u5f55\u7684\u60c5\u51b5\u4e0b\u8bbf\u95ee\u5176\u4ed6\u4fe1\u4efb\u7684\u4e1a\u52a1\u670d\u52a1\u5668\u3002\n\n**\u767b\u5f55\u6d41\u7a0b**\n\n![\u767b\u5f55\u6d41\u7a0b](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-689-sso.png)\n\n1. \u7528\u6237\u8bbf\u95ee\u7cfb\u7edf1\u7684\u53d7\u4fdd\u62a4\u8d44\u6e90\uff0c\u7cfb\u7edf1\u53d1\u73b0\u7528\u6237\u6ca1\u6709\u767b\u5f55\uff0c\u8df3\u8f6c\u5230sso\u8ba4\u8bc1\u4e2d\u5fc3\uff0c\u5e76\u5c06\u81ea\u5df1\u7684\u5730\u5740\u4f5c\u4e3a\u53c2\u6570\n2. sso\u8ba4\u8bc1\u4e2d\u5fc3\u53d1\u73b0\u7528\u6237\u672a\u767b\u5f55\uff0c\u5c06\u7528\u6237\u5f15\u5bfc\u5230\u767b\u5f55\u9875\u9762\n3. \u7528\u6237\u63d0\u4ea4\u7528\u6237\u540d\u3001\u5bc6\u7801\u8fdb\u884c\u767b\u5f55\n4. sso\u8ba4\u8bc1\u4e2d\u5fc3\u6821\u9a8c\u7528\u6237\u4fe1\u606f\uff0c\u521b\u5efa\u7528\u6237\u4e0esso\u8ba4\u8bc1\u4e2d\u5fc3\u4e4b\u95f4\u7684\u4f1a\u8bdd\uff0c\u79f0\u4e4b\u4e3a\u5168\u5c40\u4f1a\u8bdd\uff0c\u540c\u65f6\u521b\u5efa\u6388\u6743\u4ee4\u724c\n5. sso \u5e26\u7740\u4ee4\u724c\u8df3\u8f6c\u56de\u6700\u521d\u7684\u8bf7\u6c42\u7684\u5730\u5740(\u7cfb\u7edf1)\n6. \u7cfb\u7edf1\u62ff\u7740\u4ee4\u724c\uff0c\u53bbsso\u8ba4\u8bc1\u4e2d\u5fc3\u6821\u9a8c\u4ee4\u724c\u662f\u5426\u6709\u6548\n7. sso\u8ba4\u8bc1\u4e2d\u5fc3\u6821\u9a8c\u4ee4\u724c\uff0c\u8fd4\u56de\u6709\u6548\uff0c\u6ce8\u518c\u7cfb\u7edf1(\u4e5f\u5c31\u662f\u8fd4\u56de\u4e00\u4e2acookie)\n8. \u7cfb\u7edf\u4e00\u4f7f\u7528\u8be5\u4ee4\u724c\u521b\u5efa\u4e0e\u7528\u6237\u7684\u4f1a\u8bdd\uff0c\u6210\u4e3a\u5c40\u90e8\u4f1a\u8bdd\uff0c\u8fd4\u56de\u53d7\u4fdd\u62a4\u7684\u8d44\u6e90\n9. \u7528\u6237\u8bbf\u95ee\u7cfb\u7edf2\u53d7\u4fdd\u62a4\u7684\u8d44\u6e90\n10. \u7cfb\u7edf2\u53d1\u73b0\u7528\u6237\u672a\u767b\u5f55\uff0c\u8df3\u8f6c\u81f3sso\u8ba4\u8bc1\u4e2d\u5fc3\uff0c\u5e76\u5c06\u81ea\u5df1\u7684\u5730\u5740\u4f5c\u4e3a\u53c2\u6570\n11. sso\u8ba4\u8bc1\u4e2d\u5fc3\u53d1\u73b0\u7528\u6237\u5df2\u767b\u5f55\uff0c\u8df3\u8f6c\u56de\u7cfb\u7edf2\u7684\u5730\u5740\uff0c\u5e76\u4e14\u9644\u4e0a\u4ee4\u724c\n12. \u7cfb\u7edf2\u62ff\u5230\u4ee4\u724c\uff0c\u53bbsso\u4e2d\u5fc3\u9a8c\u8bc1\u4ee4\u724c\u662f\u5426\u6709\u6548\uff0c\u8fd4\u56de\u6709\u6548\uff0c\u6ce8\u518c\u7cfb\u7edf2\n13. \u7cfb\u7edf2\u4f7f\u7528\u8be5\u4ee4\u724c\u521b\u5efa\u4e0e\u7528\u6237\u7684\u5c40\u90e8\u4f1a\u8bdd\uff0c\u8fd4\u56de\u53d7\u4fdd\u62a4\u8d44\u6e90\n14. \u7528\u6237\u767b\u5f55\u6210\u529f\u4e4b\u540e\uff0c\u4f1a\u4e0esso\u8ba4\u8bc1\u4e2d\u5fc3\u4ee5\u53ca\u5404\u4e2a\u5b50\u7cfb\u7edf\u5efa\u7acb\u4f1a\u8bdd\uff0c\u7528\u6237\u4e0esso\u8ba4\u8bc1\u4e2d\u5fc3\u5efa\u7acb\u7684\u4f1a\u8bdd\u79f0\u4e4b\u4e3a\u5168\u5c40\u4f1a\u8bdd\uff0c\u7528\u6237\u4e0e\u5404\u4e2a\u5b50\u7cfb\u7edf\u5efa\u7acb\u7684\u4f1a\u8bdd\u79f0\u4e4b\u4e3a\u5c40\u90e8\u4f1a\u8bdd\uff0c\u5c40\u90e8\u4f1a\u8bdd\u5efa\u7acb\u4e4b\u540e\uff0c\u7528\u6237\u8bbf\u95ee\u5b50\u7cfb\u7edf\u53d7\u4fdd\u62a4\u8d44\u6e90\u5c06\u4e0d\u518d\u901a\u8fc7sso\u8ba4\u8bc1\u4e2d\u5fc3\n\n**\u6ce8\u9500\u6d41\u7a0b**\n\n![\u6ce8\u9500\u6d41\u7a0b](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-689-out.png)\n\n1. \u7528\u6237\u5411\u7cfb\u7edf\u63d0\u4ea4\u6ce8\u9500\u64cd\u4f5c\n2. \u7cfb\u7edf\u6839\u636e\u7528\u6237\u4e0e\u7cfb\u7edf1\u5efa\u7acb\u7684\u4f1a\u8bdd\uff0c\u62ff\u5230\u4ee4\u724c\uff0c\u5411sso\u8ba4\u8bc1\u4e2d\u5fc3\u63d0\u4ea4\u6ce8\u9500\u64cd\u4f5c\n3. sso\u8ba4\u8bc1\u4e2d\u5fc3\u6821\u9a8c\u4ee4\u724c\u6709\u6548\uff0c\u9500\u6bc1\u5168\u5c40\u4f1a\u8bdd\uff0c\u540c\u65f6\u53d6\u51fa\u6240\u6709\u7528\u6b64\u4ee4\u724c\u6ce8\u518c\u7684\u7cfb\u7edf\u5730\u5740\n4. sso\u8ba4\u8bc1\u4e2d\u5fc3\u5411\u6240\u6709\u6ce8\u518c\u7cfb\u7edf\u53d1\u8d77\u6ce8\u9500\u8bf7\u6c42\uff0c\u5404\u6ce8\u518c\u7cfb\u7edf\u9500\u6bc1\u5c40\u90e8\u4f1a\u8bdd\n5. sso\u8ba4\u8bc1\u4e2d\u5fc3\u5f15\u5bfc\u7528\u6237\u5230\u767b\u5f55\u9875\u9762", "answer": "", "biz_type": 1, "qid": 689, "subject": "", "title": "\u5355\u70b9\u767b\u5f55\u5b9e\u73b0\u539f\u7406"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7384\u6b66\u79d1\u6280"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 8, "qid": 710, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001\u4e3a\u4ec0\u4e48\u9700\u8981\u505a\u4f18\u5316\n\n\u9875\u9762\u6027\u80fd\u7684\u95ee\u9898\u5bf9\u7528\u6237\u4f53\u9a8c\u7684\u5f71\u54cd\u975e\u5e38\u5927\uff0c\u52a0\u8f7d\u65f6\u95f4\u6bcf\u591a 1 \u79d2\uff0c\u4f60\u5c31\u4f1a\u6d41\u5931 7%\u7684\u7528\u6237\uff0c\u5728\u6b63\u5e38\u9875\u9762\u7684\u60c5\u51b5\u4e0b\uff0c\u82e5\u9875\u9762\u52a0\u8f7d\u65f6\u95f4\u8d85\u8fc7 8 \u79d2\u65f6\u95f4\uff0c\u90a3\u4e48\u4f60\u53ef\u80fd\u4f1a\u635f\u5931 85%\u4ee5\u4e0a\u7684\u7528\u6237\u3002\n\n\u4e3a\u4e86\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u66f4\u597d\u7684\u4e1a\u52a1\u652f\u6301\u66f4\u597d\u7684\u4ee3\u7801\u8d28\u91cf\u6211\u4eec\u9700\u8981\u5bf9\u9875\u9762\u8fdb\u884c\u4f18\u5316\n\n### \u4e8c\u3001\u5b9e\u73b0\u65b9\u6848\n\n\u9875\u9762\u8fdb\u884c\u4f18\u5316\uff0c\u5176\u5b9e\u6709\u4e24\u79cd\u4f18\u5316\u65b9\u5f0f\n\n#### 1.\u8f6f\u4ef6\u4f18\u5316\n\n- **html** \u4f18\u5316 \u5e03\u5c40\u4f18\u5316\uff0c\u51cf\u5c11\u7a7a\u683c\uff0c\u4e0d\u7528 table \u5e03\u5c40\uff0c\u5c3d\u91cf\u4e0d\u7528 iframe \u6807\u7b7e\n- **css \u4f18\u5316** \u53ef\u4ee5\u7528 css \u5b9e\u73b0\u7684\u5c3d\u91cf\u4e0d\u7528 js \u5b9e\u73b0\uff1bcss \u4ee3\u7801\u538b\u7f29\uff1bcss \u5408\u5e76\uff1b\u7528\u5b57\u4f53\u56fe\u6807\u4ee3\u66ff\u56fe\u7247\uff1b \u5f00\u542f css \u786c\u4ef6\u52a0\u901f\uff1atransform \u52a8\u753b\u7531 GPU \u63a7\u5236\uff0c\u652f\u6301\u786c\u4ef6\u52a0\u901f\uff0c\u5e76\u4e0d\u9700\u8981\u8f6f\u4ef6\u65b9\u9762\u7684\u6e32\u67d3\uff0c\u4f7f\u7528 top \u548c left \u5b9e\u73b0\u52a8\u753b\u65f6\u6d4f\u89c8\u5668\u53d1\u751f\u7684 repaint\n- **js \u4f18\u5316** \u56fe\u7247\u4f18\u5316(\u9884\u52a0\u8f7d/\u61d2\u52a0\u8f7d/\u5ef6\u65f6\u52a0\u8f7d)\uff1b\u89c6\u9891\u6216\u97f3\u9891\u4e0d\u52a0\u8f7d\uff0c\u5f53\u70b9\u51fb\u4e4b\u540e\u5f00\u59cb\u5355\u72ec\u52a0\u8f7d\u89c6\u9891\u6216\u97f3\u9891\uff1b \u5728 js \u4e2d\u5c3d\u91cf\u51cf\u5c11\u95ed\u5305\u7684\u4f7f\u7528\uff08\u539f\u56e0\uff1a\u95ed\u5305\u4f1a\u4ea7\u751f\u4e0d\u91ca\u653e\u7684\u6808\u5185\u5b58\uff09 DOM \u7684\u64cd\u4f5c \u5176\u5b9e css \u538b\u7f29\u4e0e js \u7684\u538b\u7f29\u548c\u6df7\u4e71\u6bd4 html \u538b\u7f29\u6536\u76ca\u8981\u5927\u5f97\u591a\uff0c\u540c\u65f6 css \u4ee3\u7801\u548c js \u4ee3\u7801\u6bd4 html \u4ee3\u7801\u591a\u5f97\u591a\uff0c\u901a\u8fc7 css \u538b\u7f29\u548c js \u538b\u7f29\u5e26\u6765\u6d41\u91cf\u7684\u51cf\u5c11\uff0c\u4f1a\u975e\u5e38\u660e\u663e\u3002\n- **http \u4f18\u5316** \u5c3d\u91cf\u51cf\u5c11\u9700\u8981\u53d1\u9001\u7684 http \u8bf7\u6c42\uff1b\n- **\u7f13\u5b58\u65b9\u9762** \u4f7f\u7528\u6d4f\u89c8\u5668\u7684\u7f13\u5b58\u673a\u5236\uff0c\u4e0d\u9700\u8981\u6bcf\u6b21\u767b\u5f55\u6216\u8005\u600e\u4e48\u6837\u90fd\u9700\u8981\u518d\u53bb\u8bbf\u95ee\u670d\u52a1\u5668\uff1b\u5229\u7528\u6d4f\u89c8\u5668\u548c\u670d\u52a1\u5668\u7aef\u7684\u7f13\u5b58\u6280\u672f\uff08304 \u7f13\u5b58\uff09\uff0c\u628a\u4e00\u4e9b\u4e0d\u7ecf\u5e38\u66f4\u65b0\u7684\u9759\u6001\u8d44\u6e90\u6587\u4ef6\u505a\u7f13\u5b58\u5904\u7406\n\n#### 2.\u786c\u4ef6\u4f18\u5316\n\n- \u8bbe\u7f6e\u8d1f\u8f7d\u5747\u8861\u670d\u52a1\u5668 \u901a\u8fc7\u8d1f\u8f7d\u5747\u8861\u670d\u52a1\u5668\u4f7f\u5f97\u540e\u53f0\u6570\u636e\u538b\u529b\u5e73\u8861\uff1b\n- \u589e\u52a0\u5e26\u5bbd \u4f46\u662f\u786c\u4ef6\u7684\u6210\u672c\u8fdc\u8fdc\u9ad8\u4e8e\u8f6f\u4ef6\u4f18\u5316\u3002\n\n\n\n\n", "answer": "", "biz_type": 1, "qid": 710, "subject": "", "title": "\u8bf7\u63cf\u8ff0\u4e0b\u4e3a\u4ec0\u4e48\u9875\u9762\u9700\u8981\u505a\u4f18\u5316\uff1f\u5e76\u5199\u51fa\u5e38\u7528\u7684\u9875\u9762\u4f18\u5316\u5b9e\u73b0\u65b9\u6848\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7384\u6b66\u79d1\u6280"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 6, "qid": 714, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001Scope\n\n#### 1.Lexical scope \u8bcd\u6cd5\u4f5c\u7528\u57df\n\n\u4f5c\u7528\u57df\u662f\u4ec0\u4e48\u5462\uff1f\u5b83\u6307\u7684\u662f\u4f60\u7684\u53d8\u91cf\u548c\u51fd\u6570\u8fd0\u884c\u5230\u67d0\u4e2a\u5730\u65b9\u7684\u4ee3\u7801\u5904\u80fd\u5426\u88ab\u8bbf\u95ee\u5230\u3002\n\n\u4e3a\u4ec0\u4e48\u9700\u8981\u4f5c\u7528\u57df\u5462\uff1f\u4e3a\u4ec0\u4e48\u8981\u9650\u5236\u53d8\u91cf\u7684\u8bbf\u95ee\u6027\u800c\u975e\u5168\u90e8\u66b4\u9732\u5230\u516c\u5171\u57df\u4e0b\u5462\uff1f\n\n\u8fd9\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u6700\u57fa\u672c\u7684\u6982\u5ff5\u548c\u7406\u5ff5\uff1a\u9694\u79bb\u6027\uff08The Principle of Least Access\uff09\u3002\u4e3a\u4e86\u804c\u8d23\u660e\u786e\uff0c\u4f60\u53ea\u80fd\u521a\u597d\u8bbf\u95ee\u5230\u4f60\u9700\u8981\u7684\u6240\u6709\u4e1c\u897f\uff0c\u4e0d\u591a\u4e5f\u4e0d\u5c11\u3002\u9644\u5e26\u5730\uff0c\u5b83\u5e26\u6765\u4e86\u6a21\u5757\u5316\u3001\u547d\u540d\u7a7a\u95f4\u7b49\u597d\u5904\uff0c\u8ba9\u4f60\u5199\u51fa\u66f4\u6613\u9605\u8bfb\u3001\u66f4\u6613\u7ef4\u62a4\u7684\u4ee3\u7801\u3002\u53ef\u4ee5\u8bf4\uff0c\u4f5c\u7528\u57df\u662f\u8bb8\u591a\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\u90fd\u4ece\u8bed\u8a00\u5c42\u9762\u652f\u6301\u7684\u4e00\u4e2a\u7279\u6027\u3002\n\n\u800c\u8bcd\u6cd5\u4f5c\u7528\u57df\uff0c\u6307\u7684\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u540d\u5f15\u7528\u4e4b\uff0c\u800c\u4e0d\u53d1\u751f\u5f15\u7528\u9519\u8bef\u3002\n\n\u5b83\u672c\u8d28\u4e0a\u662f \u9759\u6001\u4f5c\u7528\u57df static scopes \u3002\n\n#### 2.\u6709\u4ec0\u4e48\u7c7b\u578b\u7684\u4f5c\u7528\u57df\n\n**1\uff09\u5168\u5c40\u4f5c\u7528\u57df**\n\n\u4e0d\u5b9a\u4e49\u5728\u4efb\u4f55\u51fd\u6570\u4ee5\u5185\u7684\u53d8\u91cf\u6216\u51fd\u6570\u90fd\u4f4d\u4e8e\u5168\u5c40\u4f5c\u7528\u57df\u4e0b\u3002\u5f53\u7136\uff0c\u8fd9\u4e2a\u5168\u5c40\u4f5c\u7528\u57df\u5176\u5b9e\u4e5f\u662f\u6709\u8fb9\u754c/\u4e0a\u4e0b\u6587\u7684\uff0c\u6bd4\u5982 NodeJS \u4e2d\u4e0d\u540c\u6587\u4ef6\u4e4b\u95f4\u7684\u5168\u5c40\u53d8\u91cf\u4e0d\u80fd\u4e92\u76f8\u8bbf\u95ee\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u5168\u5c40\u5bf9\u8c61 global \u7684\u4e0a\u4e0b\u6587\u4ec5\u9650\u4e8e\u4e00\u4e2a\u6587\u4ef6\uff1b\u6bd4\u5982\u6d4f\u89c8\u5668\u4e2d \u4e0d\u540c tab \u4e4b\u95f4\u7684\u5168\u5c40\u53d8\u91cf\u4e5f\u662f\u4e0d\u80fd\u4e92\u76f8\u8bbf\u95ee\u7684\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u6807\u7b7e\u7684\u5168\u5c40\u5bf9\u8c61 window \u7684\u4e0a\u4e0b\u6587\u4e5f\u4ec5\u9650\u4e8e\u4e00\u4e2a tab \u4e2d\n\n**2\uff09\u51fd\u6570\u4f5c\u7528\u57df**\n\n\u4efb\u4f55\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u7684\u53d8\u91cf\u6216\u51fd\u6570\u90fd\u5904\u4e8e\u51fd\u6570\u4f5c\u7528\u57df\u4e0b\uff0c\u8fd9\u4e9b\u53d8\u91cf\u65e0\u6cd5\u5728\u51fd\u6570\u4ee5\u5916\u88ab\u5f15\u7528\u5230\n\n**3\uff09\u5757\u4f5c\u7528\u57df**\n\nES6\u4e4b\u524d\u662f\u6ca1\u6709\u8fd9\u4e2a\u4e1c\u897f\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b9a\u4e49\u5728 { } \u5927\u62ec\u53f7\u5bf9 \u53ca for (i = 0; i < 10; i++) { ... } \u5faa\u73af\u7ed3\u6784\u4e2d\u7684\u53d8\u91cf\u7edf\u7edf\u90fd\u4f1a\u8dd1\u5230\u5168\u5c40\u4f5c\u7528\u57df\u4e0b\u53bb\u3002\u8fd9\u5c31\u5f88\u8fdd\u53cd\u76f4\u89c9\u4e86\u3002ES6 \u901a\u8fc7 let \u548c const \u5173\u952e\u5b57\u4fee\u590d\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u5e76\u4e14\u8d4b\u4e88\u4e86\u4e00\u822c\u7684\u5757 block \u4ee5\u5757\u4f5c\u7528\u57df\n\n------\n\n\u5728\u51fd\u6570\u6216\u5757\u4f5c\u7528\u57df\u4e2d\u58f0\u660e\u7684\u53d8\u91cf\u6216\u51fd\u6570\u82e5\u53d1\u751f\u5d4c\u5957\uff0c\u5219\u53c8\u6709\u4e86\u5d4c\u5957\u4e0b\u7684\u8bcd\u6cd5\u4f5c\u7528\u57df\u89c4\u5219\u3002\u76f8\u5bf9\u5730\uff0c\u4f4d\u4e8e\u4e00\u4e2a\u51fd\u6570/\u5757\u5185\u90e8\u7684\u53d8\u91cf\u6216\u51fd\u6570\u79f0\u4f4d\u4e8e\u5185\u5c42\u4f5c\u7528\u57df inner scope\uff1b\u524d\u8005\u76f8\u5bf9\u5730\u79f0\u5176\u4f4d\u4e8e\u5916\u5c42\u7684\u4f5c\u7528\u57df outer scope\u3002\n\n\u5185\u5916\u4e4b\u95f4\u7684\u53d8\u91cf\u8bbf\u95ee\u6027\u89c4\u5219\u4e3a\uff1a\n\n**3\uff09\u5916\u5c42\u4f5c\u7528\u57df**\n\n\u5168\u5c40\u4f5c\u7528\u57df\u662f\u6700\u5916\u5c42\u7684\u4f5c\u7528\u57df\u3002\n\n\u5916\u5c42\u4f5c\u7528\u57df\u4e0b\u7684\u53d8\u91cf\u53ca\u51fd\u6570\u53ef\u4ee5\u5728\u5185\u5c42\u4f5c\u7528\u57df\u4e2d\u88ab\u8bbf\u95ee\u83b7\u53d6\n\n**4\uff09\u5185\u5c42\u4f5c\u7528\u57df**\n\n\u53ef\u4ee5\u8bbf\u95ee\u5230\u5916\u5c42\u4f5c\u7528\u57df\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u8bbf\u95ee\u81ea\u8eab\u7684\u53d8\u91cf\uff0c\u4f46\u4e0d\u80fd\u88ab\u5916\u5c42\u4f5c\u7528\u57df\u5f15\u7528\n\n#### 3.\u4f5c\u7528\u57df\u4ec0\u4e48\u65f6\u5019\u751f\u6210\n\n**1\uff09\u5168\u5c40\u4f5c\u7528\u57df**\n\n\u6ca1\u58f0\u660e\u5c31\u4f7f\u7528\u7684\u4ee3\u7801\uff0c\u9ed8\u8ba4\u5168\u8dd1\u5230\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528 use strict \u6a21\u5f0f\u7981\u6b62\n\n**2\uff09\u51fd\u6570\u4f5c\u7528\u57df**\n\n\u51fd\u6570\u58f0\u660e\u5f00\u59cb\u65f6 function() { ... }\uff0c\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u8bcd\u6cd5\u4f5c\u7528\u57df\n\n**3\uff09\u5757\u4f5c\u7528\u57df**\n\nES6 \u524d\uff0c{ } \u5927\u62ec\u53f7\u5bf9 \u53ca for (i = 0; i < 10; i++) { ... } \u5faa\u73af\u7ed3\u6784 \u5747\u4e0d\u751f\u6210\u8bcd\u6cd5\u4f5c\u7528\u57df\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5757\u91cc\u9762\u58f0\u660e\u7684\u53d8\u91cf\u5168\u90e8\u90fd\u4f1a\u8dd1\u5230\u5f53\u524d\u7684\u5168\u5c40\u4f5c\u7528\u57df\u91cc\u53bb\u3002\u8fd9\u662f\u524d JS \u65f6\u4ee3\u7684\u4e00\u4e2a\u5751\u3002ES6 \u4ee5\u540e let \u548c const \u5173\u952e\u5b57\u90fd\u4fee\u590d\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u5b83\u4eec\u4f1a\u751f\u6210\u4e00\u4e2a\u5757\u4f5c\u7528\u57df\uff0c\u6216\u53ef\u79f0\u4e3a condition/loop lexical scope\n\n\u56e0\u6b64 ES6 \u65f6\u4ee3\u540e\uff0c\u53ef\u4ee5\u8fd9\u6837\u7406\u89e3\uff1a\u9664\u4e86\u51fd\u6570\u4f5c\u7528\u57df\u548c\u5757\u4f5c\u7528\u57df\uff0c\u5176\u4ed6\u7684\u5168\u662f\u5168\u5c40\u4f5c\u7528\u57df\u3002\n\n\u4f5c\u7528\u57df\u5bf9\u4e8e\u7f16\u7a0b\u6a21\u578b\u7684\u91cd\u8981\u610f\u4e49\u4e4b\u4e00\uff0c\u5373\u662f\u5176\u4f53\u73b0\u7684\u6a21\u5757\u6982\u5ff5\u3002\u4f60\u53ea\u9700\u8981\u5173\u6ce8\u6a21\u5757\u5185\u4e0e\u81ea\u5df1\u76f8\u5173\u7684\u53d8\u91cf\u548c\u4ee3\u7801\uff0c\u800c\u4e0d\u9700\u8003\u8651\u4ee3\u7801\u5e93\u4e2d\u5176\u4ed6\u4efb\u4f55\u4ee3\u7801\u3002\u8fd9\u6837\u4f60\u53ef\u4ee5\u4e13\u6ce8\u5728\u5f53\u524d\u4ee3\u7801\u7247\u6bb5\u4e0a\uff0c\u51cf\u5c11\u7f16\u5199\u65f6\u5927\u8111\u7684\u601d\u8003\u8d1f\u62c5\uff0c\u4e5f\u51cf\u5c11\u4e86\u7ef4\u62a4\u9605\u8bfb\u65f6\u7684\u7406\u89e3\u8d1f\u62c5\u3002\u8fd9\u6837\u7684\u7f16\u7a0b\u6a21\u578b\u5f88\u7b26\u5408\u5355\u4e00\u804c\u8d23\u539f\u5219\uff08SRP\uff09\uff0c\u5927\u5927\u63d0\u9ad8\u4e86\u5de5\u4f5c\u6548\u7387 \u3002\n\n### \u4e8c\u3001Closure\n\n#### 1.\u4ec0\u4e48\u662f\u95ed\u5305\n\n\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u7684\u5f15\u7528\uff0c\u5c31\u5f62\u6210\u4e86\u4e00\u4e2a\u95ed\u5305\u3002\n\n**\u95ed\u5305:** \u5f53\u51fd\u6570\u80fd\u8bb0\u4f4f\u5e76\u8bbf\u95ee\u6240\u5728\u8bcd\u6cd5\u4f5c\u7528\u57df\uff0c\u5373\u4f7f\u51fd\u6570\u5728\u8bcd\u6cd5\u4f5c\u7528\u57df\u8303\u56f4\u4e4b\u5916\uff0c\u6b64\u65f6\u5c31\u4ea7\u751f\u4e86\u95ed\u5305\u3002\n\n\u4ece\u5b9e\u73b0\u4e0a\u8bb2\uff0c\u95ed\u5305\u662f\u4e00\u6761\u8bb0\u5f55\uff0c\u5b83\u5b58\u50a8\u4e86\u4e00\u4e2a\u51fd\u6570\u4e0e\u5176\u73af\u5883\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u4e3b\u8981\u8bb0\u5f55\u4e86\uff1a\u51fd\u6570\u7684\u6bcf\u4e2a\u81ea\u7531\u53d8\u91cf\uff08\u5728\u5185\u5c42\u51fd\u6570\u4f7f\u7528\uff0c\u4f46\u5728\u5916\u5c42\u51fd\u6570\u5904\u88ab\u5b9a\u4e49\u7684\u53d8\u91cf\uff09\u4e0e\u5176\u88ab\u7ed1\u5b9a\u7684\u503c\u6216\u5f15\u7528\u4e4b\u95f4\u7684\u5173\u8054,\u4e3b\u8981\u662f\u7528\u6765\u5b9e\u73b0\u4fe1\u606f\u5c01\u88c5\u3002\n\n#### 2.\u95ed\u5305\u7684\u4f5c\u7528\n\n\u95ed\u5305\u6700\u5b9e\u7528\u7684\u4f8b\u5b50\u5c31\u662f\u4f7f\u5c40\u90e8\u4f5c\u7528\u57df\u5916\u80fd\u8c03\u7528\u5c40\u90e8\u4f5c\u7528\u57df\u5185\u7684\u53d8\u91cf\u3002\n\n```js\nvar closure = function () {\n var innerVar = 'inner'\n return function () {\n console.log(innerVar)\n }\n}\n\nvar getInner = closure()\ngetInner() // \u5c31\u53bb\u5230\u4e86\u5c40\u90e8\u4f5c\u7528\u57df\u5185\u7684\u53d8\u91cf\u58f0\u660e inner\n```\n\n\u5f53\u7136\u8fd9\u53ea\u662f\u95ed\u5305\u7684\u4e00\u79cd\u573a\u666f\uff0c\u5e76\u4e0d\u662f\u8bf4\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u624d\u662f\u95ed\u5305\uff0c\u6362\u800c\u8a00\u4e4b\uff0c\u7b80\u5355\u5730\u4f7f\u8bcd\u6cd5\u4f5c\u7528\u57df\u7684\u5916\u5c42\u53ef\u4ee5\u8bbf\u95ee\u5176\u4e2d\u7684\u53d8\u91cf\uff0c\u8fd9\u4fbf\u521b\u5efa\u4e86\u4e00\u4e2a\u95ed\u5305\u3002\n\n### \u4e09\u3001Prototype\n\n\u539f\u578b:\u6bcf\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u90fd\u5177\u6709\u4e00\u4e2aprototype\u5c5e\u6027\u3002\u6784\u9020\u51fd\u6570\u521b\u9020\u7684\u5b9e\u4f8b\u5bf9\u8c61\u5b9e\u4f8b\u5185\u90e8\u6709\u4e00\u4e2a\u5185\u90e8\u5c5e\u6027[[prototype]],\u4f5c\u4e3a\u4e00\u4e2a\u6307\u9488\uff0c\u6307\u5411\u6784\u9020\u51fd\u6570\u539f\u578b\u6240\u6307\u7684\u5bf9\u8c61.\u6240\u6709\u7684\u5b9e\u4f8b\u5171\u4eab\u539f\u578b\u5bf9\u8c61\u4e0a\u7684\u5c5e\u6027\u548c\u65b9\u6cd5 \n\n\u539f\u578b\u662fjs\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u6362\u53e5\u8bdd\u8bf4\uff0c \u539f\u578b\u662fjs\u8bed\u8a00\u7684\u4e00\u4e2a\u7279\u5f81\u3002js\u4e2d\u4efb\u4f55\u5bf9\u8c61\u90fd\u6709\u539f\u578b\uff0c\u51fd\u6570\u5bf9\u8c61\u6709\u539f\u578b\uff08\u53ea\u4e0d\u8fc7\u51fd\u6570\u4e0d\u5145\u5f53\u6784\u9020\u51fd\u6570\u65f6\uff0c\u539f\u578b\u4e0d\u8d77\u4f5c\u7528\uff09\uff0c\u666e\u901a\u7684js`object`\u4e5f\u6709\u539f\u578b\uff0c\u539f\u578b\u662f\u4e00\u4e2a`object`\uff0c\u5b83\u4e5f\u6709\u539f\u578b\uff0c\u8fd9\u5c31\u6784\u6210\u4e86\u4e00\u4e2a**\u539f\u578b\u94fe**\uff0c\u76f4\u5230`Object.prototype`\u3002`Object.prototype`\u7684\u539f\u578b\u662f`null`\u3002\n\n\u8bfb\u53d6\u5bf9\u8c61\u7684\u67d0\u4e2a\u5c5e\u6027\u65f6\uff0c`JavaScript` \u5f15\u64ce\u5148\u5bfb\u627e\u5bf9\u8c61\u672c\u8eab\u7684\u5c5e\u6027\uff0c\u5982\u679c\u627e\u4e0d\u5230\uff0c\u5c31\u5230\u5b83\u7684\u539f\u578b\u53bb\u627e\uff0c\u5982\u679c\u8fd8\u662f\u627e\u4e0d\u5230\uff0c\u5c31\u5230\u539f\u578b\u7684\u539f\u578b\u53bb\u627e\u3002\u5982\u679c\u76f4\u5230\u6700\u9876\u5c42\u7684`Object.prototype`\u8fd8\u662f\u627e\u4e0d\u5230\uff0c\u5219\u8fd4\u56de`undefined`\u3002\u5982\u679c\u5bf9\u8c61\u81ea\u8eab\u548c\u5b83\u7684\u539f\u578b\uff0c\u90fd\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540c\u540d\u5c5e\u6027\uff0c\u90a3\u4e48\u4f18\u5148\u8bfb\u53d6\u5bf9\u8c61\u81ea\u8eab\u7684\u5c5e\u6027\uff0c\u8fd9\u53eb\u505a\u201c\u8986\u76d6\u201d\u3002\n\n### \u56db\u3001\u5c01\u88c5\n\n\u5c01\u88c5\uff0c\u4e5f\u5c31\u662f\u628a\u5ba2\u89c2\u4e8b\u7269\u5c01\u88c5\u6210\u62bd\u8c61\u7684\u7c7b\uff0c\u5e76\u4e14\u7c7b\u53ef\u4ee5\u628a\u81ea\u5df1\u7684\u6570\u636e\u548c\u65b9\u6cd5\u53ea\u8ba9\u53ef\u4fe1\u7684\u7c7b\u6216\u8005\u5bf9\u8c61\u64cd\u4f5c\uff0c\u5bf9\u4e0d\u53ef\u4fe1\u7684\u8fdb\u884c\u4fe1\u606f\u9690\u85cf\u3002\u5c01\u88c5\u662f\u9762\u5411\u5bf9\u8c61\u7684\u7279\u5f81\u4e4b\u4e00\uff0c\u662f\u5bf9\u8c61\u548c\u7c7b\u6982\u5ff5\u7684\u4e3b\u8981\u7279\u6027\u3002 \n\n\u901a\u4fd7\u7684\u8bb2\u5c31\u662f\uff0c\u5c06\u7528\u6237\u4e0d\u9700\u8981\u77e5\u9053\u7684\u6570\u636e\u548c\u65b9\u6cd5\u9690\u85cf\u8d77\u6765\uff0c\u5916\u90e8\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u3002\u5728Java\u4e2d\uff0c\u53ef\u4ee5\u7528private\uff0c protected\u5173\u952e\u5b57\u8fdb\u884c\u4fee\u9970\u3002\u5728JS\u4e2d\u53ef\u4ee5\u7528**\u95ed\u5305**\u5b9e\u73b0\u3002\n\n```js\nvar person = {\n fullName : \"coolb\",\n};\n \nalert(person.fullName); // coolb\nperson.fullName = \"jay\";\nalert(person.fullName); // jay\n```\n\n\u8fd9\u91ccperson\u5bf9\u8c61\u7684fullName\u5c5e\u6027\u662f\u66b4\u9732\u7ed9\u7528\u6237\u7684\uff0c\u4f60\u65e0\u6cd5\u4fdd\u8bc1\u7528\u6237\u603b\u662f\u8d4b\u5408\u6cd5\u7684\u540d\u5b57\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u95ed\u5305\u3002\n\n```js\nvar person = function () {\n \n var fullName = \"coolb\";\n var reg = new RegExp(/\\d+/);\n \n return { \n setFullName : function (newValue) {\n if(reg.test(newValue)) {\n console.log(\"Invalid Name\");\n }\n else {\n fullName = newValue;\n }\n },\n getFullName : function () {\n return fullName; \n }\n }\n} \n \nvar p = person();\nconsole.log(p.getFullName()); // coolb\np.setFullName('jay');\nconsole.log(p.getFullName()); // jay\np.setFullName(42); // Invalid Name\np.fullName = 42; // \u8fd9\u79cd\u65b9\u5f0f\u5e76\u4e0d\u5f71\u54cd\u5185\u90e8fullName\u7684\u503c\nconsole.log(p.getFullName()); // jay\n```\n\n\n\u7b80\u5355\u7684\u8bf4\uff0c\u4e00\u4e2a\u7c7b\u5c31\u662f\u4e00\u4e2a\u5c01\u88c5\u4e86\u6570\u636e\u4ee5\u53ca\u64cd\u4f5c\u8fd9\u4e9b\u6570\u636e\u7684\u4ee3\u7801\u7684\u903b\u8f91\u5b9e\u4f53\u3002\u5728\u4e00\u4e2a\u5bf9\u8c61\u5185\u90e8\uff0c\u67d0\u4e9b\u4ee3\u7801\u6216\u67d0\u4e9b\u6570\u636e\u53ef\u4ee5\u662f\u79c1\u6709\u7684\uff0c\u4e0d\u80fd\u88ab\u5916\u754c\u8bbf\u95ee\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5bf9\u8c61\u5bf9\u5185\u90e8\u6570\u636e\u63d0\u4f9b\u4e86\u4e0d\u540c\u7ea7\u522b\u7684\u4fdd\u62a4\uff0c\u4ee5\u9632\u6b62\u7a0b\u5e8f\u4e2d\u65e0\u5173\u7684\u90e8\u5206\u610f\u5916\u7684\u6539\u53d8\u6216\u9519\u8bef\u7684\u4f7f\u7528\u4e86\u5bf9\u8c61\u7684\u79c1\u6709\u90e8\u5206\u3002\n\n**1\uff09\u521b\u5efa\u4e00\u4e2a\u7c7b**\n\n\u5728ES6\u4e4b\u524d\u521b\u5efa\u4e00\u4e2a\u7c7b\uff1a\u9996\u5148\u58f0\u660e\u4e00\u4e2a\u51fd\u6570\u4fdd\u5b58\u5728\u4e00\u4e2a\u5bf9\u8c61\u91cc\uff0c\u7136\u540e\u6309\u7167\u7f16\u7a0b\u4e60\u60ef\u8fd9\u4e2a\u4ee3\u8868\u7c7b\u7684\u53d8\u91cf\u540d\u9996\u5b57\u6bcd\u5927\u5199\uff0c\u7136\u540e\u518d\u8fd9\u4e2a\u51fd\u6570\u7684\u5185\u90e8\u901a\u8fc7this\uff08\u51fd\u6570\u5185\u90e8\u81ea\u5e26\u7684\u4e00\u4e2a\u53d8\u91cf\uff0c\u7528\u4e8e\u6307\u5411\u5f53\u524d\u8fd9\u4e2a\u5bf9\u8c61\uff09\u53d8\u91cf\uff0c\u6dfb\u52a0\u5c5e\u6027\u6216\u8005\u65b9\u6cd5\u6765\u5b9e\u73b0\u5bf9\u7c7b\u5c5e\u6027\u6216\u65b9\u6cd5\u7684\u6dfb\u52a0\u7ed9\u7c7b\u6dfb\u52a0\u5c5e\u6027\u7684\u65f6\u5019\u6709\u4e24\u79cd\u65b9\u6cd5\uff0c\u4e00\u79cd\u662f\u76f4\u63a5\u7528this\u6307\u5411\u8fdb\u884c\u5c5e\u6027\u7684\u8d4b\u503c\uff0c\u4e00\u79cd\u662f\u901a\u8fc7prototype\u5c5e\u6027\u8fdb\u884c\u8d4b\u503c\uff0c\u4f7f\u7528this\u8fdb\u884c\u5c5e\u6027\u6dfb\u52a0\u7684\u65f6\u5019\u6240\u6709\u7684\u5b9e\u4f8b\u5316\u5bf9\u8c61\u90fd\u4f1a\u521b\u5efa\u5e76\u4e14\u5177\u6709\u8fd9\u4e2a\u5c5e\u6027\u7684\u5b9e\u9645\u5185\u5bb9\u4f46\u662f\u4f7f\u7528prototype\u5c5e\u6027\u8fdb\u884c\u5c5e\u6027\u6dfb\u52a0\uff0c\u5176\u5b9e\u6240\u6709\u7684\u5b9e\u4f8b\u5316\u5bf9\u8c61\u672c\u8eab\u662f\u4e0d\u5305\u542b\u8be5\u5c5e\u6027\u7684\uff0c\u53ea\u662f\u5c06\u8be5\u5c5e\u6027\u6dfb\u52a0\u5230\u8be5\u5bf9\u8c61\u7684\u539f\u578b\u5c5e\u6027\u4e0a\uff0c\u4f46\u662f\u7531\u4e8ejs\u7684\u539f\u578b\u94fe\u7684\u539f\u7406\uff0c\u6240\u6709\u7684\u539f\u578b\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u8c03\u7528\u548c\u4f7f\u7528\u8be5\u5c5e\u6027\n\n**2\uff09\u5c5e\u6027\u4e0e\u65b9\u6cd5\u7684\u5c01\u88c5**\n\n- \u79c1\u6709\u5c5e\u6027\uff1a\u5bf9\u8c61\u7684\u5c5e\u6027\u4e0d\u80fd\u88ab\u8bbf\u95ee\u8005\u770b\u5230\uff1b\u53ea\u80fd\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u3002\u597d\u5904\u5c31\u662f\u5b89\u5168\uff0c\u5c31\u7c7b\u4f3c\u95ed\u5305\u4e2d\u7684\u51fd\u6570\u4e00\u6837\uff0c\u51cf\u5c11\u6c61\u67d3\n- \u516c\u6709\u5c5e\u6027\uff08\u5171\u6709\u5c5e\u6027\uff09\uff1a\u5f53\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u5bf9\u8c61\u540e\uff0c\u4f7f\u7528\u5bf9\u8c61\u7684\u4eba\u5728\u5b9e\u4f8b\u5316\u4e4b\u540e\u53ef\u4ee5\u8bbf\u95ee\u5230\u5bf9\u8c61\u5185\u90e8\u7684\u5c5e\u6027\uff1b\n- \u79c1\u6709\u65b9\u6cd5\uff1a\u5bf9\u8c61\u7684\u65b9\u6cd5\u4e0d\u80fd\u88ab\u8bbf\u95ee\u8005\u770b\u5230\uff1b\u53ea\u80fd\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u3002\u597d\u5904\u5c31\u662f\u5b89\u5168\uff0c\u5c31\u7c7b\u4f3c\u95ed\u5305\u4e2d\u7684\u51fd\u6570\u4e00\u6837\uff0c\u51cf\u5c11\u6c61\u67d3\n- \u6784\u9020\u65b9\u6cd5\uff1a \u901a\u8fc7\u65b9\u6cd5\u4fee\u6539\u5b9e\u4f8b\u4e2d\u5c5e\u6027\u6216\u8005\u79c1\u6709\u5c5e\u6027\u7684\u65b9\u6cd5\n- \u5b9e\u73b0\u65b9\u6cd5\uff1a \u7531\u4e8eJs\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u90fd\u53ea\u4f1a\u5b58\u5728\u6700\u8fd1\u7684\u51fd\u6570\u4f5c\u7528\u57df\u6216\u8005\u6700\u8fd1\u7684\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u6240\u4ee5\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u5305\u88f9\u4f5c\u7528\u57df\uff0c\u5b9e\u73b0\u65b9\u6cd5\u548c\u5c5e\u6027\u7684\u79c1\u6709\u5316\n\n### \u4e94\u3001\u7ee7\u627f\n\n#### 1.\u7ee7\u627f\u662f\u4ec0\u4e48\n\n\u987e\u540d\u601d\u4e49\uff0c\u4e00\u4e2a\u5bf9\u8c61\u4e0a\u60f3\u62e5\u6709\u88ab\u7ee7\u627f\u5bf9\u8c61\u7684\u65b9\u6cd5\u5c5e\u6027\uff0c\u7ee7\u627f\u8fc7\u6765\u5c31\u597d\u4e86\n\n\u5728`OOP`\u4e2d\uff0c\u901a\u8fc7\u7c7b\u7684\u7ee7\u627f\u6765\u5b9e\u73b0\u4ee3\u7801\u7684\u590d\u7528\uff0c\u901a\u8fc7\u5b9e\u4f8b\u5316\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u521b\u5efa\u8bb8\u591a\u5bf9\u8c61\uff0c\u5728JS\u4e2d\u7ee7\u627f\u662f\u901a\u8fc7\u539f\u578b\u5b9e\u73b0\u7684\u3002\n\n```js\nlet user = function(name) {\n this.name = name;\n this.getName = function () {\n console.log(this.name);\n }\n};\n \n//\u4e3a\u4e86\u907f\u514d\u4e0b\u9762\u6bd4\u8f83name\u65f6\uff0c\u5bf9\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u8fd9\u91cc\u6545\u610f\u4f20\u5165\u4e86String\u5bf9\u8c61\nlet user1 = new user(new String('KK'));\nlet user2 = new user(new String('KK'));\n \nconsole.log(user1.name === user2.name); //\u8f93\u51fafalse\nconsole.log(user1.getName === user2.getName); //\u8f93\u51fafalse\n```\n\n\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u6784\u9020\u51fd\u6570user\uff0c\u521b\u5efa\u4e86\u4e24\u4e2a\u5bf9\u8c61\u3002\u5b9e\u9645\u4e0a\u662f\u901a\u8fc7\u590d\u5236 \u6784\u9020\u51fd\u6570user\u7684\u539f\u578b\u5bf9\u8c61 \u6765\u521b\u5efauser1\u548cuser2\u3002\u539f\u578b\u5bf9\u8c61\u4e2d\u6709\u4e2aconstructor\u6307\u5411\u4e86user\u51fd\u6570\uff0c\u5b9e\u9645\u4e0a\u8fd8\u662f\u901a\u8fc7\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u7684\u5bf9\u8c61\u3002\n\n\u5047\u5982\u4e0d\u7528\u539f\u578b\uff08\u66f4\u51c6\u786e\u5730\u8bf4\u539f\u578b\u5bf9\u8c61\u4e2d\u6ca1\u6709\u7528\u6237\u5b9a\u4e49\u7684\u5c5e\u6027\uff09\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u5bf9\u8c61\u5c31\u65e0\u6cd5\u5171\u4eab\u4efb\u4f55\u5c5e\u6027\uff0c\u5bf9\u4e8e\u8fd9\u4e2a\u4f8b\u5b50\u6765\u8bf4\uff0cgetName\u7684\u903b\u8f91\u662f\u4e00\u6837\u7684\uff0c\u4e0d\u9700\u8981\u4e24\u4efdgetName\uff0c\u6240\u6709\u7684user\u5bf9\u8c61\u5176\u5b9e\u53ef\u4ee5\u5171\u4eab\u8fd9\u4e2agetName\u65b9\u6cd5\u3002\u8fd9\u4e2a\u903b\u8f91\u975e\u5e38\u50cfjava\u7c7b\u4e2d\u7684\u9759\u6001\u51fd\u6570\uff0c\u53ea\u4e0d\u8fc7\u9759\u6001\u51fd\u6570\u53ea\u80fd\u591f\u8c03\u7528\u9759\u6001\u53d8\u91cf\u548c\u9759\u6001\u65b9\u6cd5\u3002\u4f46\u662f\u5728JS\u7684\u4e16\u754c\u91cc\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06getName\u5b9a\u4e49\u5728\u539f\u578b\u4e2d\uff0c\u5df2\u8fbe\u5230\u6240\u6709\u5bf9\u8c61\u5171\u4eab\u8fd9\u4e2a\u51fd\u6570\u3002\n\n```js\nlet user = function(name) {\n this.name = name;\n};\n \nuser.prototype.getName = function () {\n console.log(this.name);\n}\n \nuser.prototype.color = new String('White');\n \nlet user1 = new user(new String('KK'));\nlet user2 = new user(new String('KK'));\n \nconsole.log(user1.name === user2.name); //\u8f93\u51fafalse\nconsole.log(user1.getName === user2.getName); //\u8f93\u51fatrue\nconsole.log(user1.color === user2.color); //\u8f93\u51fatrue\n```\n\n\u8fd9\u91cc\u5c31\u4e00\u76ee\u4e86\u7136\u4e86\u3002\u5728\u539f\u578b\u5bf9\u8c61\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u548c\u65b9\u6cd5\u80fd\u591f\u88ab\u6240\u6709\u591a\u4e2a\u5bf9\u8c61\u5171\u4eab\u3002\u539f\u578b\u7684\u5c5e\u6027\u88ab\u5bf9\u8c61\u5171\u4eab\uff0c\u4f46\u662f\u5b83\u4e0d\u5c5e\u4e8e\u5bf9\u8c61\u672c\u8eab\u3002\n\n```js\nuser1.hasOwnProperty('name'); //true;\nuser1.hasOwnProperty('getName'); //false;\n```\n\n\u8fd9\u91cc\u8981\u6ce8\u610f\uff1a\u539f\u578b\u5bf9\u8c61\u7684\u5c5e\u6027\u4e0d\u662f\u5b9e\u4f8b\u5bf9\u8c61\u81ea\u8eab\u7684\u5c5e\u6027\u3002\u53ea\u8981\u4fee\u6539\u539f\u578b\u5bf9\u8c61\uff0c\u53d8\u52a8\u5c31\u7acb\u523b\u4f1a\u4f53\u73b0\u5728**\u6240\u6709**\u5b9e\u4f8b\u5bf9\u8c61\u4e0a\u3002\u53cd\u4e4b\uff0c\u5982\u679c\u5bf9\u8c61\u7684\u5c5e\u6027\u88ab\u4fee\u6539\uff0c\u539f\u578b\u7684\u5bf9\u8c61\u4e2d\u76f8\u540c\u7684\u5c5e\u6027\u5e76\u4e0d\u4f1a\u4fee\u6539\u3002\n\n#### 2.\u7ee7\u627f\u7684\u5b9e\u73b0\u539f\u7406\n\n\u4ece\u6784\u9020\u51fd\u6570\u5b9e\u4f8b\u5316\u8bf4\u8d77\uff0c\u5f53\u4f60\u518d\u8c03\u7528`new`\u7684\u65f6\u5019\uff0cjs\u5b9e\u9645\u4e0a\u6267\u884c\u7684\u662f\n\n```js\nvar o = new Object();\no.__proto__ = Foo.prototype;\nFoo.call(o);\n```\n\n\u7136\u540e\uff0c\u5f53\u4f60\u6267\u884c\n\n```js\no.someProp\n```\n\n\u5b83\u68c0\u67e5 o \u662f\u5426\u5177\u6709 `someProp` \u5c5e\u6027\u3002\u5982\u679c\u6ca1\u6709\uff0c\u5b83\u4f1a\u67e5\u627e`Object.getPrototypeOf(o).someProp`\uff0c\u5982\u679c\u4ecd\u65e7\u6ca1\u6709\uff0c\u5b83\u4f1a\u7ee7\u7eed\u67e5\u627e `Object.getPrototypeOf(Object.getPrototypeOf(o)).someProp`\u3002\n\n**\u5b9e\u73b0\u4e00\u4e2a\u7ee7\u627f\uff1aB\u7ee7\u627f\u81eaA**\n\n```js\nfunction A(a){\n this.varA = a;\n}\n\n// \u4ee5\u4e0a\u51fd\u6570 A \u7684\u5b9a\u4e49\u4e2d\uff0c\u65e2\u7136 A.prototype.varA \u603b\u662f\u4f1a\u88ab this.varA \u906e\u853d\uff0c\n// \u90a3\u4e48\u5c06 varA \u52a0\u5165\u5230\u539f\u578b\uff08prototype\uff09\u4e2d\u7684\u76ee\u7684\u662f\u4ec0\u4e48\uff1f\nA.prototype = {\n varA : null,\n/*\n\u65e2\u7136\u5b83\u6ca1\u6709\u4efb\u4f55\u4f5c\u7528\uff0c\u5e72\u561b\u4e0d\u5c06 varA \u4ece\u539f\u578b\uff08prototype\uff09\u53bb\u6389 ? \n\u4e5f\u8bb8\u4f5c\u4e3a\u4e00\u79cd\u5728\u9690\u85cf\u7c7b\u4e2d\u4f18\u5316\u5206\u914d\u7a7a\u95f4\u7684\u8003\u8651 \n\u5982\u679cvarA\u5e76\u4e0d\u662f\u5728\u6bcf\u4e2a\u5b9e\u4f8b\u4e2d\u90fd\u88ab\u521d\u59cb\u5316\uff0c\u90a3\u8fd9\u6837\u505a\u5c06\u662f\u6709\u6548\u679c\u7684\u3002\n*/\n doSomething : function(){\n // ...\n }\n}\n\nfunction B(a, b){\n A.call(this, a);\n this.varB = b;\n}\nB.prototype = Object.create(A.prototype, {\n varB : {\n value: null, \n enumerable: true, \n configurable: true, \n writable: true \n },\n doSomething : { \n value: function(){ // override\n A.prototype.doSomething.apply(this, arguments); \n // call super\n // ...\n },\n enumerable: true,\n configurable: true, \n writable: true\n }\n});\nB.prototype.constructor = B;\n\nvar b = new B();\nb.doSomething();\n```\n\n**\u6700\u91cd\u8981\u7684\u90e8\u5206**\n\n- \u7c7b\u578b\u88ab\u5b9a\u4e49\u5728 `.prototype` \u4e2d\n- \u7528 `Object.create()` \u6765\u7ee7\u627f\n\n\n`Object.create()`\u7684\u5b9e\u73b0\u539f\u7406\u6216\u8005\u8bf4\u662f\u5bf9`ES5`\u4e4b\u524d\u7248\u672c\u7684polyfill\n\n```js\nif (typeof Object.create !== \"function\") {\n Object.create = function (proto, propertiesObject) {\n if (typeof proto !== 'object' && typeof proto !== 'function') {\n throw new TypeError('Object prototype may only be an Object: ' + proto);\n } else if (proto === null) {\n throw new Error(\"This browser's implementation of Object.create is a shim and doesn't support 'null' as the first argument.\");\n }\n\n if (typeof propertiesObject != 'undefined') throw new Error(\"This browser's implementation of Object.create is a shim and doesn't support a second argument.\");\n\n function F() {}\n F.prototype = proto;\n\n return new F();\n };\n}\n```\n\n#### 3.\u7ee7\u627f\u7684\u65b9\u5f0f\n\n**1\uff09\u7c7b\u7ee7\u627f**\n\n```js\nfunction SuperClass(){\n this.SuperObj = {}\n}\nSuperClass.prototype.SuperMethod = function(){}\n\nfunction SubClass(){\n \n}\nSubClass.prototype.SubMethod = function(){\n \n}\nSubClass.prototype = new SuperClass()\n//\u5c06\u5b50\u7c7b\u7684prototype\u5c5e\u6027\u8bbe\u7f6e\u4e3a\u7236\u7c7b\u7684\u5b9e\u4f8b\n\n//\u5b9e\u4f8b\u5316\u65b9\u6cd5\nvar Sub = new SubClass()\n```\n\n**2\uff09\u6784\u9020\u51fd\u6570\u7ee7\u627f**\n\ncall\u65b9\u6cd5\u53ef\u4ee5\u6539\u53d8\u51fd\u6570\u7684\u6267\u884c\u4e0a\u4e0b\u6587\uff0c\u56e0\u6b64\u5728\u5b50\u7c7b\u4e2d\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u5c31\u76f8\u5f53\u4e8e\u4ee5\u5b50\u7c7b\u7684this\u8c03\u7528\u4e86\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u4f46\u662f\u6700\u540e\u4f1a\u8fd4\u56de\u5230\u5b50\u7c7b\u7684\u521b\u5efa\u7684\u5bf9\u8c61\u4e2d\uff0c\u5b9e\u73b0\u4e86\u5b50\u7c7b\u8c03\u7528\u7236\u7c7b\u6784\u9020\u51fd\u6570\u7684\u64cd\u4f5c\uff0c\u53eb\u505a**\u6784\u9020\u51fd\u6570\u7ee7\u627f**\n\n```js\nfunction SuperClass(){\n this.SuperObj = {}\n}\nSuperClass.prototype.SuperMethod = function(){} \n\nfunction SubClass(opt){\n SuperClass.call(this,opt)\n //\u7ee7\u627f\u7236\u7c7b\n} \n\n//\u5b9e\u4f8b\u5316\u65b9\u6cd5\nvar Sub = new SubClass()\n```\n\n**3\uff09\u7ec4\u5408\u7ee7\u627f**\n\n\u65e2\u4e0d\u4f1a\u5b9e\u4f8b\u5c5e\u6027\u5f71\u54cd\u7c7b\u5c5e\u6027\u7684\u5f15\u7528\u7279\u6027\uff0c\u540c\u65f6\u5728\u5b50\u7c7b\u6784\u9020\u51fd\u6570\u4e2d\u6267\u884c\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\u80fd\u90fd\u4f20\u9012\u53c2\u6570\uff0c\u770b\u8d77\u6765\u4e24\u5168\u5176\u7f8e\u8fd1\u4e4e\u5b8c\u7f8e\n\n```js\nfunction SuperClass(){\n this.SuperObj = {}\n }\n SuperClass.prototype.SuperMethod = function(){}\n \n function SubClass(opt){\n SuperClass.call(this,opt)\n //\u7ee7\u627f\u7236\u7c7b\n }\n \n SubClass.prototype = new SuperClass()\n //\u5728\u6784\u9020\u51fd\u6570\u7ee7\u627f\u4e2d\u518d\u6b21\u628a\u539f\u578b\u5c5e\u6027\u8d4b\u503c\u5230\u5b50\u7c7b\u7684\u539f\u578b\u5c5e\u6027\u4e0a\n \n //\u5b9e\u4f8b\u5316\u65b9\u6cd5\n var Sub = new SubClass()\n```\n\n**4\uff09\u539f\u578b\u5f0f\u7ee7\u627f**\n\n```js\nfunction inheritObject(o){\n function F(){}\n F.prototype = o\n return new F()\n}\n\n//\u6d4b\u8bd5\u7528\u4f8b\n\nvar book = {\n name:'js book',\n alikeBook:[\"css\",\"htmlbook\"]\n}\nvar newBook = inheritObject(book)\n//\u5b9e\u73b0\u7ee7\u627f\n\nnewBook.name = 'new book'\n//\u4fee\u6539\u5b50\u7c7b\u5c5e\u6027\n```\n\n**5\uff09\u5bc4\u751f\u7ec4\u5408\u5f0f\u7ee7\u627f**\n\n\u539f\u7406\uff1a\u76f4\u63a5\u5c06\u7236\u7c7b\u7684\u539f\u578b\u590d\u5236\u5230\u5b50\u7c7b\uff0c\u4f46\u662f\u540c\u65f6\u5c06\u7236\u7c7b\u7684\u539f\u578b\u4e2d\u7684\u6784\u9020\u51fd\u6570\u6539\u6210\u5b50\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u53d8\u4e3a\u4e00\u4e2a\u5b50\u7c7b\u7684\u5bf9\u8c61\u5b9e\u73b0\u7ee7\u627f\uff0c\u4e00\u822c\u548c\u5176\u4ed6\u7684\u6784\u9020\u51fd\u6570\u5f0f\u7ee7\u627f\u6df7\u5408\u4f7f\u7528\n\n```js\nfunction inheritObject(o){\n function F(){}\n F.prototype = o\n return new F()\n}\nfunction inheritPrototype(subClass,superClass){\n var p = inheritObject(superClass.prototype)\n //\u590d\u5236\u4e00\u4efd\u539f\u578b\u526f\u672c\u4fdd\u5b58\u5728\u53d8\u91cf\u4e2d\n p.constructor = subClass\n //\u4fee\u6b63\u56e0\u4e3a\u91cd\u5199\u5b50\u7c7b\u539f\u578b\u5bfc\u81f4\u5b50\u7c7b\u7684construtor\u5c5e\u6027\u88ab\u4fee\u6539\n subClass.prototype = p;\n}\n```\n\n### \u516d\u3001\u603b\u7ed3\n\n`Javascript`\u4e2d\u7684\u8fd9\u4e9b\u6982\u5ff5\uff0c`scope`\u3001`closure`\u3001`prototype`\u7b49\u5bf9\u4e8e\u8bed\u8a00\u7684\u7279\u5f81\u6765\u8bf4\u662f\u57fa\u5efa\uff0c\u4ed6\u4eec\u606f\u606f\u76f8\u5173\uff0c\u6ca1\u6709\u54ea\u4e2a\u66f4\u91cd\u8981\uff0c\u90fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u53ea\u6709\u8fd9\u4e9b\u57fa\u7840\u7684\u4e1c\u897f\u641e\u660e\u767d\u4e86\u4e4b\u540e\uff0c\u624d\u80fd\u53bb\u8bfb\u61c2\u4e00\u4e9b\u6846\u67b6\u7684\u6e90\u7801\uff0c\u751a\u81f3\u53bb\u5f00\u53d1\u4e00\u5957\u6846\u67b6\u5f00\u53d1\u4e00\u4e9b\u725bx\u7684\u5f00\u6e90\u5e93\u3002", "answer": "", "biz_type": 1, "qid": 714, "subject": "", "title": "\u8bf7\u63cf\u8ff0\u4e0b JavaScript \u4e2d Scope\u3001Closure\u3001Prototype \u6982\u5ff5\uff0c\u5e76\u8bf4\u660e JavaScript \u5c01\u88c5\u3001\u7ee7\u627f\u5b9e\u73b0\u539f\u7406\u3002"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 6, "qid": 775, "qtype": "short_answer", "short_answer": {"analysis": " ### \u4ee3\u7801\u5b9e\u73b0\n\n- \u4f7f\u7528let\u3001\u7bad\u5934\u51fd\u6570\u3001\u5e8f\u53f7+1\n\n```js\nconst obj = {\n name:\"jsCoder\",\n skill:[\"es6\",\"react\",\"angular\"],\n say:function(){\n for(let i = 0,len = this.skill.length;i<len;i++){\n setTimeout(()=>{\n console.log('No.' + (i+1) + this.name);\n console.log(this.skill[i]);\n console.log('----------------');\n },0);\n console.log(i+1);\n }\n }\n}\nobj.say();\n```", "answer": "", "biz_type": 1, "qid": 775, "subject": "```js\nconst obj = {\n name:\"jsCoder\",\n skill:[\"es6\",\"react\",\"angular\"],\n say:function(){\n for(var i = 0,len = this.skill.length;i<len;i++){\n setTimeout(function(){\n console.log('No.' + i + this.name);\n console.log(this.skill[i]);\n console.log('----------------');\n },0);\n console.log(i);\n }\n }\n}\nobj.say();\n\n/* \n \u671f\u671b\u5f97\u5230\u4e0b\u9762\u7684\u7ed3\u679c\n 1\n 2\n 3\n No.1 jsCoder\n es6\n ----------------\n No.2 jsCoder\n react\n ----------------\n No.3 jsCoder\n angular\n*/\n```\n", "title": "\u4ee5\u6700\u5c0f\u7684\u6539\u52a8\u89e3\u51b3\u4ee5\u4e0b\u4ee3\u7801\u7684\u9519\u8bef(\u53ef\u4ee5\u4f7f\u7528ES6)"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7231\u8303\u513f"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 9, "qid": 702, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 702, "subject": "", "title": "\u6709\u54ea\u51e0\u79cd\u65b9\u5f0f\u53ef\u4ee5\u89e3\u51b3\u8de8\u57df\u95ee\u9898\uff1f(\u63cf\u8ff0\u5bf9\u5e94\u7684\u539f\u7406)"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9ad8\u601d\u6559\u80b2"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 4, "qid": 725, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 725, "subject": "", "title": "\u5982\u4f55\u5b9e\u73b0 a,b \u4e24\u4e2a\u53d8\u91cf\u7684\u4ea4\u6362"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 4, "qid": 748, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 748, "subject": "```js\n//counter.js\nlet counter = 10;\nexport default counter;\n\n//index.js\nimport myCounter from \"./counter\";\nmyCounter += 1;\nconsole.log(myCounter);\n```\n", "title": "\u5199\u51fa\u4e0b\u9762\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 4, "qid": 758, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 758, "subject": "```js\nfunction test(m) {\n m = { v: 5 };\n}\nvar m = { k: 30 };\ntest(m);\nalert(m.v);\n```\n", "title": "\u5199\u51fa\u8f93\u51fa\u503c\uff0c\u5e76\u89e3\u91ca\u4e3a\u4ec0\u4e48"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 4, "qid": 760, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 760, "subject": "```js\nfunction fn() {\n console.log(this.length);\n}\nvar person = {\n length: 5,\n method: function (fn) {\n fn();\n },\n};\nperson.method(fn, 1);\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6267\u2f8f\u7ed3\u679c\uff0c\u5e76\u89e3\u91ca\u4e3a\u4ec0\u4e48"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 5, "qid": 773, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 773, "subject": "", "title": "\u51fd\u6570\u4e2d\u7684arguments\u662f\u6570\u7ec4\u5417\uff1f\u82e5\u4e0d\u662f\uff0c\u5982\u4f55\u5c06\u5b83\u8f6c\u5316\u4e3a\u771f\u6b63\u7684\u6570\u7ec4\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 6, "qid": 774, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 774, "subject": "```js\nif([] == false){console.log(1)};\nif({} == false) {console.log(2)};\nif([]){console.log(3)};\nif([1] == [1]){console.log(4)};\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee5\u4e0b\u4ee3\u7801\u7684\u6253\u5370\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["58"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 6, "qid": 738, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 738, "subject": "", "title": "\u5217\u4e3e 3 \u79cd\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u548c 2 \u79cd\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362"}, "tech_tag": ["\u5de5\u7a0b\u5316", "JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["58"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 6, "qid": 736, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u5fae\u4efb\u52a1\u548c\u5b8f\u4efb\u52a1\u7684\u533a\u522b\n\n\n\u5fae\u4efb\u52a1\u548c\u5b8f\u4efb\u52a1\u662f\u5f02\u6b65\u4efb\u52a1\u7684\u4e24\u4e2a\u79cd\u7c7b\u3002\n\n**\u5b8f\u4efb\u52a1:** \u5f53\u524d\u8c03\u7528\u6808\u4e2d\u6267\u884c\u7684\u4ee3\u7801\u6210\u4e3a\u5b8f\u4efb\u52a1\u3002\uff08\u4e3b\u4ee3\u7801\u5feb\uff0c\u5b9a\u65f6\u5668\u7b49\u7b49\uff09\u3002 \n\n**\u5fae\u4efb\u52a1:** \u5f53\u524d\uff08\u6b64\u6b21\u4e8b\u4ef6\u5faa\u73af\u4e2d\uff09\u5b8f\u4efb\u52a1\u6267\u884c\u5b8c\uff0c\u5728\u4e0b\u4e00\u4e2a\u5b8f\u4efb\u52a1\u5f00\u59cb\u4e4b\u524d\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1,\u53ef\u4ee5\u7406\u89e3\u4e3a\u56de\u8c03\u4e8b\u4ef6\u3002\uff08promise.then\uff0cproness.nextTick\u7b49\u7b49\uff09\u3002\n\n\u5b8f\u4efb\u52a1\u4e2d\u7684\u4e8b\u4ef6\u653e\u5728callback queue\u4e2d\uff0c\u7531\u4e8b\u4ef6\u89e6\u53d1\u7ebf\u7a0b\u7ef4\u62a4\uff1b\u5fae\u4efb\u52a1\u7684\u4e8b\u4ef6\u653e\u5728\u5fae\u4efb\u52a1\u961f\u5217\u4e2d\uff0c\u7531js\u5f15\u64ce\u7ebf\u7a0b\u7ef4\u62a4\u3002\n\n\u5728\u6302\u8d77\u4efb\u52a1\u65f6\uff0cJS\u5f15\u64ce\u4f1a\u5c06\u6240\u6709\u4efb\u52a1\u6309\u7167\u7c7b\u522b\u5206\u5230\u8fd9\u4e24\u4e2a\u961f\u4f0d\u4e2d\uff0c\u9996\u5148\u5728macrotask\u7684\u961f\u5217\u4e2d\u53d6\u51fa\u7b2c\u4e00\u4e2a\u4efb\u52a1\uff0c\u6267\u884c\u5b8c\u6bd5\u540e\u53d6\u51famicrotask\u961f\u5217\u4e2d\u7684\u6240\u6709\u4efb\u52a1\u987a\u5e8f\u6267\u884c\uff1b\u4e4b\u540e\u518d\u53d6macrotask\u4efb\u52a1\uff0c\u5468\u800c\u590d\u59cb\uff0c\u76f4\u81f3\u4e24\u4e2a\u961f\u5217\u7684\u4efb\u52a1\u90fd\u53d6\u5b8c", "answer": "", "biz_type": 1, "qid": 736, "subject": "", "title": "\u5fae\u4efb\u52a1\u548c\u5b8f\u4efb\u52a1\u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7231\u8303\u513f"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 6, "qid": 692, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u5b9e\u73b0\u65b9\u6848\n\n\u5f53\u5bf9Dom\u5143\u7d20\u8fdb\u884c\u4e00\u7cfb\u5217\u64cd\u4f5c\u65f6\uff0c\u5bf9Dom\u8fdb\u884c\u8bbf\u95ee\u548c\u4fee\u6539Dom\u5f15\u8d77\u7684\u91cd\u7ed8\u548c\u91cd\u6392\u90fd\u6bd4\u8f83\u6d88\u8017\u6027\u80fd\uff0c\u6240\u4ee5\u5173\u4e8e\u64cd\u4f5cDom,\u5e94\u8be5\u4ece\u4ee5\u4e0b\u51e0\u70b9\u51fa\u53d1\uff1a\n\n#### 1.\u7f13\u5b58Dom\u5bf9\u8c61 \n\n\u9996\u5148\u4e0d\u7ba1\u5728\u4ec0\u4e48\u573a\u666f\u4e0b\u3002\u64cd\u4f5cDom\u4e00\u822c\u9996\u5148\u4f1a\u53bb\u8bbf\u95eeDom\uff0c\u5c24\u5176\u662f\u50cf\u5faa\u73af\u904d\u5386\u8fd9\u79cd\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u80fd\u4f1a\u6bd4\u8f83\u9ad8\u7684\u64cd\u4f5c\u3002\u90a3\u4e48\u53ef\u4ee5\u5728\u5faa\u73af\u4e4b\u524d\u5c31\u5c06\u4e3b\u8282\u70b9\uff0c\u4e0d\u5fc5\u5faa\u73af\u7684Dom\u8282\u70b9\u5148\u83b7\u53d6\u5230\uff0c\u90a3\u4e48\u5728\u5faa\u73af\u91cc\u5c31\u53ef\u4ee5\u76f4\u63a5\u5f15\u7528\uff0c\u800c\u4e0d\u5fc5\u53bb\u91cd\u65b0\u67e5\u8be2\u3002\n\n```js\nlet rootElem = document.querySelector('#app');\nlet childList = rootElem.child; // \u5047\u8bbe\u5168\u662fdom\u8282\u70b9\nfor(let i = 0;i<childList.len;j++){\n /**\n * \u6839\u636e\u6761\u4ef6\u5bf9\u5e94\u64cd\u4f5c\n */\n}\n```\n\n#### 2.\u6587\u6863\u7247\u6bb5\n\n\u5229\u7528`document.createDocumentFragment()`\u65b9\u6cd5\u521b\u5efa\u6587\u6863\u788e\u7247\u8282\u70b9\uff0c\u521b\u5efa\u7684\u662f\u4e00\u4e2a\u865a\u62df\u7684\u8282\u70b9\u5bf9\u8c61\u3002\u5411\u8fd9\u4e2a\u8282\u70b9\u6dfb\u52a0dom\u8282\u70b9\uff0c\u4fee\u6539dom\u8282\u70b9\u5e76\u4e0d\u4f1a\u5f71\u54cd\u5230\u771f\u5b9e\u7684dom\u7ed3\u6784\u3002\n\n\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u70b9\u5148\u5c06\u6211\u4eec\u9700\u8981\u4fee\u6539\u7684dom\u4e00\u5e76\u4fee\u6539\u5b8c\uff0c\u4fdd\u5b58\u81f3\u6587\u6863\u788e\u7247\u4e2d\uff0c\u7136\u540e\u7528\u6587\u6863\u788e\u7247\u4e00\u6b21\u6027\u7684\u66ff\u6362\u771f\u662f\u7684dom\u8282\u70b9\u3002\u4e0e\u865a\u62dfdom\u7c7b\u4f3c\uff0c\u540c\u6837\u8fbe\u5230\u4e86\u4e0d\u9891\u7e41\u4fee\u6539dom\u800c\u5bfc\u81f4\u7684\u91cd\u6392\u8ddf\u91cd\u7ed8\u7684\u8fc7\u7a0b\u3002\n\n```js\nlet fragment = document.createDocumentFragment();\nconst operationDomHandle = (fragment) =>{\n // \u64cd\u4f5c \n}\noperationDomHandle(fragment);\n// \u7136\u540e\u6700\u540e\u518d\u66ff\u6362 \nrootElem.replaceChild(fragment,oldDom);\n```\n\n\u8fd9\u6837\u5c31\u53ea\u4f1a\u89e6\u53d1\u4e00\u6b21\u56de\u6d41\uff0c\u6548\u7387\u4f1a\u5f97\u5230\u5f88\u5927\u7684\u63d0\u5347\u3002\u5982\u679c\u9700\u8981\u5bf9\u5143\u7d20\u8fdb\u884c\u590d\u6742\u7684\u64cd\u4f5c\uff08\u5220\u51cf\u3001\u6dfb\u52a0\u5b50\u8282\u70b9\uff09\uff0c\u90a3\u4e48\u6211\u4eec\u5e94\u5f53\u5148\u5c06\u5143\u7d20\u4ece\u9875\u9762\u4e2d\u79fb\u9664\uff0c\u7136\u540e\u518d\u5bf9\u5176\u8fdb\u884c\u64cd\u4f5c\uff0c\u6216\u8005\u5c06\u5176\u590d\u5236\u4e00\u4e2a\uff08cloneNode()\uff09\uff0c\u5728\u5185\u5b58\u4e2d\u8fdb\u884c\u64cd\u4f5c\u540e\u518d\u66ff\u6362\u539f\u6765\u7684\u8282\u70b9\u3002\n\n```js\nvar clone=old.cloneNode(true);\noperationDomHandle(clone);\nrootElem.replaceChild(clone,oldDom)\n```\n\n#### 3.\u7528innerHtml \u4ee3\u66ff\u9ad8\u9891\u7684appendChild\n\n#### 4.\u6700\u4f18\u7684layout\u65b9\u6848\n\n\u6279\u91cf\u8bfb\uff0c\u4e00\u6b21\u6027\u5199\u3002\u5148\u5bf9\u4e00\u4e2a\u4e0d\u5728render tree\u4e0a\u7684\u8282\u70b9\u8fdb\u884c\u64cd\u4f5c\uff0c\u518d\u628a\u8fd9\u4e2a\u8282\u70b9\u6dfb\u52a0\u56derender tree\u3002\u8fd9\u6837\u53ea\u4f1a\u89e6\u53d1\u4e00\u6b21DOM\u64cd\u4f5c\u3002 \u4f7f\u7528`requestAnimationFrame()`\uff0c\u628a\u4efb\u4f55\u5bfc\u81f4\u91cd\u7ed8\u7684\u64cd\u4f5c\u653e\u5165`requestAnimationFrame`\n\n#### 5.\u865a\u62dfDom \n\njs\u6a21\u62dfDOM\u6811\u5e76\u5bf9DOM\u6811\u64cd\u4f5c\u7684\u4e00\u79cd\u6280\u672f\u3002virtual DOM\u662f\u4e00\u4e2a\u7eafjs\u5bf9\u8c61\uff08\u5b57\u7b26\u4e32\u5bf9\u8c61\uff09\uff0c\u6240\u4ee5\u5bf9\u4ed6\u64cd\u4f5c\u4f1a\u9ad8\u6548\u3002\n\n\u5229\u7528virtual dom\uff0c\u5c06dom\u62bd\u8c61\u4e3a\u865a\u62dfdom\uff0c\u5728dom\u53d1\u751f\u53d8\u5316\u7684\u65f6\u5019\u5148\u5bf9\u865a\u62dfdom\u8fdb\u884c\u64cd\u4f5c\uff0c\u901a\u8fc7dom diff\u7b97\u6cd5\u5c06\u865a\u62dfdom\u548c\u539f\u865a\u62dfdom\u7684\u7ed3\u6784\u505a\u5bf9\u6bd4\uff0c\u6700\u7ec8\u6279\u91cf\u7684\u53bb\u4fee\u6539\u771f\u5b9e\u7684dom\u7ed3\u6784\uff0c\u5c3d\u53ef\u80fd\u7684\u907f\u514d\u4e86\u9891\u7e41\u4fee\u6539dom\u800c\u5bfc\u81f4\u7684\u9891\u7e41\u7684\u91cd\u6392\u548c\u91cd\u7ed8\u3002\n\n", "answer": "", "biz_type": 1, "qid": 692, "subject": "", "title": "\u6709 1000 \u4e2a dom\uff0c\u9700\u8981\u66f4\u65b0\u5176\u4e2d\u7684 100 \u4e2a\uff0c\u5982\u4f55\u64cd\u4f5c\u624d\u80fd\u51cf\u5c11 dom \u7684\u64cd\u4f5c\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4e9a\u7f8e\u79d1\u6280"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 8, "qid": 722, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n```js\nfunction parseQueryString(url){\n if(typeof url !== 'string') throw new Error('invalid url')\n const search = decodeURIComponent(url).split('?')[1];\n if(!search) return {};\n return search.split('&').reduce((pre, cur) => {\n const [key, value] = cur.split('=');\n pre[key] = value;\n return pre;\n }, {});\n}\nparseQueryString(\"http://iauto360.cn/index.php?key0=0&key1=1&key2=2\")\n```", "answer": "", "biz_type": 1, "qid": 722, "subject": "", "title": "\u8bf7\u7f16\u5199\u4e00\u4e2a JavaScript \u51fd\u6570 parseQueryString,\u5b83\u7684\u7528\u9014\u662f\u628a URL \u53c2\u6570\u89e3\u6790\u4e3a\u4e00\u4e2a\u5bf9\u8c61\uff0curl=\"http://iauto360.cn/index.php?key0=0&key1=1&key2=2\""}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7384\u6b66\u79d1\u6280"], "date": "Fri, 19 Jun 2020 16:20:44 GMT", "favorite_num": 8, "qid": 713, "qtype": "short_answer", "short_answer": {"analysis": "\n\n#### JavaScript\u6807\u7816\u5185\u7f6e\u5bf9\u8c61\n\n\u5168\u5c40\u7684\u5bf9\u8c61\uff08 global objects \uff09\u6216\u79f0\u6807\u51c6\u5185\u7f6e\u5bf9\u8c61\uff0c\u4e0d\u8981\u548c \"\u5168\u5c40\u5bf9\u8c61\uff08global object\uff09\" \u6df7\u6dc6\u3002\u8fd9\u91cc\u8bf4\u7684\u5168\u5c40\u7684\u5bf9\u8c61\u662f\u8bf4\u5728\u5168\u5c40\u4f5c\u7528\u57df\u91cc\u7684\u5bf9\u8c61\u3002\n\n\n### \u4e00\u3001\u5e38\u7528\u7684\u4e94\u79cd\u5185\u7f6e\u5bf9\u8c61\n\n#### 1.encodeURI()\n\n\u51fd\u6570\u901a\u8fc7\u5c06\u7279\u5b9a\u5b57\u7b26\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u66ff\u6362\u4e3a\u4e00\u4e2a\u3001\u4e24\u4e2a\u3001\u4e09\u6216\u56db\u8f6c\u4e49\u5e8f\u5217\u6765\u5bf9\u7edf\u4e00\u8d44\u6e90\u6807\u8bc6\u7b26 (URI) \u8fdb\u884c\u7f16\u7801 (\u8be5\u5b57\u7b26\u7684 UTF-8 \u7f16\u7801\u4ec5\u4e3a\u56db\u8f6c\u4e49\u5e8f\u5217)\u7531\u4e24\u4e2a \"\u4ee3\u7406\" \u5b57\u7b26\u7ec4\u6210)\u3002\n\n#### 2.eval()\n\n\u51fd\u6570\u4f1a\u5c06\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u5f53\u505a JavaScript \u4ee3\u7801\u8fdb\u884c\u6267\u884c\u3002\n\n#### 3.isFinite()\n\n\u51fd\u6570\u7528\u6765\u5224\u65ad\u88ab\u4f20\u5165\u7684\u53c2\u6570\u503c\u662f\u5426\u4e3a\u4e00\u4e2a\u6709\u9650\u6570\u503c\uff08finite number\uff09\u3002\u5728\u5fc5\u8981\u60c5\u51b5\u4e0b\uff0c\u53c2\u6570\u4f1a\u9996\u5148\u8f6c\u4e3a\u4e00\u4e2a\u6570\u503c\u3002 \n\n#### 4.isNaN()\n\n\u51fd\u6570\u7528\u6765\u786e\u5b9a\u4e00\u4e2a\u503c\u662f\u5426\u4e3a `NaN`\uff0c\u53e6\u5916\u4e5f\u53ef\u4ee5\u4f7f\u7528 ECMAScript 2015 \u4e2d\u5b9a\u4e49\u7684 `Number.isNaN()` \u6765\u5224\u65ad\u3002\n\n> \u6ce8\uff1a`isNaN`\u51fd\u6570\u5185\u5305\u542b\u4e00\u4e9b\u975e\u5e38\u6709\u8da3\u7684\u89c4\u5219,\u8be6\u7ec6\u770b\u67e5\u770bMDN\n\n#### 5.parseInt(string, radix) \n\n\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32 string \u8f6c\u6362\u4e3a radix \u8fdb\u5236\u7684\u6574\u6570\uff0c radix \u4e3a\u4ecb\u4e8e2-36\u4e4b\u95f4\u7684\u6570\u3002\n\n\n### \u4e8c\u3001JavaScript\u6807\u51c6\u5185\u7f6e\u5bf9\u8c61\n\n\u5168\u5c40\u7684\u5bf9\u8c61\uff08 global objects \uff09\u6216\u79f0\u6807\u51c6\u5185\u7f6e\u5bf9\u8c61\uff0c\u4e0d\u8981\u548c \"\u5168\u5c40\u5bf9\u8c61\uff08global object\uff09\" \u6df7\u6dc6\u3002\u8fd9\u91cc\u8bf4\u7684\u5168\u5c40\u7684\u5bf9\u8c61\u662f\u8bf4\u5728\u5168\u5c40\u4f5c\u7528\u57df\u91cc\u7684\u5bf9\u8c61\u3002\n\n\u6807\u51c6\u7684\u5185\u7f6e\u5bf9\u8c61\u5206\u7c7b\uff1a\n\n#### 1.\u503c\u5c5e\u6027\n\n\u8fd9\u4e9b\u5168\u5c40\u5c5e\u6027\u8fd4\u56de\u4e00\u4e2a\u7b80\u5355\u503c\uff0c\u8fd9\u4e9b\u503c\u6ca1\u6709\u81ea\u5df1\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\n\n- Infinity\n- NaN\n- undefined\n- globalThis\n\n#### 2.\u51fd\u6570\u5c5e\u6027\n\n\u5168\u5c40\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\uff0c\u4e0d\u9700\u8981\u5728\u8c03\u7528\u65f6\u6307\u5b9a\u6240\u5c5e\u5bf9\u8c61\uff0c\u6267\u884c\u7ed3\u675f\u540e\u4f1a\u5c06\u7ed3\u679c\u76f4\u63a5\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\u3002\n\n- eval()\n- uneval()\n- isFinite()\n- isNaN()\n- parseFloat()\n- parseInt()\n- decodeURI()\n- decodeURIComponent()\n- encodeURI()\n- encodeURIComponent()\n\n#### 3.\u57fa\u672c\u5bf9\u8c61\n\n\u987e\u540d\u601d\u4e49\uff0c\u57fa\u672c\u5bf9\u8c61\u662f\u5b9a\u4e49\u6216\u4f7f\u7528\u5176\u4ed6\u5bf9\u8c61\u7684\u57fa\u7840\u3002\u57fa\u672c\u5bf9\u8c61\u5305\u62ec\u4e00\u822c\u5bf9\u8c61\u3001\u51fd\u6570\u5bf9\u8c61\u548c\u9519\u8bef\u5bf9\u8c61\u3002\n\n- Object\n- Function\n- Boolean\n- Symbol\n\n**\u9519\u8bef\u5bf9\u8c61:**\n\n\u9519\u8bef\u5bf9\u8c61\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u57fa\u672c\u5bf9\u8c61\u3002\u5b83\u4eec\u62e5\u6709\u57fa\u672c\u7684 Error \u7c7b\u578b\uff0c\u540c\u65f6\u4e5f\u6709\u591a\u79cd\u5177\u4f53\u7684\u9519\u8bef\u7c7b\u578b\u3002\n\n- Error\n- AggregateError\n- EvalError\n- InternalError\n- RangeError\n- ReferenceError\n- SyntaxError\n- TypeError\n- URIError\n\n#### 4.\u6570\u5b57\u548c\u65e5\u671f\u5bf9\u8c61\n\n\u7528\u6765\u8868\u793a\u6570\u5b57\u3001\u65e5\u671f\u548c\u6267\u884c\u6570\u5b66\u8ba1\u7b97\u7684\u5bf9\u8c61\u3002\n\n- Number\n- BigInt\n- Math\n- Date\n\n#### 5.\u5b57\u7b26\u4e32\n\n\u7528\u6765\u8868\u793a\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\u7684\u5bf9\u8c61\u3002\n\n- String\n- RegExp\n\n#### 6.\u53ef\u7d22\u5f15\u7684\u96c6\u5408\u5bf9\u8c61\n\n\u8fd9\u4e9b\u5bf9\u8c61\u8868\u793a\u6309\u7167\u7d22\u5f15\u503c\u6765\u6392\u5e8f\u7684\u6570\u636e\u96c6\u5408\uff0c\u5305\u62ec\u6570\u7ec4\u548c\u7c7b\u578b\u6570\u7ec4\uff0c\u4ee5\u53ca\u7c7b\u6570\u7ec4\u7ed3\u6784\u7684\u5bf9\u8c61\u3002\n\n- Array\n- Int8Array\n- Uint8Array\n- Uint8ClampedArray\n- Int16Array\n- Uint16Array\n- Int32Array\n- Uint32Array\n- Float32Array\n- Float64Array\n- BigInt64Array\n- BigUint64Array\n\n#### 7.\u4f7f\u7528\u952e\u7684\u96c6\u5408\u5bf9\u8c61\n\n\u8fd9\u4e9b\u96c6\u5408\u5bf9\u8c61\u5728\u5b58\u50a8\u6570\u636e\u65f6\u4f1a\u4f7f\u7528\u5230\u952e\uff0c\u5305\u62ec\u53ef\u8fed\u4ee3\u7684Map \u548c Set\uff0c\u652f\u6301\u6309\u7167\u63d2\u5165\u987a\u5e8f\u6765\u8fed\u4ee3\u5143\u7d20\u3002\n\n- Map\n- Set\n- WeakMap\n- WeakSet\n\n#### 8.\u7ed3\u6784\u5316\u6570\u636e\n\n\u8fd9\u4e9b\u5bf9\u8c61\u7528\u6765\u8868\u793a\u548c\u64cd\u4f5c\u7ed3\u6784\u5316\u7684\u7f13\u51b2\u533a\u6570\u636e\uff0c\u6216\u4f7f\u7528 JSON \uff08JavaScript Object Notation\uff09\u7f16\u7801\u7684\u6570\u636e\u3002\n\n- ArrayBuffer\n- SharedArrayBuffer\n- Atomics\n- DataView\n- JSON\n\n#### 9.\u63a7\u5236\u62bd\u8c61\u5bf9\u8c61\n\n\u63a7\u4ef6\u62bd\u8c61\u53ef\u4ee5\u5e2e\u52a9\u6784\u9020\u4ee3\u7801\uff0c\u5c24\u5176\u662f\u5f02\u6b65\u4ee3\u7801\uff08\u4f8b\u5982\uff0c\u4e0d\u4f7f\u7528\u6df1\u5ea6\u5d4c\u5957\u7684\u56de\u8c03\uff09\u3002\n\n- Promise\n- Generator\n- GeneratorFunction\n- AsyncFunction\n\n#### 10.\u53cd\u5c04\n\n- Reflect\n- Proxy\n\n#### 11.\u56fd\u9645\u5316\n\nECMAScript\u6838\u5fc3\u7684\u9644\u52a0\u529f\u80fd\uff0c\u7528\u4e8e\u652f\u6301\u591a\u8bed\u8a00\u5904\u7406\u3002\n\n- Intl\n- Intl.Collator\n- Intl.DateTimeFormat\n- Intl.ListFormat\n- Intl.NumberFormat\n- Intl.PluralRules\n- Intl.RelativeTimeFormat\n- Intl.Locale\n\n#### 12.WebAssembly\n\n- WebAssembly\n- WebAssembly.Module\n- WebAssembly.Instance\n- WebAssembly.Memory\n- WebAssembly.Table\n- WebAssembly.CompileError\n- WebAssembly.LinkError\n- WebAssembly.RuntimeError\n\n#### 13.\u5176\u5b83\n\n- arguments", "answer": "", "biz_type": 1, "qid": 713, "subject": "", "title": "\u8bf7\u5217\u51fa\u81f3\u5c11 5 \u4e2a JavaScript \u5e38\u7528\u7684\u5185\u7f6e\u5bf9\u8c61\uff0c\u8bf4\u660e\u7528\u9014"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6d77\u98ce\u6559\u80b2"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 6, "qid": 551, "qtype": "short_answer", "short_answer": {"analysis": "Promise \u5bf9\u8c61\u662f JavaScript \u7684\u5f02\u6b65\u64cd\u4f5c\u89e3\u51b3\u65b9\u6848\uff0c\u4e3a\u5f02\u6b65\u64cd\u4f5c\u63d0\u4f9b\u7edf\u4e00\u63a5\u53e3\u3002\u5b83\u8d77\u5230\u4ee3\u7406\u4f5c\u7528\uff08proxy\uff09\uff0c\u5145\u5f53\u5f02\u6b65\u64cd\u4f5c\u4e0e\u56de\u8c03\u51fd\u6570\u4e4b\u95f4\u7684\u4e2d\u4ecb\uff0c\u4f7f\u5f97\u5f02\u6b65\u64cd\u4f5c\u5177\u5907\u540c\u6b65\u64cd\u4f5c\u7684\u63a5\u53e3\u3002Promise \u53ef\u4ee5\u8ba9\u5f02\u6b65\u64cd\u4f5c\u5199\u8d77\u6765\uff0c\u5c31\u50cf\u5728\u5199\u540c\u6b65\u64cd\u4f5c\u7684\u6d41\u7a0b\uff0c\u800c\u4e0d\u5fc5\u4e00\u5c42\u5c42\u5730\u5d4c\u5957\u56de\u8c03\u51fd\u6570\u3002\n\nPromise\u89e3\u51b3\u4e86callback\u56de\u8c03\u5730\u72f1\u7684\u95ee\u9898\uff0casync\u3001await \u662f\u5f02\u6b65\u7684\u7ec8\u6781\u89e3\u51b3\u65b9\u6848\u3002\n\n\u6765\u770b\u4e00\u4e0bJavaScript\u4e2d\u5f02\u6b65\u65b9\u6848\n\n#### 1\uff09\u56de\u8c03\u51fd\u6570\uff08callback\uff09\n\n```js\nsetTimeout(() => {\n // callback \u51fd\u6570\u4f53\n}, 1000)\n```\n\n**\u7f3a\u70b9\uff1a** \u56de\u8c03\u5730\u72f1\uff0c\u4e0d\u80fd\u7528 try catch \u6355\u83b7\u9519\u8bef\uff0c\u4e0d\u80fd return\n\n\u56de\u8c03\u5730\u72f1\u7684\u6839\u672c\u95ee\u9898\u5728\u4e8e\uff1a\n\n- \u7f3a\u4e4f\u987a\u5e8f\u6027\uff1a \u56de\u8c03\u5730\u72f1\u5bfc\u81f4\u7684\u8c03\u8bd5\u56f0\u96be\uff0c\u548c\u5927\u8111\u7684\u601d\u7ef4\u65b9\u5f0f\u4e0d\u7b26\n- \u5d4c\u5957\u51fd\u6570\u5b58\u5728\u8026\u5408\u6027\uff0c\u4e00\u65e6\u6709\u6240\u6539\u52a8\uff0c\u5c31\u4f1a\u7275\u4e00\u53d1\u800c\u52a8\u5168\u8eab\uff0c\u5373\uff08\u63a7\u5236\u53cd\u8f6c\uff09\n- \u5d4c\u5957\u51fd\u6570\u8fc7\u591a\u7684\u591a\u8bdd\uff0c\u5f88\u96be\u5904\u7406\u9519\u8bef\n\n```js\najax('XXX1', () => {\n // callback \u51fd\u6570\u4f53\n ajax('XXX2', () => {\n // callback \u51fd\u6570\u4f53\n ajax('XXX3', () => {\n // callback \u51fd\u6570\u4f53\n })\n })\n})\n```\n\n**\u4f18\u70b9\uff1a** \u89e3\u51b3\u4e86\u540c\u6b65\u7684\u95ee\u9898\uff08\u53ea\u8981\u6709\u4e00\u4e2a\u4efb\u52a1\u8017\u65f6\u5f88\u957f\uff0c\u540e\u9762\u7684\u4efb\u52a1\u90fd\u5fc5\u987b\u6392\u961f\u7b49\u7740\uff0c\u4f1a\u62d6\u5ef6\u6574\u4e2a\u7a0b\u5e8f\u7684\u6267\u884c\u3002\uff09\n\n#### 2\uff09Promise\n\nPromise\u5c31\u662f\u4e3a\u4e86\u89e3\u51b3callback\u7684\u95ee\u9898\u800c\u4ea7\u751f\u7684\u3002Promise \u5b9e\u73b0\u4e86\u94fe\u5f0f\u8c03\u7528\uff0c\u4e5f\u5c31\u662f\u8bf4\u6bcf\u6b21 then \u540e\u8fd4\u56de\u7684\u90fd\u662f\u4e00\u4e2a\u5168\u65b0 Promise\uff0c\u5982\u679c\u6211\u4eec\u5728 then \u4e2d return \uff0creturn \u7684\u7ed3\u679c\u4f1a\u88ab Promise.resolve() \u5305\u88c5\n\n**\u4f18\u70b9\uff1a** \u89e3\u51b3\u4e86\u56de\u8c03\u5730\u72f1\u7684\u95ee\u9898\n\n```js\najax('XXX1')\n .then(res => {\n // \u64cd\u4f5c\u903b\u8f91\n return ajax('XXX2')\n }).then(res => {\n // \u64cd\u4f5c\u903b\u8f91\n return ajax('XXX3')\n }).then(res => {\n // \u64cd\u4f5c\u903b\u8f91\n })\n```\n\n**\u7f3a\u70b9\uff1a** \u65e0\u6cd5\u53d6\u6d88 Promise \uff0c\u9519\u8bef\u9700\u8981\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u6765\u6355\u83b7\n\n#### 3\uff09Generator\n\n**\u7279\u70b9\uff1a** \u53ef\u4ee5\u63a7\u5236\u51fd\u6570\u7684\u6267\u884c\uff0c\u53ef\u4ee5\u914d\u5408 co \u51fd\u6570\u5e93\u4f7f\u7528\n\n```js\nfunction *fetch() {\n yield ajax('XXX1', () => {})\n yield ajax('XXX2', () => {})\n yield ajax('XXX3', () => {})\n}\nlet it = fetch()\nlet result1 = it.next()\nlet result2 = it.next()\nlet result3 = it.next()\n```\n\n#### 4\uff09Async/await\n\nasync\u3001await \u662f\u5f02\u6b65\u7684\u7ec8\u6781\u89e3\u51b3\u65b9\u6848\n\n**\u4f18\u70b9\uff1a** \u4ee3\u7801\u6e05\u6670\uff0c\u4e0d\u7528\u50cf Promise \u5199\u4e00\u5927\u5806 then \u94fe\uff0c\u5904\u7406\u4e86\u56de\u8c03\u5730\u72f1\u7684\u95ee\u9898\n\n**\u7f3a\u70b9\uff1a** await \u5c06\u5f02\u6b65\u4ee3\u7801\u6539\u9020\u6210\u540c\u6b65\u4ee3\u7801\uff0c\u5982\u679c\u591a\u4e2a\u5f02\u6b65\u64cd\u4f5c\u6ca1\u6709\u4f9d\u8d56\u6027\u800c\u4f7f\u7528 await \u4f1a\u5bfc\u81f4\u6027\u80fd\u4e0a\u7684\u964d\u4f4e\u3002\n\n```js\nasync function test() {\n // \u4ee5\u4e0b\u4ee3\u7801\u6ca1\u6709\u4f9d\u8d56\u6027\u7684\u8bdd\uff0c\u5b8c\u5168\u53ef\u4ee5\u4f7f\u7528 Promise.all \u7684\u65b9\u5f0f\n // \u5982\u679c\u6709\u4f9d\u8d56\u6027\u7684\u8bdd\uff0c\u5176\u5b9e\u5c31\u662f\u89e3\u51b3\u56de\u8c03\u5730\u72f1\u7684\u4f8b\u5b50\u4e86\n await fetch('XXX1')\n await fetch('XXX2')\n await fetch('XXX3')\n}\n```\n\n\n\n\n\n\n\n\n", "answer": "", "biz_type": 1, "qid": 551, "subject": "", "title": "Promise \u6709\u6ca1\u6709\u89e3\u51b3\u5f02\u6b65\u7684\u95ee\u9898"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fae\u533b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 5, "qid": 498, "qtype": "short_answer", "short_answer": {"analysis": "#### promise\u6784\u9020\u51fd\u6570\u662f\u540c\u6b65\u6267\u884c\u7684\uff0cthen\u65b9\u6cd5\u662f\u5f02\u6b65\u6267\u884c\u7684\n\n\u770b\u4e2a\u4ee3\u7801\u793a\u4f8b\n\n```js\nnew Promise(resolve => {\n console.log(1);\n resolve(3);\n}).then(num => {\n console.log(num)\n});\nconsole.log(2)\n\n// \u8f93\u51fa 123\n```\n\n#### \u5177\u4f53\u5206\u6790\u4e0b\n\n**\u8bed\u6cd5**\n\n```js\nnew Promise( function(resolve, reject) {...} /* executor */ ) \n```\n\n- \u6784\u5efa Promise \u5bf9\u8c61\u65f6\uff0c\u9700\u8981\u4f20\u5165\u4e00\u4e2a executor \u51fd\u6570\uff0c\u4e3b\u8981\u4e1a\u52a1\u6d41\u7a0b\u90fd\u5728 executor \u51fd\u6570\u4e2d\u6267\u884c\u3002\n- Promise\u6784\u9020\u51fd\u6570\u6267\u884c\u65f6\u7acb\u5373\u8c03\u7528executor \u51fd\u6570\uff0c resolve \u548c reject \u4e24\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9executor\uff0cresolve \u548c reject \u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u5206\u522b\u5c06promise\u7684\u72b6\u6001\u6539\u4e3afulfilled(\u5b8c\u6210)\u6216rejected(\u5931\u8d25)\u3002\u4e00\u65e6\u72b6\u6001\u6539\u53d8\uff0c\u5c31\u4e0d\u4f1a\u518d\u53d8\uff0c\u4efb\u4f55\u65f6\u5019\u90fd\u53ef\u4ee5\u5f97\u5230\u8fd9\u4e2a\u7ed3\u679c\u3002\n- \u5728 executor \u51fd\u6570\u4e2d\u8c03\u7528 resolve \u51fd\u6570\u540e\uff0c\u4f1a\u89e6\u53d1 promise.then \u8bbe\u7f6e\u7684\u56de\u8c03\u51fd\u6570;\u800c\u8c03\u7528 reject \u51fd\u6570\u540e\uff0c\u4f1a\u89e6\u53d1 promise.catch \u8bbe\u7f6e\u7684\u56de\u8c03\u51fd\u6570\u3002\n\n> \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cPromise \u662f\u7528\u6765\u7ba1\u7406\u5f02\u6b65\u7f16\u7a0b\u7684\uff0c\u5b83\u672c\u8eab\u4e0d\u662f\u5f02\u6b65\u7684\uff0cnew Promise\u7684\u65f6\u5019\u4f1a\u7acb\u5373\u628aexecutor\u51fd\u6570\u6267\u884c\uff0c\u53ea\u4e0d\u8fc7\u6211\u4eec\u4e00\u822c\u4f1a\u5728executor\u51fd\u6570\u4e2d\u5904\u7406\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u3002\u6bd4\u5982\u4e0b\u9762\u4ee3\u7801\u4e2d\uff0c\u4e00\u5f00\u59cb\u662f\u4f1a\u5148\u6253\u5370\u51fa2\u3002\n\n```js\nlet p1 = new Promise(()=>{ \n setTimeout(()=>{ \n console.log(1) \n },1000) \n console.log(2) \n }) \nconsole.log(3) // 2 3 1 \n```\n\nPromise \u91c7\u7528\u4e86\u56de\u8c03\u51fd\u6570\u5ef6\u8fdf\u7ed1\u5b9a\u6280\u672f\uff0c\u5728\u6267\u884c resolve \u51fd\u6570\u7684\u65f6\u5019\uff0c\u56de\u8c03\u51fd\u6570\u8fd8\u6ca1\u6709\u7ed1\u5b9a\uff0c\u90a3\u4e48\u53ea\u80fd\u63a8\u8fdf\u56de\u8c03\u51fd\u6570\u7684\u6267\u884c\u3002\n\n\u518d\u770b\u4e2a\u4f8b\u5b50\n\n```js\nlet p = new Promise((resolve,reject)=>{ \n console.log(1); \n resolve('yideng') \n console.log(2) \n}) \n// then:\u8bbe\u7f6e\u6210\u529f\u6216\u8005\u5931\u8d25\u540e\u5904\u7406\u7684\u65b9\u6cd5 \np.then(result=>{ \n //p\u5ef6\u8fdf\u7ed1\u5b9a\u56de\u8c03\u51fd\u6570 \n console.log('\u6210\u529f '+result) \n},reason=>{ \n console.log('\u5931\u8d25 '+reason) \n}) \nconsole.log(3) \n// 1 \n// 2 \n// 3 \n// \u6210\u529f yideng\n```\n\nnew Promise\u7684\u65f6\u5019\u5148\u6267\u884cexecutor\u51fd\u6570\uff0c\u6253\u5370\u51fa 1\u30012\uff0cPromise\u5728\u6267\u884cresolve\u65f6\uff0c\u89e6\u53d1\u5fae\u4efb\u52a1\uff0c\u8fd8\u662f\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\u540c\u6b65\u4efb\u52a1\uff0c \u6267\u884cp.then\u65f6\uff0c\u5b58\u50a8\u8d77\u6765\u4e24\u4e2a\u51fd\u6570(\u6b64\u65f6\u8fd9\u4e24\u4e2a\u51fd\u6570\u8fd8\u6ca1\u6709\u6267\u884c),\u7136\u540e\u6253\u5370\u51fa3\uff0c\u6b64\u65f6\u540c\u6b65\u4efb\u52a1\u6267\u884c\u5b8c\u6210\uff0c\u6700\u540e\u6267\u884c\u521a\u521a\u90a3\u4e2a\u5fae\u4efb\u52a1\uff0c\u4ece\u800c\u6267\u884c.then\u4e2d\u6210\u529f\u7684\u65b9\u6cd5\u3002\n\n\n\n", "answer": "", "biz_type": 1, "qid": 498, "subject": "", "title": "Promise \u6784\u9020\u51fd\u6570\u662f\u540c\u6b65\u8fd8\u662f\u5f02\u6b65\u6267\u884c\uff0cthen \u5462"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 11, "qid": 582, "qtype": "short_answer", "short_answer": {"analysis": "#### 1.\u539f\u751fjs\u5b9e\u73b0\n\n\u62d6\u62fd\u9700\u8981\u4e09\u5927\u4e8b\u4ef6\uff1a\n\n- mousedown \u9f20\u6807\u6441\u4e0b\u89e6\u53d1\n- mousemove \u9f20\u6807\u79fb\u52a8\u89e6\u53d1\n- mouseup \u9f20\u6807\u62ac\u8d77\u89e6\u53d1\n\n```html\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>\u624b\u52a8\u5b9e\u73b0\u62d6\u62fd</title>\n</head>\n<style>\n .move {\n position: absolute;\n width: 100px;\n height: 100px;\n background: gray\n }\n</style>\n\n<body>\n <div class=\"move-container\">\n <div class=\"move\">\n </div>\n </div>\n <script>\n let elem = document.querySelector('.move');\n let dragging; //\u62d6\u62fd\u72b6\u6001\n let trans, portrait; //\u9f20\u6807\u6309\u4e0b\u65f6\u76f8\u5bf9\u4e8e\u9009\u4e2d\u5143\u7d20\u7684\u4f4d\u79fb\n\n document.addEventListener('mousedown', function (e) {\n if (e.target == elem) {\n dragging = true; //\u6fc0\u6d3b\u62d6\u62fd\u72b6\u6001\n let elemRect = elem.getBoundingClientRect(); //\u8fd4\u56de\u5143\u7d20\u7684\u5927\u5c0f\u53ca\u5176\u76f8\u5bf9\u4e8e\u89c6\u53e3\u7684\u4f4d\u7f6e\n trans = e.clientX - elemRect.left; //\u9f20\u6807\u6309\u4e0b\u65f6\u548c\u9009\u4e2d\u5143\u7d20\u7684\u5750\u6807\u504f\u79fb:x\u5750\u6807\n portrait = e.clientY - elemRect.top; //\u9f20\u6807\u6309\u4e0b\u65f6\u548c\u9009\u4e2d\u5143\u7d20\u7684\u5750\u6807\u504f\u79fb:y\u5750\u6807\n }\n });\n document.addEventListener('mouseup', function (e) {\n dragging = false;\n });\n document.addEventListener('mousemove', function (e) {\n if (dragging) {\n var moveX = e.clientX - trans,\n moveY = e.clientY - portrait;\n\n elem.style.left = moveX + 'px';\n elem.style.top = moveY + 'px';\n\n }\n });\n </script>\n</body>\n</html>\n```\n\n#### 2.HTML5\u539f\u2f63 \u62d6\u62fddraggable\u5c5e\u6027\u4ee5\u53caDataTranfers\u5bf9\u8c61\n\n![\u6d41\u7a0b\u56fe](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-582-process.png)\n\n```html\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>\u624b\u52a8\u5b9e\u73b0\u62d6\u62fd</title>\n</head>\n<style>\n .main {\n display: flex;\n justify-content: space-around;\n }\n\n .left {\n width: 300px;\n height: 500px;\n margin-right: 10px;\n border: 1px solid red;\n text-align: center;\n box-sizing: border-box;\n padding: 1pxx\n }\n\n .right {\n width: 300px;\n height: 500px;\n border: 1px solid lightseagreen;\n text-align: center;\n box-sizing: border-box;\n padding: 1px;\n }\n\n .txt {\n border: 1px solid gray;\n margin: 1px;\n padding: 5px;\n cursor: move;\n }\n</style>\n\n<body>\n <main class=\"main\">\n <div class=\"left\" id=\"left\">\n <div class=\"txt-show\">\u5de6\u8fb9\u533a\u57df</div>\n <div id='txt1' draggable=\"true\" class=\"dragable txt txt1\">\u53ef\u79fb\u52a8\u7684\u6587\u5b57\u4e00</div>\n <div id='txt2' draggable=\"true\" class=\"dragable txt txt2\">\u53ef\u79fb\u52a8\u7684\u6587\u5b57\u4e8c</div>\n <div id='txt3' draggable=\"true\" class=\"dragable txt txt3\">\u53ef\u79fb\u52a8\u7684\u6587\u5b57\u4e09</div>\n <div id='txt4' draggable=\"true\" class=\"dragable txt txt4\">\u53ef\u79fb\u52a8\u7684\u6587\u5b57\u56db</div>\n <div id='txt5' draggable=\"true\" class=\"dragable txt txt5\">\u53ef\u79fb\u52a8\u7684\u6587\u5b57\u4e94</div>\n </div>\n <div class=\"right\" id='right'>\n <div class=\"txt-show\">\u53f3\u8fb9\u533a\u57df</div>\n </div>\n </main>\n\n <script>\n let txtObj = document.getElementsByClassName('txt')\n for (let i = 0; i < txtObj.length; i++) {\n txtObj[i].ondragstart = handle_start\n txtObj[i].ondrag = handle_drag\n txtObj[i].ondragend = handle_end\n }\n\n function handle_start(e) {\n e.dataTransfer.setData('Text', e.target.id)\n console.log('handle_start-\u62d6\u52a8\u5f00\u59cb')\n }\n\n function handle_drag(e) {\n console.log('handle_drag-\u62d6\u52a8\u4e2d')\n }\n\n function handle_end(e) {\n console.log('handle_end-\u62d6\u52a8\u7ed3\u675f')\n }\n let target = document.getElementById('right')\n target.ondragenter = handle_enter\n target.ondragover = handle_over\n target.ondragleave = handle_leave\n target.ondrop = handle_drop\n\n function handle_enter(e) {\n e.preventDefault()\n console.log('handle_enter-\u8fdb\u5165\u76ee\u7684\u5730')\n }\n\n function handle_over(e) {\n e.preventDefault()\n let returnObj = e.dataTransfer.getData('Text')\n console.log(returnObj + '-handle_over-\u5728\u76ee\u7684\u5730\u8303\u56f4\u5185')\n }\n\n function handle_leave(e) {\n e.preventDefault()\n let returnObj = e.dataTransfer.getData('Text')\n console.log(returnObj)\n console.log('handle_leave-\u6ca1\u6709\u653e\u4e0b\u5c31\u79bb\u5f00\u76ee\u7684\u5730')\n }\n\n function handle_drop(e) {\n e.stopPropagation(); // \u4e0d\u518d\u6d3e\u53d1\u4e8b\u4ef6\u3002\u89e3\u51b3Firefox\u6d4f\u89c8\u5668\uff0c\u6253\u5f00\u65b0\u7a97\u53e3\u7684\u95ee\u9898\u3002\n e.preventDefault()\n let returnObj = e.dataTransfer.getData('Text')\n if (returnObj) {\n e.target.appendChild(document.getElementById(returnObj))\n }\n console.log(returnObj + '-handle_drop-\u5728\u76ee\u7684\u5730\u533a\u91ca\u653e')\n }\n </script>\n</body>\n\n</html>\n```", "answer": "", "biz_type": 1, "qid": 582, "subject": "", "title": "\u8bf7\u624b\u5199\u5b9e\u73b0\u4e00\u4e2a\u62d6\u62fd"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6d77\u5eb7\u5a01\u89c6"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 6, "qid": 525, "qtype": "short_answer", "short_answer": {"analysis": "### \u9879\u76ee\u5982\u4f55\u7ba1\u7406\u6a21\u5757\n\n\u5728\u4e00\u4e2a\u9879\u76ee\u5185\uff0c\u5f53\u6709\u591a\u4e2a\u5f00\u53d1\u8005\u4e00\u8d77\u534f\u4f5c\u5f00\u53d1\u65f6\uff0c\u6216\u8005\u529f\u80fd\u8d8a\u6765\u8d8a\u591a\u3001\u9879\u76ee\u8d8a\u6765\u8d8a\u5e9e\u5927\u65f6\uff0c\u4fdd\u8bc1\u9879\u76ee\u4e95\u7136\u6709\u5e8f\u7684\u8fdb\u884c\u662f\u76f8\u5f53\u91cd\u8981\u7684\u3002\u4e00\u822c\u4f1a\u4ece\u4e0b\u9762\u51e0\u70b9\u6765\u8003\u8bc1\u4e00\u4e2a\u9879\u76ee\u662f\u5426\u7ba1\u7406\u5f97\u5f88\u597d\uff1a\n\n1. **\u53ef\u6269\u5c55\u6027**\uff1a\u80fd\u591f\u5f88\u65b9\u4fbf\u3001\u6e05\u6670\u7684\u6269\u5c55\u4e00\u4e2a\u9875\u9762\u3001\u7ec4\u4ef6\u3001\u6a21\u5757\n2. **\u7ec4\u4ef6\u5316**\uff1a\u591a\u4e2a\u9875\u9762\u4e4b\u95f4\u5171\u7528\u7684\u5927\u5757\u4ee3\u7801\u53ef\u4ee5\u72ec\u7acb\u6210\u7ec4\u4ef6\uff0c\u591a\u4e2a\u9875\u9762\u3001\u7ec4\u4ef6\u4e4b\u95f4\u5171\u7528\u7684\u5c0f\u5757\u4ee3\u7801\u53ef\u4ee5\u72ec\u7acb\u6210\u516c\u5171\u6a21\u5757\n3. **\u53ef\u9605\u8bfb\u6027**\uff1a\u9605\u8bfb\u6027\u826f\u597d\uff08\u5305\u62ec\u76ee\u5f55\u6587\u4ef6\u7ed3\u6784\u3001\u4ee3\u7801\u7ed3\u6784\uff09\uff0c\u80fd\u591f\u5f88\u5feb\u6377\u7684\u627e\u5230\u67d0\u4e2a\u9875\u9762\u3001\u7ec4\u4ef6\u7684\u6587\u4ef6\uff0c\u4e5f\u80fd\u5feb\u6377\u7684\u770b\u51fa\u9879\u76ee\u6709\u54ea\u4e9b\u9875\u9762\u3001\u7ec4\u4ef6\n4. **\u53ef\u79fb\u690d\u6027**\uff1a\u80fd\u591f\u8f7b\u677e\u7684\u5bf9\u9879\u76ee\u67b6\u6784\u8fdb\u884c\u5347\u7ea7\uff0c\u6216\u79fb\u690d\u67d0\u4e9b\u9875\u9762\u3001\u7ec4\u4ef6\u3001\u6a21\u5757\u5230\u5176\u4ed6\u9879\u76ee\n5. **\u53ef\u91cd\u6784\u6027**\uff1a\u5bf9\u67d0\u4e2a\u9875\u9762\u3001\u7ec4\u4ef6\u3001\u6a21\u5757\u8fdb\u884c\u91cd\u6784\u65f6\uff0c\u80fd\u591f\u4fdd\u8bc1\u5728\u91cd\u6784\u4e4b\u540e\u529f\u80fd\u4e0d\u4f1a\u6539\u53d8\u3001\u4e0d\u4f1a\u4ea7\u751f\u65b0 bug\n6. **\u5f00\u53d1\u53cb\u597d**\uff1a\u5f00\u53d1\u8005\u5728\u5f00\u53d1\u67d0\u4e00\u4e2a\u529f\u80fd\u65f6\uff0c\u80fd\u591f\u6709\u6bd4\u8f83\u597d\u7684\u4f53\u9a8c\uff08\u4e0d\u597d\u7684\u4f53\u9a8c\u6bd4\u5982\uff1a\u591a\u4e2a\u6587\u4ef6\u76f8\u9694\u5f88\u8fdc\uff09\n7. **\u534f\u4f5c\u6027**\uff1a\u591a\u4eba\u534f\u4f5c\u65f6\uff0c\u5f88\u5c11\u4ea7\u751f\u4ee3\u7801\u51b2\u7a81\u3001\u6587\u4ef6\u8986\u76d6\u7b49\u95ee\u9898\n8. **\u53ef\u4ea4\u63a5\u6027**\uff1a\u5f53\u6709\u4eba\u8981\u79bb\u5f00\u9879\u76ee\u65f6\uff0c\u4ea4\u63a5\u7ed9\u5176\u4ed6\u4eba\u662f\u5f88\u65b9\u4fbf\u7684\n\n\u591a\u4e2a\u9879\u76ee\u4e4b\u95f4\uff0c\u5982\u4f55\u7ba1\u7406\u597d\u9879\u76ee\u4e4b\u95f4\u8054\u7cfb\uff0c\u6bd4\u5982\u5171\u7528\u7ec4\u4ef6\u3001\u516c\u5171\u6a21\u5757\u7b49\uff0c\u4fdd\u8bc1\u5feb\u6377\u9ad8\u6548\u5f00\u53d1\u3001\u4e0d\u91cd\u590d\u9020\u8f6e\u5b50\uff0c\u4e5f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e00\u822c\u4f1a\u4ece\u4e0b\u9762\u51e0\u70b9\u6765\u8003\u8bc1\u591a\u4e2a\u9879\u76ee\u4e4b\u95f4\u662f\u5426\u7ba1\u7406\u5f97\u5f88\u597d\uff1a\n\n1. **\u7ec4\u4ef6\u5316**\uff1a\u591a\u4e2a\u9879\u76ee\u5171\u7528\u7684\u4ee3\u7801\u5e94\u5f53\u72ec\u7acb\u51fa\u6765\uff0c\u6210\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u7ec4\u4ef6\u9879\u76ee\n2. **\u7248\u672c\u5316**\uff1a\u7ec4\u4ef6\u9879\u76ee\u4e0e\u5e94\u7528\u9879\u76ee\u90fd\u5e94\u5f53\u7248\u672c\u5316\u7ba1\u7406\uff0c\u7279\u522b\u662f\u7ec4\u4ef6\u9879\u76ee\u7684\u7248\u672c\u5e94\u5f53\u7b26\u5408 semver \u8bed\u4e49\u5316\u7248\u672c\u89c4\u8303\n3. **\u7edf\u4e00\u6027**\uff1a\u591a\u4e2a\u9879\u76ee\u4e4b\u95f4\u5e94\u5f53\u4f7f\u7528\u76f8\u540c\u7684\u6280\u672f\u9009\u578b\u3001UI \u6846\u67b6\u3001\u811a\u624b\u67b6\u3001\u5f00\u53d1\u5de5\u5177\u3001\u6784\u5efa\u5de5\u5177\u3001\u6d4b\u8bd5\u5e93\u3001\u76ee\u5f55\u89c4\u8303\u3001\u4ee3\u7801\u89c4\u8303\u7b49\uff0c\u76f8\u540c\u529f\u80fd\u5e94\u6307\u5b9a\u4f7f\u7528\u56fa\u5b9a\u67d0\u4e00\u4e2a\u5e93\n4. **\u6587\u6863\u5316**\uff1a\u7ec4\u4ef6\u9879\u76ee\u4e00\u5b9a\u9700\u8981\u76f8\u5173\u7684\u6587\u6863\uff0c\u5e94\u7528\u9879\u76ee\u5728\u5fc5\u8981\u7684\u65f6\u5019\u4e5f\u8981\u5f62\u6210\u76f8\u5e94\u7684\u6587\u6863", "answer": "", "biz_type": 1, "qid": 525, "subject": "", "title": "\u9879\u76ee\u5982\u4f55\u7ba1\u7406\u6a21\u5757"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 6, "qid": 638, "qtype": "short_answer", "short_answer": {"analysis": "### \u9875\u9762\u57cb\u70b9\u65b9\u6848\n\n#### 1\uff09\u73b0\u6709\u7684\u57cb\u70b9\u7c7b\u578b\uff1a\n\n1. **\u624b\u52a8\u4ee3\u7801\u57cb\u70b9**\uff1a\u5728\u9700\u8981\u91c7\u96c6\u6570\u636e\u7684\u5730\u65b9\u8c03\u7528\u57cb\u70b9\u7684\u65b9\u6cd5\u3002\u5728\u4efb\u610f\u5730\u70b9\u4efb\u610f\u573a\u666f\u8fdb\u884c\u6570\u636e\u91c7\u96c6\uff0c\n2. **\u53ef\u89c6\u5316\u57cb\u70b9**\uff1a\u5143\u7d20\u90fd\u5e26\u6709\u552f\u4e00\u6807\u8bc6\u3002\u901a\u8fc7\u57cb\u70b9\u914d\u7f6e\u540e\u53f0\uff0c\u5c06\u5143\u7d20\u4e0e\u8981\u91c7\u96c6\u4e8b\u4ef6\u5173\u8054\u8d77\u6765\uff0c\u53ef\u4ee5\u81ea\u52a8\u751f\u6210\u57cb\u70b9\u4ee3\u7801\u5d4c\u5165\u5230\u9875\u9762\u4e2d\u3002\n3. **\u65e0\u57cb\u70b9**\uff1a\u524d\u7aef\u81ea\u52a8\u91c7\u96c6\u5168\u90e8\u4e8b\u4ef6\uff0c\u4e0a\u62a5\u57cb\u70b9\u6570\u636e\uff0c\u7531\u540e\u7aef\u6765\u8fc7\u6ee4\u548c\u8ba1\u7b97\u51fa\u6709\u7528\u7684\u6570\u636e\uff0c\n\n#### 2\uff09\u57fa\u672c\u5b9e\u73b0\u65b9\u6848\uff1a\n\n\n1. \u7ea6\u5b9a\u901a\u7528\u7684\u57cb\u70b9\u91c7\u96c6\u63a5\u53e3\u89c4\u8303: \u5982header(\u6807\u8bc6X-Device-Id, X-Source-Url, X-Current-Url, X-User-Id\u7b49\u4fe1\u606f), body(\u6807\u8bc6PageSessionID, Event, PageTitle, CurrentTime, ExtraInfo);\n2. \u6307\u5b9a\u8c03\u7528\u91c7\u96c6\u811a\u672c\u7684\u65b9\u5f0f: \u5355\u9875\u9762\u5e94\u7528 => \u5bf9history\u8def\u5f84\u7684\u53d8\u5316\u4fdd\u6301\u76d1\u542c, \u8def\u5f84\u53d8\u5316\u65f6\u89e6\u53d1\u57cb\u70b9\u6536\u96c6; \u9875\u9762\u52a0\u8f7d/\u79bb\u5f00\u7ed1\u5b9a\u5bf9\u5e94\u7684onload, unload\u4e8b\u4ef6, \u9875\u9762\u5143\u7d20\u4e0a\u7ed1\u5b9a\u76f8\u5173\u7684\u4ea4\u4e92\u4e8b\u4ef6(click, event\u7b49)\n\n\n\n#### 3\uff09\u793a\u610f\u4f2a\u4ee3\u7801\uff1a \n\n```js\nvar collect = {\n deviceUrl:'',\n eventUrl:'',\n isuploadUrl:'',\n parmas:{},\n device:{}\n};\n\n//\u83b7\u53d6\u57cb\u70b9\u914d\u7f6e\ncollect.setParames = function(){}\n\n//\u66f4\u65b0\u8bbf\u95ee\u8def\u5f84\u53ca\u9875\u9762\u4fe1\u606f\ncollect.updatePageInfo = function(){}\n\n//\u83b7\u53d6\u4e8b\u4ef6\u53c2\u6570\ncollect.getParames = function(){}\n\n//\u83b7\u53d6\u8bbe\u5907\u4fe1\u606f\ncollect.getDevice = function(){}\n\n//\u4e8b\u4ef6\u91c7\u96c6\ncollect.send = function(){}\n\n//\u8bbe\u5907\u91c7\u96c6\ncollect.sendDevice = function(){}\n\n//\u5224\u65ad\u662f\u5426\u91c7\u96c6\uff0c\u57cb\u70b9\u91c7\u96c6\u7684\u5f00\u5173\ncollect.isupload = function(){\n/*\n1. \u5224\u65ad\u662f\u5426\u91c7\u96c6\uff0c\u4e0d\u91c7\u96c6\u5c31\u6ce8\u9500\u4e8b\u4ef6\u76d1\u542c\uff08\u9879\u76ee\u4e2d\u533a\u5206\u6e38\u5ba2\u8eab\u4efd\u548c\u7528\u6237\u8eab\u4efd\u7684\u91c7\u96c6\u60c5\u51b5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u88ab\u5224\u65ad\u4e24\u6b21\uff09\n2. \u91c7\u96c6\u5219\u5224\u65ad\u662f\u5426\u5df2\u7ecf\u91c7\u96c6\u8fc7\n a.\u5df2\u7ecf\u91c7\u96c6\u8fc7\u4e0d\u505a\u4efb\u4f55\u64cd\u4f5c\n b.\u6ca1\u6709\u91c7\u96c6\u8fc7\u6dfb\u52a0\u4e8b\u4ef6\u76d1\u542c\n3. \u5224\u65ad\u662f \u6df7\u5408\u5e94\u7528\u8fd8\u662f\u7eaf web \u5e94\u7528\n a.\u5982\u679c\u662fweb \u5e94\u7528\uff0c\u8c03\u7528 collect.setIframe \u8bbe\u7f6e iframe\n b.\u5982\u679c\u662f\u6df7\u5408\u5e94\u7528 \u5c06\u5f00\u59cb\u52a0\u8f7d\u548c\u52a0\u8f7d\u5b8c\u6210\u4e8b\u4ef6\u4f20\u8f93\u7ed9 app\n*/\n\n}\n\n//\u70b9\u51fb\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\ncollect.clickHandler = function(){}\n\n//\u79bb\u5f00\u9875\u9762\u7684\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\ncollect.beforeUnloadHandler = function(){}\n\n//\u9875\u9762\u56de\u9000\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\ncollect.onPopStateHandler = function(){}\n\n//\u7cfb\u7edf\u4e8b\u4ef6\u521d\u59cb\u5316\uff0c\u6ce8\u518c\u79bb\u5f00\u4e8b\u4ef6\uff0c\u6d4f\u89c8\u5668\u540e\u9000\u4e8b\u4ef6\ncollect.event = function(){}\n\n//\u83b7\u53d6\u8bb0\u5f55\u5f00\u59cb\u52a0\u8f7d\u6570\u636e\u4fe1\u606f\ncollect.getBeforeload = function(){}\n\n//\u5b58\u50a8\u52a0\u8f7d\u5b8c\u6210\uff0c\u83b7\u53d6\u8bbe\u5907\u7c7b\u578b\uff0c\u8bb0\u5f55\u52a0\u8f7d\u5b8c\u6210\u4fe1\u606f\ncollect.onload = function(){\n/*\n 1. \u5224\u65adcookie\u662f\u5426\u6709\u5b58\u8bbe\u5907\u7c7b\u578b\u4fe1\u606f\uff0c\u6709\u8868\u793a\u6df7\u5408\u5e94\u7528\n 2. \u91c7\u96c6\u52a0\u8f7d\u5b8c\u6210\u65f6\u95f4\u7b49\u4fe1\u606f\n 3. \u8c03\u7528 collect.isupload \u5224\u65ad\u662f\u5426\u8fdb\u884c\u91c7\u96c6\n*/\n}\n\n//web \u5e94\u7528\uff0c\u901a\u8fc7\u5d4c\u5165 iframe \u8fdb\u884c\u8de8\u57df cookie \u901a\u8baf\uff0c\u8bbe\u7f6e\u8bbe\u5907id\ncollect.setIframe = function(){}\n\n//app \u4e0e h5 \u6df7\u5408\u5e94\u7528\uff0c\u76f4\u63a5\u5c06\u6570\u4fe1\u606f\u53d1\u7ed9 app,\u5224\u65ad\u8bbe\u5907\u7c7b\u578b\u505a\u539f\u751f\u65b9\u6cd5\u9002\u914d\u5668\ncollect.saveEvent = function(){}\n\n//\u91c7\u96c6\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u7c7b\u578b\ncollect.dispatch = function(){}\n\n//\u5c06\u53c2\u6570 userId \u5b58\u5165sessionStorage\ncollect.storeUserId = function(){}\n\n//\u91c7\u96c6H5\u4fe1\u606f,\u5982\u679c\u662f\u6df7\u5408\u5e94\u7528\uff0c\u5c06\u91c7\u96c6\u5230\u7684\u4fe1\u606f\u53d1\u9001\u7ed9 app \u7aef\ncollect.saveEventInfo = function(){}\n\n//\u9875\u9762\u521d\u59cb\u5316\u8c03\u7528\u65b9\u6cd5\ncollect.init = function(){\n/*\n 1. \u83b7\u53d6\u5f00\u59cb\u52a0\u8f7d\u7684\u91c7\u96c6\u4fe1\u606f\n 2. \u83b7\u53d6 SDK \u914d\u7f6e\u4fe1\u606f\uff0c\u8bbe\u5907\u4fe1\u606f\n 3. \u6539\u5199 history \u4e24\u4e2a\u65b9\u6cd5\uff0c\u5355\u9875\u9762\u5e94\u7528\u9875\u9762\u8df3\u8f6c\u524d\u8c03\u7528\u6211\u4eec\u81ea\u5df1\u7684\u65b9\u6cd5\n 4. \u9875\u9762\u52a0\u8f7d\u5b8c\u6210\uff0c\u8c03\u7528 collect.onload \u65b9\u6cd5\n*/\n\n}\n\n\ncollect.init(); // \u521d\u59cb\u5316\n\n//\u66b4\u9732\u7ed9\u4e1a\u52a1\u65b9\u8c03\u7528\u7684\u65b9\u6cd5\nreturn {\n dispatch:collect.dispatch,\n storeUserId:collect.storeUserId,\n}\n```", "answer": "", "biz_type": 1, "qid": 638, "subject": "", "title": "\u9875\u9762\u57cb\u70b9\u600e\u4e48\u5b9e\u73b0"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fae\u533b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 6, "qid": 495, "qtype": "short_answer", "short_answer": {"analysis": "### \u89e3\u6790\n\n\u6570\u7ec4\u53ef\u4ee5\u76f4\u63a5\u6839\u636e\u7d22\u5f15\u53d6\u7684\u5bf9\u5e94\u7684\u5143\u7d20\uff0c\u6240\u4ee5\u4e0d\u7ba1\u53d6\u54ea\u4e2a\u4f4d\u7f6e\u7684\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f O(1)\n\nJavaScript \u6ca1\u6709\u771f\u6b63\u610f\u4e49\u4e0a\u7684\u6570\u7ec4\uff0c\u6240\u6709\u7684\u6570\u7ec4\u5176\u5b9e\u662f\u5bf9\u8c61\uff0c\u5176\u201c\u7d22\u5f15\u201d\u770b\u8d77\u6765\u662f\u6570\u5b57\uff0c\u5176\u5b9e\u4f1a\u88ab\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\uff0c\u4f5c\u4e3a\u5c5e\u6027\u540d\uff08\u5bf9\u8c61\u7684 key\uff09\u6765\u4f7f\u7528\u3002\u6240\u4ee5\u65e0\u8bba\u662f\u53d6\u7b2c 1 \u4e2a\u8fd8\u662f\u53d6\u7b2c 10 \u4e07\u4e2a\u5143\u7d20\uff0c\u90fd\u662f\u7528 key \u7cbe\u786e\u67e5\u627e\u54c8\u5e0c\u8868\u7684\u8fc7\u7a0b\uff0c\u5176\u6d88\u8017\u65f6\u95f4\u5927\u81f4\u76f8\u540c\u3002\n\n\u770b\u4e00\u4e0bchrome\u63a7\u5236\u53f0\u4e0b\u7684\u7ed3\u679c\n\n```js\nvar arr = new Array(100000).fill(null)\nconsole.time('arr1')\narr[0]\nconsole.timeEnd('arr1')\n// arr1: 0.003173828125ms\n```\n```js\nvar arr = new Array(100000).fill(null)\nconsole.time('arr100000')\narr[99999]\nconsole.timeEnd('arr100000')\n// arr100000: 0.002685546875ms\n```", "answer": "", "biz_type": 1, "qid": 495, "subject": "", "title": "\u6570\u7ec4\u91cc\u9762\u6709 10 \u4e07\u4e2a\u6570\u636e\uff0c\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u548c\u7b2c 10 \u4e07\u4e2a\u5143\u7d20\u7684\u65f6\u95f4\u76f8\u5dee\u591a\u5c11"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 7, "qid": 665, "qtype": "short_answer", "short_answer": {"analysis": "### \u7f51\u7ad9\u6027\u80fd\u63d0\u5347\n\n#### 1\u3001\u9759\u6001\u8d44\u6e90\u7684\u4f18\u5316\n \n\u4e3b\u8981\u662f\u51cf\u5c11\u9759\u6001\u8d44\u6e90\u7684\u52a0\u8f7d\u65f6\u95f4\uff0c\u4e3b\u8981\u5305\u62echtml\u3001js\u3001css\u548c\u56fe\u7247\u3002\n\n- a\u3001\u51cf\u5c11http\u8bf7\u6c42\u6570\uff1a\u5408\u5e76js\u3001css\u3001\u5236\u4f5c\u96ea\u78a7\u56fe\u4ee5\u53ca\u4f7f\u7528http\u7f13\u5b58\uff1b\n- b\u3001\u51cf\u5c0f\u8d44\u6e90\u7684\u5927\u5c0f\uff1a\u538b\u7f29\u6587\u4ef6\u3001\u538b\u7f29\u56fe\u7247\uff0c\u5c0f\u56fe\u4f7f\u7528base64\u7f16\u7801\u7b49\uff1b\n- c\u3001\u5f02\u6b65\u7ec4\u4ef6\u548c\u56fe\u7247\u61d2\u52a0\u8f7d\uff1b\n- d\u3001CDN\u52a0\u901f\u548c\u7f13\u5b58\uff08bootCND\uff09\uff1a\u5ba2\u6237\u7aef\u53ef\u901a\u8fc7\u6700\u4f73\u7684\u7f51\u7edc\u94fe\u8def\u52a0\u8f7d\u9759\u6001\u8d44\u6e90\uff0c\u63d0\u9ad8\u8bbf\u95ee\u7684\u901f\u5ea6\u548c\u6210\u529f\u7387\u3002\uff08CDN\uff1a\u901a\u8fc7\u5728\u7f51\u7edc\u5404\u5904\u653e\u7f6e\u8282\u70b9\u670d\u52a1\u5668\u6784\u6210\u7684\u4e00\u5c42\u667a\u80fd\u865a\u62df\u7f51\u7edc\uff0c\u53ef\u5c06\u7528\u6237\u7684\u8bf7\u6c42\u91cd\u65b0\u5bfc\u5411\u79bb\u7528\u6237\u6700\u8fd1\u7684\u670d\u52a1\u8282\u70b9\u4e0a\uff09\n \n \n \n#### 2\u3001\u63a5\u53e3\u8bbf\u95ee\u7684\u4f18\u5316\n \n1. http\u6301\u4e45\u94fe\u63a5\uff08Conection:keep-alive\uff09\n2. \u540e\u7aef\u4f18\u5316\u5408\u5e76\u8bf7\u6c42\uff08\u6bd4\u5982\u5728\u8fdb\u5165\u4e00\u4e2a\u5546\u54c1\u8be6\u60c5\u9875\u7684\u65f6\u5019\u540e\u7aef\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u63a5\u53e3\u83b7\u53d6\u5546\u54c1\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u7136\u540e\u5f53\u7528\u6237\u70b9\u51fb\u5047\u5982\u8d2d\u7269\u8f66\u65f6\uff09\n3. \u51b7\u6570\u636e\u63a5\u53e3\u7f13\u5b58\u5230localstorage\uff0c\u51cf\u5c11\u8bf7\u6c42\n \n \n \n#### 3\u3001\u9875\u9762\u6e32\u67d3\u901f\u5ea6\u7684\u4f18\u5316\n \n1. \u7531\u4e8e\u6d4f\u89c8\u5668\u7684js\u5f15\u64ce\u7ebf\u7a0b\u548cGUI\u6e32\u67d3\u7ebf\u7a0b\u662f\u4e92\u65a5\u7684\uff0c\u6240\u4ee5\u5728\u6267\u884cjs\u7684\u65f6\u5019\u4f1a\u963b\u585e\u5b83\u7684\u6e32\u67d3\uff0c\u6240\u4ee5\u4e00\u822c\u4f1a\u5c06css\u653e\u5728\u9876\u90e8\uff0c\u4f18\u5148\u6e32\u67d3\uff0cjs\u653e\u5728\u5e95\u90e8\uff1b\n2. \u51cf\u5c11dom\u7684\u64cd\u4f5c\uff1a\n3. \u4f7f\u7528\u865a\u62dfDOM\u6e32\u67d3\u65b9\u6848\uff0c\u505a\u5230\u6700\u5c0f\u5316\u64cd\u4f5c\u771f\u5b9e\u7684dom\uff1b\n4. \u4e8b\u4ef6\u4ee3\u7406\uff1a\u5229\u7528\u4e8b\u4ef6\u5192\u6ce1\u539f\u7406\uff0c\u628a\u51fd\u6570\u6ce8\u518c\u5230\u7236\u7ea7\u5143\u7d20\u4e0a\u3002\n5. \u51cf\u5c11\u9875\u9762\u7684\u91cd\u7ed8\u548c\u56de\u6d41\u3002", "answer": "", "biz_type": 1, "qid": 665, "subject": "", "title": "\u5982\u4f55\u52a0\u5feb\u9875\u9762\u6e32\u67d3\u901f\u5ea6\uff0c\u90fd\u6709\u54ea\u4e9b\u65b9\u5f0f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9177\u5bb6\u4e50"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 5, "qid": 534, "qtype": "short_answer", "short_answer": {"analysis": "### \u7b80\u8981\u56de\u7b54\n\n\u9996\u5148\uff0c\u6211\u4eec\u8981\u77e5\u9053 setInterval \u7684\u8fd0\u884c\u673a\u5236\uff0csetInterval \u5c5e\u4e8e\u5b8f\u4efb\u52a1\uff0c\u8981\u7b49\u5230\u4e00\u8f6e\u540c\u6b65\u4ee3\u7801\u4ee5\u53ca\u5fae\u4efb\u52a1\u6267\u884c\u5b8c\u540e\u624d\u4f1a\u8d70\u5230\u5b8f\u4efb\u52a1\u961f\u5217\uff0c\u4f46\u662f\u524d\u9762\u7684\u4efb\u52a1\u5230\u5e95\u9700\u8981\u591a\u957f\u65f6\u95f4\uff0c\u8fd9\u4e2a\u6211\u4eec\u662f\u4e0d\u786e\u5b9a\u7684\n\n\u7b49\u5230\u5b8f\u4efb\u52a1\u6267\u884c\uff0c\u4ee3\u7801\u4f1a\u68c0\u67e5 setInterval \u662f\u5426\u5230\u4e86\u6307\u5b9a\u65f6\u95f4\uff0c\u5982\u679c\u5230\u4e86\uff0c\u5c31\u4f1a\u6267\u884c setInterval\uff0c\u5982\u679c\u4e0d\u5230\uff0c\u90a3\u5c31\u8981\u7b49\u5230\u4e0b\u6b21 EventLoop \u91cd\u65b0\u5224\u65ad\n\n\u5f53\u7136\uff0c\u8fd8\u6709\u4e00\u90e8\u5206\u4e0d\u786e\u5b9a\u7684\u56e0\u7d20\uff0c\u6bd4\u5982 setInterval \u7684\u65f6\u95f4\u6233\u5c0f\u4e8e 10ms\uff0c\u90a3\u4e48\u4f1a\u88ab\u8c03\u6574\u81f3 10ms \u6267\u884c\uff0c\u56e0\u4e3a\u8fd9\u662f setInterval \u8bbe\u8ba1\u53ca\u89c4\u5b9a\uff0c\u5f53\u7136\uff0c\u7531\u4e8e\u5176\u4ed6\u4efb\u52a1\u7684\u5f71\u54cd\uff0c\u8fd9\u4e2a 10ms \u4e5f\u4f1a\u4e0d\u7cbe\u786e\n\n\u8fd8\u6709\u4e00\u4e9b\u7269\u7406\u539f\u56e0\uff0c\u5982\u679c\u7528\u6237\u4f7f\u7528\u7684\u8bbe\u5907\u5904\u4e8e\u4f9b\u7535\u72b6\u6001\u7b49\uff0c\u4e3a\u4e86\u8282\u7535\uff0c\u6d4f\u89c8\u5668\u4f1a\u4f7f\u7528\u7cfb\u7edf\u5b9a\u65f6\u5668\uff0c\u65f6\u95f4\u95f4\u9694\u5c06\u4f1a\u88ab\u8c03\u6574\u81f3 16.6ms\n\n### \u6df1\u5165\u63a2\u7a76\u7248\n\n#### 1.\u8d85\u65f6\u9650\u5236\u4e3a>=4ms\n\n\u5728\u73b0\u4ee3\u6d4f\u89c8\u5668\u4e2d\uff0c\u7531\u4e8e\u56de\u8c03\u5d4c\u5957\uff08\u5d4c\u5957\u7ea7\u522b\u81f3\u5c11\u4e3a\u7279\u5b9a\u6df1\u5ea6\uff09\u6216\u8005\u7ecf\u8fc7\u4e00\u5b9a\u6570\u91cf\u7684\u8fde\u7eed\u95f4\u9694\u800c\u89e6\u53d1\u8fde\u7eed\u8c03\u7528\u65f6\uff0c`setTimeout`/`setInterval`\u8c03\u7528\u81f3\u5c11\u6bcf**4ms**\u88ab\u9650\u5236\u4e00\u6b21\n\n```js\nfunction f(){}\nfunction cb(){\n f()\n setTimeout(cb,0)\n}\nsetTimeout(cb,0)\n```\n\n- \u5728Chrome\u548cFirefox \u7b2c\u4e94\u6b21\u8fde\u7eed\u7684\u8c03\u7528\u5c31\u4f1a\u88ab\u9650\u5236\n- Safari\u9501\u5b9a\u4e86\u7b2c\u516d\u6b21\u901a\u8bdd\n- Edge\u5728\u7b2c\u4e09\u6b21\n- Gecko\u5728`version56`\u5df2\u7ecf\u8fd9\u6837\u5f00\u59cb\u5c1d\u8bd5`setInterval`(\u5bf9setTimeout\u4e5f\u4e00\u6837) \u3002`In Chrome and Firefox, the 5th successive callback call is clamped; Safari clamps on the 6th call; in Edge its the 3rd one. Gecko started to treat setInterval() like this in version 56 (it already did this with setTimeout(); see below). `\n\n**\u4ece\u5386\u53f2\u4e0a\u6765\u770b\uff0c\u67d0\u4e9b\u6d4f\u89c8\u5668\u5728\u6267\u884c\u6b64\u8282\u6d41\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\u4e86\uff0c\u5728`setInterval`\u4ece\u4efb\u4f55\u5730\u65b9\u7684\u8c03\u7528\u4e0a\uff0c\u6216\u8005\u5728`setTimeout`\u5d4c\u5957\u7ea7\u522b\u81f3\u5c11\u8fbe\u5230\u4e00\u5b9a\u6df1\u5ea6\u7684\u60c5\u51b5\u4e0b\u8c03\u7528\u5d4c\u5957\u65f6\uff0c\u8981\u60f3\u5728\u73b0\u4ee3\u6d4f\u89c8\u5668\u5b9e\u73b00\u6beb\u79d2\u5ef6\u8fdf\u53ef\u4ee5\u4f7f\u7528`postMessage`**\n\n> \u6ce8\u610f\uff1a\u6700\u5c0f\u5ef6\u8fdf`DOM_MIN_TIMEOUT_VALUE`\u4e3a4ms\uff0c\u540c\u65f6`DOM_CLAMP_TIMEOUT_NESTING_LEVEL`\u662f5\uff08dom\u56fa\u5b9a\u8d85\u65f6\u5d4c\u5957\u7ea7\u522b\uff09\n\n#### 2.\u5728\u975e\u6d3b\u52a8tab\u5361\uff0c\u8d85\u65f6\u9650\u5236\u4e3a>=1000ms\n\n\u4e3a\u4e86\u51cf\u5c11\u80cc\u666f\u9009\u9879\u5361\u7684\u8d1f\u8f7d\uff08\u548c\u76f8\u5173\u7684\u8d44\u6e90\u4f7f\u7528\uff09\uff0c\u5728\u4e0d\u6d3b\u52a8\u7684\u8d44\u6e90\u5361\u5c06\u8d85\u65f6\u9650\u5236\u4e3a1000ms\u4ee5\u4e0b\n\nfirefox\u4ece\u7248\u672c5\u5f00\u59cb\u5b9e\u65bd\u8be5\u884c\u4e3a\uff08\u53ef\u901a\u8fc7`dom.min_background_timeout_value`\u9996\u9009\u9879\u8c03\u65741000ms\u5e38\u91cf\uff09\u3002Chrome\u4ece\u7248\u672c11\u5f00\u59cb\u5b9e\u73b0\u8be5\u884c\u4e3a\uff0c\u81eaFirefox 14\u4e2d\u51fa\u73b0\u9519\u8bef736602\u4ee5\u6765\uff0cAndroid\u7248Firefox\u7684\u80cc\u666f\u6807\u7b7e\u4f7f\u7528\u7684\u8d85\u65f6\u503c\u4e3a15\u5206\u949f\uff0c\u5e76\u4e14\u80cc\u666f\u6807\u7b7e\u4e5f\u53ef\u4ee5\u5b8c\u5168\u5378\u8f7d\n\n#### 3.\u9650\u5236\u8ddf\u8e2a\u8d85\u65f6\u811a\u672c\n\n\u81eaFirefox 55\u8d77\uff0c\u8ddf\u8e2a\u811a\u672c\uff08\u4f8b\u5982Google Analytics\uff08\u5206\u6790\uff09\uff0cFirefox\u901a\u8fc7\u5176TP\u5217\u8868\u5c06\u5176\u8bc6\u522b\u4e3a\u8ddf\u8e2a\u811a\u672c\u7684\u4efb\u4f55\u811a\u672cURL \uff09\u90fd\u53d7\u5230\u4e86\u8fdb\u4e00\u6b65\u7684\u9650\u5236\u3002\u5728\u524d\u53f0\u8fd0\u884c\u65f6\uff0c\u8282\u6d41\u6700\u5c0f\u5ef6\u8fdf\u4ecd\u4e3a4ms\u3002\u4f46\u662f\uff0c\u5728\u540e\u53f0\u9009\u9879\u5361\u4e2d\uff0c\u9650\u5236\u6700\u5c0f\u5ef6\u8fdf\u4e3a10,000\u6beb\u79d2\uff08\u537310\u79d2\uff09\uff0c\u8be5\u5ef6\u8fdf\u5728\u9996\u6b21\u52a0\u8f7d\u6587\u6863\u540e30\u79d2\u751f\u6548\u3002\n\n\u63a7\u5236\u6b64\u884c\u4e3a\u7684\u9996\u9009\u9879\u662f\uff1a\n\n- dom.min_tracking_timeout_value\uff1a4\n- dom.min_tracking_background_timeout_value\uff1a10000\n- dom.timeout.tracking_throttling_delay\uff1a30000\n\n#### 4.\u903e\u671f\u8d85\u65f6\n\n\u9664\u4e86\u56fa\u5b9a\u503c\u610f\u5916\uff0c\u5f53\u9875\u9762\uff08\u6216OS /\u6d4f\u89c8\u5668\u672c\u8eab\uff09\u5fd9\u4e8e\u5176\u4ed6\u4efb\u52a1\u65f6\uff0c\u8d85\u65f6\u8fd8\u4f1a\u5728\u4ee5\u540e\u89e6\u53d1\u3002\u8981\u6ce8\u610f\u7684\u4e00\u4e2a\u91cd\u8981\u60c5\u51b5\u662f\uff0c\u76f4\u5230\u8c03\u7528\u7684\u7ebf\u7a0bsetTimeout()\u7ec8\u6b62\uff0c\u51fd\u6570\u6216\u4ee3\u7801\u6bb5\u624d\u80fd\u6267\u884c\u3002\u4f8b\u5982\uff1a\n\n```js\nfunction foo() {\n console.log('foo has been called');\n}\nsetTimeout(foo, 0);\nconsole.log('After setTimeout');\n// After setTimeout foo has been called\n```\n\n\u8fd9\u662f\u56e0\u4e3a\u5373\u4f7fsetTimeout\u4ee5\u96f6\u7684\u5ef6\u8fdf\u88ab\u8c03\u7528\uff0c\u5b83\u4e5f\u88ab\u653e\u7f6e\u5728\u961f\u5217\u4e2d\u5e76\u8ba1\u5212\u5728\u4e0b\u4e00\u4e2a\u673a\u4f1a\u8fd0\u884c\u3002\u4e0d\u662f\u7acb\u5373\u3002\u5f53\u524d\u6267\u884c\u7684\u4ee3\u7801\u5fc5\u987b\u5728\u6267\u884c\u961f\u5217\u4e2d\u7684\u529f\u80fd\u4e4b\u524d\u5b8c\u6210\uff0c\u56e0\u6b64\u751f\u6210\u7684\u6267\u884c\u987a\u5e8f\u53ef\u80fd\u4e0e\u9884\u671f\u7684\u4e0d\u540c", "answer": "", "biz_type": 1, "qid": 534, "subject": "", "title": "\u5b9a\u65f6\u5668\u4e3a\u4ec0\u4e48\u662f\u4e0d\u7cbe\u786e\u7684"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 5, "qid": 634, "qtype": "short_answer", "short_answer": {"analysis": "### \u7b80\u5355\u6a21\u62df\u5b9e\u73b0\n\n```js\nFunction.prototype.myapply = function (context, ...argus) {\n if (typeof this !== 'function') {\n throw new TypeError('not funciton')\n }\n const fn = this\n let result = null\n\n context = context || window\n argus = argus && argus[0] || []\n context.fn = fn\n result = context.fn(...argus)\n delete context.fn\n\n return result\n}\n```", "answer": "", "biz_type": 1, "qid": 634, "subject": "", "title": "\u624b\u5199\u5b9e\u73b0 apply"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fae\u533b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 501, "qtype": "short_answer", "short_answer": {"analysis": "### \u6e90\u7801\u5206\u6790\n\n```js\n// cloneDeep.js\nfunction cloneDeep(value) {\n return baseClone(value, CLONE_DEEP_FLAG | CLONE_SYMBOLS_FLAG);\n}\nexport default cloneDeep;\n```\n\n```\nfunction cloneDeepWith(value, customizer) {\n customizer = typeof customizer == 'function' ? customizer : undefined;\n return baseClone(value, CLONE_DEEP_FLAG | CLONE_SYMBOLS_FLAG, customizer);\n}\n\nexport default cloneDeepWith;\n```\n\n\u5148\u770b lodash \u6df1\u62f7\u8d1d\u7684\u4e24\u4e2a\u4e3b\u8981\u65b9\u6cd5\uff0ccloneDeep\uff0c\u53c2\u6570\u53ea\u6709\u4e00\u4e2a value\uff0c\u5c31\u662f\u6211\u4eec\u8981\u62f7\u8d1d\u7684\u5bf9\u8c61\n\ncloneDeepWith\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u53c2\u6570\u6bd4 cloneDeep \u591a\u4e86\u4e00\u4e2a customizer\uff0c\u7ffb\u8bd1\u8fc7\u6765\u5c31\u662f\u5b9a\u5236\u7684\u610f\u601d\uff0c\u90a3\u4e5f\u5c31\u53ef\u4ee5\u77e5\u9053\uff0ccustomizer \u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u5728\u8fd9\u4e2a\u51fd\u6570\u91cc\u53ef\u4ee5\u81ea\u5b9a\u4e49\u4e00\u4e9b\u4e1c\u897f\n\n\u4ece\u4e0a\u9762\u7684\u6df1\u62f7\u8d1d\u7684\u65b9\u6cd5\u91cc\u53ef\u4ee5\u770b\u5230\uff0c\u90fd\u6267\u884c\u4e86 baseClone \u65b9\u6cd5\uff0cbaseClone \u5c31\u662f\u5b9e\u73b0\u6df1\u62f7\u8d1d\u7684\u4e3b\u8981\u65b9\u6cd5\n\n\u63a5\u4e0b\u6765\u770b\u4e0b baseClone \u8fd9\u4e2a\u65b9\u6cd5\u505a\u4e86\u4ec0\u4e48\n\n```js\nfunction baseClone(value, bitmask, customizer, key, object, stack) {\n // \u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u770b\u5230\u4e00\u8fdb\u6765\u5c31\u5b9a\u4e49\u4e86\u4e09\u4e2a\u5e38\u91cf\uff0c\u901a\u8fc7\u4f4d\u8fd0\u7b97\uff0c\u53ef\u4ee5\u5f97\u5230\u4e0b\u9762\u4e09\u4e2a\u503c isDeep isFlat isFull\n var result,\n isDeep = bitmask & CLONE_DEEP_FLAG, //\u6df1\u62f7\u8d1d 1\n isFlat = bitmask & CLONE_FLAT_FLAG, //\u62f7\u8d1d\u539f\u578b\u94fe 0\n isFull = bitmask & CLONE_SYMBOLS_FLAG; //\u62f7\u8d1dSymbol 4\n // \u8fd9\u91cc\u5224\u65ad\u662f\u5426\u6709customizer\uff0c\u5982\u679c\u5b58\u5728\uff0c\u5c31\u4f1a\u6267\u884ccustomizer\uff0c\u4e5f\u5c31\u662f\u81ea\u5b9a\u4e49clone\u51fd\u6570\n if (customizer) {\n result = object ? customizer(value, key, object, stack) : customizer(value);\n }\n // \u5224\u65ad\u51fd\u6570\u8fd4\u56de\u503c,\u5982\u679c\u4f7f\u7528\u8005\u81ea\u5df1\u5728customizer\u51fd\u6570\u4e2d\u8fd4\u56de\u4e86\u503c\uff0c\u90a3\u4e48\u76f4\u63a5return \u8fd4\u56de\u503c\n if (result !== undefined) {\n return result;\n }\n // \u975e\u5bf9\u8c61 \u76f4\u63a5\u8fd4\u56de isObject\u4e2d\u5224\u65ad\u4e5f\u5f88\u660e\u786e\uff0c\u975enull\u4ee5\u53catypeof Object || typeof Function\n if (!isObject(value)) {\n return value;\n }\n // \u8fd9\u91cc\u7684isArray\u65b9\u6cd5\uff0c\u91cc\u9762\u7528\u4e86Array.isArray\n var isArr = isArray(value);\n // \u5982\u679c\u662f\u6570\u7ec4 \u6267\u884cinitCloneArray\u65b9\u6cd5 initCloneArray\u8fd9\u4e2a\u65b9\u6cd5\u4e0b\u9762\u4e00\u8d77\u770b\n if (isArr) {\n result = initCloneArray(value);\n // \u5982\u679c\u662f\u6d45\u62f7\u8d1d\uff0c\u5219\u76f4\u63a5\u5faa\u73af\u8d4b\u503c\u5230result\u4e0a\uff0c\u4f46\u662f\u5e76\u672a\u521b\u5efa\u65b0\u6570\u7ec4\n if (!isDeep) {\n return copyArray(value, result);\n }\n } else {\n // \u8fd9\u91cc\u83b7\u53d6\u7c7b\u578b\uff0cgetTag\u65b9\u6cd5\u91cc\u8fd4\u56de\u5b57\u7b26\u4e32\uff0c\u6bd4\u5982\u5bf9\u8c61---\"[Object object]\"\n var tag = getTag(value),\n isFunc = tag == funcTag || tag == genTag;\n // \u5982\u679c\u662fbuffer\n if (isBuffer(value)) {\n return cloneBuffer(value, isDeep);\n }\n // \u5982\u679c\u662f\u5bf9\u8c61\u6216\u8005function\n if (tag == objectTag || tag == argsTag || (isFunc && !object)) {\n result = (isFlat || isFunc) ? {} : initCloneObject(value);\n if (!isDeep) {\n // \u662f\u5426\u62f7\u8d1d\u539f\u578b\n return isFlat\n ? copySymbolsIn(value, baseAssignIn(result, value))\n : copySymbols(value, baseAssign(result, value));\n }\n } else {\n if (!cloneableTags[tag]) {\n return object ? value : {};\n }\n result = initCloneByTag(value, tag, isDeep);\n }\n }\n // Check for circular references and return its corresponding clone.\n // \u5224\u65adstacked\u4e2d\u662f\u5426\u5b58\u5728value Stack\u4f7f\u7528\u51fd\u6570\u539f\u578b\u94fe\u7684\u6765\u5b9e\u73b0\u5b58\u50a8\u5bf9\u8c61\n stack || (stack = new Stack);\n var stacked = stack.get(value);\n if (stacked) {\n return stacked;\n }\n stack.set(value, result);\n // \u8fd9\u91cc\u5224\u65adSet \u4ee5\u53caMap \u4e00\u4f1a\u8be6\u7ec6\u770b\n if (isSet(value)) {\n value.forEach(function(subValue) {\n result.add(baseClone(subValue, bitmask, customizer, subValue, value, stack));\n });\n } else if (isMap(value)) {\n value.forEach(function(subValue, key) {\n result.set(key, baseClone(subValue, bitmask, customizer, key, value, stack));\n });\n }\n\n var keysFunc = isFull\n ? (isFlat ? getAllKeysIn : getAllKeys)\n : (isFlat ? keysIn : keys);\n // Symbolh\u548c__proto__\n var props = isArr ? undefined : keysFunc(value);\n arrayEach(props || value, function(subValue, key) {\n if (props) {\n key = subValue;\n subValue = value[key];\n }\n // Recursively populate clone (susceptible to call stack limits).\n assignValue(result, key, baseClone(subValue, bitmask, customizer, key, value, stack));\n });\n // \u8fd4\u56deresult\n return result;\n}\n\nexport default baseClone;\n```\n\n### \u6df1\u62f7\u8d1d\u7684\u5177\u4f53\u5b9e\u73b0\n\n\u5206\u6790\u4e0a\u9762\u5177\u4f53\u662f\u5982\u4f55\u6df1\u62f7\u8d1d\u7684\n\n#### 1.initCloneArray\n\n```js\nfunction initCloneArray(array) {\n // \u5f97\u5230\u6570\u7ec4\u7684length\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u65b0\u6570\u7ec4\u7684length\u4e0e\u539f\u6570\u7ec4\u4fdd\u6301\u4e00\u81f4\n var length = array.length,\n result = new array.constructor(length);\n\n // Add properties assigned by `RegExp#exec`.\n // \u6b63\u5219\u8fd4\u56de\u7684\u6570\u7ec4 \u56e0\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f regexObj.exec(str) \u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u5339\u914d\u5931\u8d25\u8fd4\u56denull\n if (length && typeof array[0] == 'string' && hasOwnProperty.call(array, 'index')) {\n result.index = array.index;\n result.input = array.input;\n }\n return result;\n}\n\nexport default initCloneArray;\n```\n\n\u8fd9\u91cc\u5bf9\u4e8e\u6570\u7ec4\uff0c\u8be5\u65b9\u6cd5\u53ea\u662f\u521b\u5efa\u6570\u7ec4\uff0c\u5982\u679c\u5b58\u5728\u6b63\u5219\uff0c\u53ea\u62f7\u8d1d index \u548c input\uff0c\u6570\u7ec4\u91cc\u7684\u503c\u5e76\u6ca1\u6709\u62f7\u8d1d\uff0c\u540e\u9762\u4f1a\u770b\u5230\n\n#### 2.copyObject && initCloneObject\n\n```js\n//initCloneObject\nfunction initCloneObject(object) {\n // \u6784\u9020\u51fd\u6570\u662f\u5426\u5728\u539f\u578b\u4e0a \u5982\u679c\u662f\uff0c\u4f7f\u7528Object.create\u521b\u5efa\u539f\u578b\u526f\u672c\n return (typeof object.constructor == 'function' && !isPrototype(object))\n ? Object.create(Object.getPrototypeOf(object))\n : {}\n}\n//isPrototype\nfunction isPrototype(value) {\n const Ctor = value && value.constructor\n // \u5bf9\u5e94\u539f\u578b\n const proto = (typeof Ctor == 'function' && Ctor.prototype) || Object.prototype\n return value === proto\n}\n// copyObject \u8be5\u65b9\u6cd5\u5c31\u662f\u4e0a\u9762\u7684copySymbolsIn / copySymbols\u91cc\u8c03\u7528\u7684\uff0ccopySymbolsIn / copySymbols\u4e3b\u8981\u533a\u5206\u662f\u5426\u62f7\u8d1dSymbol\nfunction copyObject(source, props, object, customizer) {\n var isNew = !object;\n object || (object = {});\n\n var index = -1,\n length = props.length;\n // \u5faa\u73af \u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u5982\u679c\u6709\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u5219\u4f20\u5165\u6267\u884c\n while (++index < length) {\n var key = props[index];\n\n var newValue = customizer\n ? customizer(object[key], source[key], key, object, source)\n : undefined;\n // \u7b49\u5f85\u81ea\u5b9a\u4e49\u65b9\u6cd5\u6267\u884c\u5b8c\u6bd5\uff0c\u5982\u679c\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u5219\u8ba4\u4e3a\u8be5\u5c5e\u6027\u76f4\u63a5\u62f7\u8d1d\u5373\u53ef\n if (newValue === undefined) {\n newValue = source[key];\n }\n // \u662f\u5426\u5b58\u5728Symbol\u7684\u6570\u7ec4\n if (isNew) {\n baseAssignValue(object, key, newValue);\n } else {\n assignValue(object, key, newValue);\n }\n }\n return object;\n}\n\nexport default copyObject;\n```\n\n#### 3.baseAssignValue \u548c assignValue\n\n\u770b\u4e0b baseAssignValue \u548c assignValue \u7684\u533a\u522b\u4ee5\u53ca\u5b83\u4eec\u662f\u5982\u4f55\u7ed9\u5bf9\u8c61\u5c5e\u6027\u8d4b\u503c\u7684\n\n```js\n// baseAssignValue\nfunction baseAssignValue(object, key, value) {\n // \u5982\u679ckey\u662f\u539f\u578b\uff0c\u90a3\u4e48\u4f7f\u7528defineProperty\u6765\u6539\u53d8\u5c5e\u6027\u7684\u503c\n if (key == '__proto__' && defineProperty) {\n defineProperty(object, key, {\n 'configurable': true,\n 'enumerable': true,\n 'value': value,\n 'writable': true\n });\n } else {\n // \u76f4\u63a5\u6539\u53d8\u5c5e\u6027\u503c\n object[key] = value;\n }\n}\n\nexport default baseAssignValue;\n\n//assignValue\nfunction assignValue(object, key, value) {\n var objValue = object[key];\n // \u503c\u662f\u5426\u4e0d\u76f8\u7b49\uff0c\u5982\u679c\u4e0d\u76f8\u7b49\uff0c\u53ca\u6267\u884cbaseAssignValue\uff0c\u4e5f\u5c31\u662f\u771f\u6b63\u8d4b\u503c\u7684\u65b9\u6cd5\n if (!(hasOwnProperty.call(object, key) && eq(objValue, value)) ||\n (value === undefined && !(key in object))) {\n baseAssignValue(object, key, value);\n }\n}\n\nexport default assignValue;\n```\n\n#### 4.\u5bf9\u4e8e Set \u548c Map \u7684\u5904\u7406\n\n\u5904\u7406\u65b9\u5f0f\u4e5f\u5f88\u6613\u61c2\uff0c\u5982\u679c\u662f Set\uff0c\u5219\u5faa\u73af add \u4e0a\u53bb\uff0c\u56e0\u4e3a\u6211\u4eec\u5728\u4e0a\u9762\u770b\u5230\u4e86\uff0c\u5982\u679c\u662f\u5bf9\u8c61\u3002\u5219\u4f1a\u62f7\u8d1d\u51fa\u4e00\u4efd\u539f\u578b\u526f\u672c\uff0c\u6240\u4ee5\u6b64\u65f6\u7684\u76ee\u6807\u5bf9\u8c61\u539f\u578b\u4e0a\uff0c\u6709\u6e90\u5bf9\u8c61\u7684\u539f\u578b\u53ca\u65b9\u6cd5\n\u5982\u679c\u662f Map\uff0c\u548c\u4e0a\u9762\u4e00\u6837\uff0c\u53ea\u662f set \u65b9\u6cd5\u800c\u4e0d\u662f add \u65b9\u6cd5\n\n#### 5.\u6570\u7ec4\u7684\u904d\u5386\n\n\u5728\u6838\u5fc3\u65b9\u6cd5 baseClone \u91cc\uff0c\u65b9\u6cd5\u7684\u672b\u5c3e\u53c8\u5224\u65ad\u4e86\u662f\u5426\u662f\u6570\u7ec4\uff0c\u4e0a\u9762\u7684 initCloneArray \u6211\u4eec\u53ea\u662f\u770b\u5230\u521b\u5efa\u4e86\u4e0e\u539f\u6570\u7ec4\u957f\u5ea6\u4e00\u6837\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u672a\u8d4b\u503c\n\n```js\n// baseClone\n// \u662f\u5426\u6709\u662f\u6570\u7ec4\uff0c\u5982\u679c\u662f\u6570\u7ec4\uff0c\u6211\u4eec\u5df2\u7ecf\u6709\u65b0\u7684\u6570\u7ec4\u7b49\u5f85\u8d4b\u503c\uff0c\u5426\u5219\u7684\u8bdd\uff0c\u518d\u6b21\u63d0\u53d6\u539f\u6570\u7ec4\u7684key\n var props = isArr ? undefined : keysFunc(value);\n arrayEach(props || value, function(subValue, key) {\n // \u66ff\u6362\u771f\u6b63\u7684key,value\n if (props) {\n key = subValue;\n subValue = value[key];\n }\n // Recursively populate clone (susceptible to call stack limits).\n // \u8d4b\u503c\u4ee5\u53ca\u9012\u5f52\u62f7\u8d1d \u5b9e\u73b0\u591a\u5c42\u6df1\u62f7\u8d1d\uff0c\u800c\u4e0d\u662f\u4e00\u5c42\n assignValue(result, key, baseClone(subValue, bitmask, customizer, key, value, stack));\n });\n```", "answer": "", "biz_type": 1, "qid": 501, "subject": "", "title": "loadsh \u6df1\u62f7\u8d1d\u5b9e\u73b0\u539f\u7406"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9177\u5bb6\u4e50"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 5, "qid": 532, "qtype": "short_answer", "short_answer": {"analysis": "### \u6dfb\u52a0\u539f\u751f\u4e8b\u4ef6\u7684\u95ee\u9898\n\n```js\nvar button = document.getElementById('button');\nfunction onClick(event) {\n button.innerHTML = 'text';\n}\nbutton.addEventListener('click', onClick);\n```\n\n\u7ed9\u5143\u7d20button\u6dfb\u52a0\u4e86\u4e00\u4e2a\u4e8b\u4ef6\u5904\u7406\u5668onClick, \u800c\u5904\u7406\u5668\u91cc\u9762\u4f7f\u7528\u4e86button\u7684\u5f15\u7528\u3002\u800c\u8001\u7248\u672c\u7684 IE \u662f\u65e0\u6cd5\u68c0\u6d4b DOM \u8282\u70b9\u4e0e JavaScript \u4ee3\u7801\u4e4b\u95f4\u7684\u5faa\u73af\u5f15\u7528\uff0c\u56e0\u6b64\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002\n\n\u5982\u4eca\uff0c\u73b0\u4ee3\u7684\u6d4f\u89c8\u5668\uff08\u5305\u62ec IE \u548c Microsoft Edge\uff09\u4f7f\u7528\u4e86\u66f4\u5148\u8fdb\u7684\u5783\u573e\u56de\u6536\u7b97\u6cd5\uff0c\u5df2\u7ecf\u53ef\u4ee5\u6b63\u786e\u68c0\u6d4b\u548c\u5904\u7406\u5faa\u73af\u5f15\u7528\u4e86\u3002\u6362\u8a00\u4e4b\uff0c\u56de\u6536\u8282\u70b9\u5185\u5b58\u65f6\uff0c\u4e0d\u5fc5\u975e\u8981\u8c03\u7528 removeEventListener \u4e86\u3002\n\n### \u5176\u5b83\u5185\u5b58\u6cc4\u9732\n\n\n- \u610f\u5916\u7684\u5168\u5c40\u53d8\u91cf (\u5982\u679c\u5fc5\u987b\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u5b58\u50a8\u5927\u91cf\u6570\u636e\u65f6\uff0c\u786e\u4fdd\u7528\u5b8c\u4ee5\u540e\u628a\u5b83\u8bbe\u7f6e\u4e3a null \u6216\u8005\u91cd\u65b0\u5b9a\u4e49\u3002\u4e0e\u5168\u5c40\u53d8\u91cf\u76f8\u5173\u7684\u589e\u52a0\u5185\u5b58\u6d88\u8017\u7684\u4e00\u4e2a\u4e3b\u56e0\u662f\u7f13\u5b58\u3002\u7f13\u5b58\u6570\u636e\u662f\u4e3a\u4e86\u91cd\u7528\uff0c\u7f13\u5b58\u5fc5\u987b\u6709\u4e00\u4e2a\u5927\u5c0f\u4e0a\u9650\u624d\u6709\u7528\u3002)\n- \u88ab\u9057\u5fd8\u7684\u8ba1\u65f6\u5668,\u6bd4\u5982\u4e0b\u9762\u793a\u4f8b\u4ee3\u7801, \u5c3d\u7ba1\u8fd9\u4e2a\u5b9a\u65f6\u5668\u4e0d\u518d\u9700\u8981\uff0c\u91cc\u9762\u7684\u56de\u8c03\u4e5f\u4e0d\u518d\u9700\u8981\uff0c\u53ef\u662f\u8ba1\u65f6\u5668\u56de\u8c03\u51fd\u6570\u5e76\u6ca1\u6709\u88ab\u56de\u6536\uff0c\u8fd9\u6837someResource,\u5982\u679c\u5b58\u50a8\u4e86\u5927\u91cf\u7684\u6570\u636e\uff0c\u4e5f\u662f\u65e0\u6cd5\u88ab\u56de\u6536\u3002 \u56e0\u6b64\u9700\u8981\u628a\u5b9a\u65f6\u5668\b\u6e05\u9664\u3002\n- \u8131\u79bbDOM\u7684\u5f15\u7528(\u5f53\u4f60\u4fdd\u5b58\u4e86\u4e00\u4e2adom\u7684\u5f15\u7528\uff0c\u7136\u540e\u5c06\u8be5dom\u4ecehtml\u4e2d\u5220\u9664\u540e\uff0c\u4f60\u5e94\u8be5\u5c06\u8fd9\u4e2a\u5f15\u7528\u8d4b\u4e3anull\uff0c\u5426\u5219GC\u4e0d\u4f1a\u56de\u6536\uff0c\b\u8fd9\u4e2adom\u4ecd\u7136\u5728\u5185\u5b58\u4e2d\u3002\u4fdd\u5b58 DOM \u5143\u7d20\u5f15\u7528\u7684\u65f6\u5019\uff0c\u8981\u5c0f\u5fc3\u8c28\u614e\u3002\n- \u95ed\u5305\uff0c\u95ed\u5305\u5305\u542b\u8fd9\u5916\u9762\u51fd\u6570\u7684\u6d3b\u52a8\u5bf9\u8c61\uff0c\u65e0\u6cd5\u88abGC\u56de\u6536\u3002 \b", "answer": "", "biz_type": 1, "qid": 532, "subject": "", "title": "\u6dfb\u52a0\u539f\u751f\u4e8b\u4ef6\u4e0d\u79fb\u9664\u4e3a\u4ec0\u4e48\u4f1a\u5185\u5b58\u6cc4\u9732\uff0c\u8fd8\u6709\u54ea\u4e9b\u5730\u65b9\u4f1a\u5b58\u5728\u5185\u5b58\u6cc4\u6f0f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6d77\u5eb7\u5a01\u89c6", "\u65b0\u4e1c\u65b9"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 526, "qtype": "short_answer", "short_answer": {"analysis": "### \u6570\u7ec4\u5224\u65ad\u65b9\u6cd5\n\n- arr instanceof Array\uff0c\u539f\u578b\u94fe\u5224\u65ad\uff1b\n- arr.constructor == Array\uff0c\u6784\u9020\u51fd\u6570\u5224\u65ad\uff1b\n- Object.prototype.toString.call(obj)=='Array\u2019\uff0c\u5bf9\u8c61\u7684toSting\u65b9\u6cd5\uff1b\n- Array.isArray\uff0c\u6570\u7ec4\u7684\u539f\u751f\u65b9\u6cd5\uff1b", "answer": "", "biz_type": 1, "qid": 526, "subject": "", "title": "\u5c3d\u53ef\u80fd\u591a\u7684\u5199\u51fa\u5224\u65ad\u6570\u7ec4\u7684\u65b9\u6cd5"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 7, "qid": 651, "qtype": "short_answer", "short_answer": {"analysis": "### \u6269\u5c55\u4e00\u4e0b\n\n- offset \u76f8\u5173\uff1aoffsetWidth offsetHeight offsetTop offsetLeft\n- client \u76f8\u5173\uff1aclientWidth clientHeight clientTop clientLeft\n- scroll \u76f8\u5173\uff1ascrollWidth scrollHeight scrollTop scrollLeft\n\n#### 1.offset\n\n**offsetHeight**:\u662f\u4e00\u4e2a\u53ea\u8bfb\u5c5e\u6027\uff0c\u5b83\u8fd4\u56de\u8be5\u5143\u7d20\u7684\u50cf\u7d20\u9ad8\u5ea6\uff0c\u9ad8\u5ea6\u5305\u542b\u8be5\u5143\u7d20\u7684\u5782\u76f4\u5185\u8fb9\u8ddd\u548c\u8fb9\u6846\uff0c\u4e14\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u4e0d\u5305\u542b:before \u6216:after \u7b49\u4f2a\u7c7b\u5143\u7d20\u7684\u9ad8\u5ea6\uff0c\u5982\u679c\u5143\u7d20\u88ab\u9690\u85cf\uff08\u4f8b\u5982 \u5143\u7d20\u6216\u8005\u5143\u7d20\u7684\u7956\u5148\u4e4b\u4e00\u7684\u5143\u7d20\u7684 style.display \u88ab\u8bbe\u7f6e\u4e3a none\uff09\uff0c\u5219\u8fd4\u56de 0\uff0c\n\n**offsetWidth**:\u540c\u4e0a\uff0c\u5c06\u7406\u89e3\u9ad8\u5ea6\u4e3a\u5bbd\u5ea6\u5373\u53ef\n\n\u8fd9\u4e2a\u4e24\u4e2a\u5c5e\u6027\u503c\u4f1a\u88ab\u56db\u820d\u4e94\u5165\u4e3a\u6574\u6570\u503c\uff0c\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u6d6e\u70b9\u6570\u503c\uff0c\u8bf7\u7528 element.getBoundingClientRect()\n\n**offsetLeft**:\u4e3a\u53ea\u8bfb\u5c5e\u6027\uff0c\u5b83\u8fd4\u56de\u5f53\u524d\u5143\u7d20 \u5de6\u8fb9\u6846 \u5916\u8fb9\u7f18 \u5230 \u6700\u8fd1\u7684\u5df2\u5b9a\u4f4d\u7236\u7ea7\uff08offsetParent\uff09 \u5de6\u8fb9\u6846 \u5185\u8fb9\u7f18\u7684\u8ddd\u79bb\u3002\u5982\u679c\u7236\u7ea7\u90fd\u6ca1\u6709\u5b9a\u4f4d\uff0c\u5219\u662f\u5230 body \u5de6\u8fb9\u7684\u8ddd\u79bb\n\n**offsetTop**\uff1a\u4e3a\u53ea\u8bfb\u5c5e\u6027\uff0c\u5f53\u524d\u5143\u7d20 \u4e0a\u8fb9\u6846 \u5916\u8fb9\u7f18 \u5230 \u6700\u8fd1\u7684\u5df2\u5b9a\u4f4d\u7236\u7ea7\uff08offsetParent\uff09 \u4e0a\u8fb9\u6846 \u5185\u8fb9\u7f18\u7684 \u8ddd\u79bb\u3002\u5982\u679c\u7236\u7ea7\u90fd\u6ca1\u6709\u5b9a\u4f4d\uff0c\u5219\u662f\u5230 body \u9876\u90e8\u7684\u8ddd\u79bb\n\n#### 2.client\n\n- **clientWidth**:\u5305\u62ec\u5143\u7d20\u5185\u5bb9\u7684\u5bbd\u5ea6\u3001\u5de6\u53f3 padding\n- **clientHeight**:\u5305\u62ec\u5143\u7d20\u5185\u5bb9\u7684\u9ad8\u5ea6\u3001\u4e0a\u4e0b padding\n- **clientTop**:\u4e3a\u53ea\u8bfb\u5c5e\u6027,\u4e0a\u8fb9\u6846\u7684\u5bbd\u5ea6\n- **clientLeft**:\u8868\u793a\u4e00\u4e2a\u5143\u7d20\u7684\u5de6\u8fb9\u6846\u7684\u5bbd\u5ea6\uff0c\u4ee5\u50cf\u7d20\u8868\u793a\u3002\u5982\u679c\u5143\u7d20\u7684\u6587\u672c\u65b9\u5411\u662f\u4ece\u53f3\u5411\u5de6\uff08RTL, right-to-left\uff09\uff0c\u5e76\u4e14\u7531\u4e8e\u5185\u5bb9\u6ea2\u51fa\u5bfc\u81f4\u5de6\u8fb9\u51fa\u73b0\u4e86\u4e00\u4e2a\u5782\u76f4\u6eda\u52a8\u6761\uff0c\u5219\u8be5\u5c5e\u6027\u5305\u62ec\u6eda\u52a8\u6761\u7684\u5bbd\u5ea6\u3002clientLeft \u4e0d\u5305\u62ec\u5de6\u5916\u8fb9\u8ddd\u548c\u5de6\u5185\u8fb9\u8ddd\u3002clientLeft \u662f\u53ea\u8bfb\u7684\u3002\n\n#### 3.scroll\n\n**scrollWidth**:\u5bbd\u5ea6\u7684\u6d4b\u91cf\u65b9\u5f0f\u4e0e clientWidth \u76f8\u540c\uff1a\u5b83\u5305\u542b\u5143\u7d20\u7684\u5185\u8fb9\u8ddd\uff0c\u4f46\u4e0d\u5305\u62ec\u8fb9\u6846\uff0c\u5916\u8fb9\u8ddd\u6216\u5782\u76f4\u6eda\u52a8\u6761\uff08\u5982\u679c\u5b58\u5728\uff09\u3002 \u5b83\u8fd8\u53ef\u4ee5\u5305\u62ec\u4f2a\u5143\u7d20\u7684\u5bbd\u5ea6\uff0c\u4f8b\u5982::before \u6216::after\u3002 \u5982\u679c\u5143\u7d20\u7684\u5185\u5bb9\u53ef\u4ee5\u9002\u5408\u800c\u4e0d\u9700\u8981\u6c34\u5e73\u6eda\u52a8\u6761\uff0c\u5219\u5176 scrollWidth \u7b49\u4e8e clientWidth\n\n\u8c37\u6b4c\u83b7\u53d6\u7684 Element.scrollWidth \u548c IE\uff0c\u706b\u72d0\u4e0b\u83b7\u53d6\u7684 Element.scrollWidth \u5e76\u4e0d\u76f8\u540c\n\n**scrollHeight**: \u7406\u89e3\u540c\u4e0a\n\n**scrollTop**:\u8bfb\u53d6\u6216\u8bbe\u7f6e\u5143\u7d20\u5185\u5bb9\u5c42\u9876\u90e8 \u5230 \u53ef\u89c6\u533a\u57df\u9876\u90e8\u7684\u8ddd\u79bb\n\n**scrollTop** \u53ef\u4ee5\u88ab\u8bbe\u7f6e\u4e3a\u4efb\u4f55\u6574\u6570\u503c\uff0c\u540c\u65f6\u6ce8\u610f\uff1a \n\n1. \u5982\u679c\u4e00\u4e2a\u5143\u7d20\u4e0d\u80fd\u88ab\u6eda\u52a8\uff08\u4f8b\u5982\uff0c\u5b83\u6ca1\u6709\u6ea2\u51fa\uff0c\u6216\u8005\u8fd9\u4e2a\u5143\u7d20\u6709\u4e00\u4e2a\"non-scrollable\"\u5c5e\u6027\uff09\uff0c scrollTop \u5c06\u88ab\u8bbe\u7f6e\u4e3a 0\u3002 \n2. \u8bbe\u7f6e scrollTop \u7684\u503c\u5c0f\u4e8e 0\uff0cscrollTop \u88ab\u8bbe\u4e3a 0 \n3. \u5982\u679c\u8bbe\u7f6e\u4e86\u8d85\u51fa\u8fd9\u4e2a\u5bb9\u5668\u53ef\u6eda\u52a8\u7684\u503c, scrollTop \u4f1a\u88ab\u8bbe\u4e3a\u6700\u5927\u503c\u3002\n\n\u517c\u5bb9\u6027\u5199\u6cd5 `var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop`\n\n\u8bfb\u53d6\u6216\u8bbe\u7f6e\u5143\u7d20\u5185\u5bb9\u5c42\u5de6\u7aef \u5230 \u53ef\u89c6\u533a\u57df\u5de6\u7aef\u7684\u8ddd\u79bb\n\n\u6ce8\u610f\u5982\u679c\u8fd9\u4e2a\u5143\u7d20\u7684\u5185\u5bb9\u6392\u5217\u65b9\u5411\uff08direction\uff09 \u662f rtl (right-to-left) \uff0c\u90a3\u4e48\u6eda\u52a8\u6761\u4f1a\u4f4d\u4e8e\u6700\u53f3\u4fa7\uff08\u5185\u5bb9\u5f00\u59cb\u5904\uff09\uff0c\u5e76\u4e14 scrollLeft \u503c\u4e3a 0\u3002\u6b64\u65f6\uff0c\u5f53\u4f60\u4ece\u53f3\u5230\u5de6\u62d6\u52a8\u6eda\u52a8\u6761\u65f6\uff0cscrollLeft \u4f1a\u4ece 0 \u53d8\u4e3a\u8d1f\u6570\u3002\n\n\u517c\u5bb9\u6027\u5199\u6cd5 `var scrollLeft = document.documentElement.scrollLeft || window.pageXOffset || document.body.scrollLeft`\n", "answer": "", "biz_type": 1, "qid": 651, "subject": "", "title": "\u8bf4\u4e0b offsetWith \u548c clientWidth\u3001offsetHeight \u548c clientHeight \u7684\u533a\u522b\uff0c\u8bf4\u8bf4 offsetTop\uff0coffsetLeft\uff0cscrollWidth\u3001scrollHeight \u5c5e\u6027\u90fd\u662f\u5e72\u5565\u7684"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 9, "qid": 568, "qtype": "short_answer", "short_answer": {"analysis": "### \u5b9e\u73b0\u65b9\u6848\n\n#### 1.\u5b9e\u73b0\u65b9\u5f0f\u4e00\n\n```js\n//\u901a\u8fc7\u6807\u7b7e\u8bed\u53e5,\u8df3\u51fa\u65e0\u9650\u5faa\u73af\uff1a\nloop1:\nwhile(1){\n switch ('yideng'){\n case 'yideng': break loop1;\n }\n}\n```\n\n#### 2.\u5b9e\u73b0\u65b9\u5f0f\u4e8c\n\n```js\ntry{\n while (1) {\n switch(\"yideng\"){\n case \"yideng\" :\n \t\tconsole.log(true);\n \t\tthrow new Error(\"\u8df3\u51fa\u5faa\u73af\");\n } \n }\n} catch(e){\n console.log(e);\n}\n```\n\n#### 3.\u5b9e\u73b0\u65b9\u5f0f\u4e09\n\n```js\n//javascript goto\u5b9e\u73b0 \nout:\nfor(let i =0;i<1;i++){\n\twhile (1) {\n switch (\"yideng\") {\n case \"yideng\":\n console.log(\"yideng\");\n continue out\n }\n\t}\n}\n```\n\n#### 4.\u5b9e\u73b0\u65b9\u5f0f\u56db\n\n```js\n//return\nfunction test(){\n while (1) {\n switch (\"yideng\") {\n case \"yideng\":\n console.log(\"yideng\");\n return;\n }\n }\n}\ntest();\n```\n\n\n", "answer": "", "biz_type": 1, "qid": 568, "subject": "```js\nwhile (1) {\n switch (\"yideng\") {\n case \"yideng\":\n //\u7981\u6b62\u76f4\u63a5\u5199\u4e00\u53e5break\n }\n}\n```\n", "title": "\u8bf7\u4fee\u6539\u4ee3\u7801\u80fd\u8df3\u51fa\u6b7b\u5faa\u73af"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fae\u533b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 500, "qtype": "short_answer", "short_answer": {"analysis": "### \u533a\u522b\n\n\u8bcd\u6cd5\u4f5c\u7528\u57df\u5305\u542b\u4e86\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\u7684\u53d8\u91cf\u58f0\u660e\uff0cthis \u662f\u6267\u884c\u4e0a\u4e0b\u6587\u7684\u4e00\u4e2a\u53ef\u80fd\u4e3a\u7a7a\u503c\u7684\u5c5e\u6027\uff0c\u662f\u5bf9\u5bf9\u8c61\u7684\u5f15\u7528\u3002\n\nJS \u5f15\u64ce\u6267\u884c\u4e00\u6bb5\u548c\u5f53\u524d\u6267\u884c\u4e0a\u4e0b\u6587\uff08running execution context\uff09\u65e0\u5173\u7684\u4ee3\u7801\u65f6\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u5bf9\u5e94\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u7528\u4e8e\u8ffd\u8e2a\u4ee3\u7801\u6267\u884c\u8fc7\u7a0b\uff0c\u5e76\u5c06\u5f53\u524d\u6267\u884c\u4e0a\u4e0b\u6587\u6307\u5411\u8be5\u4e0a\u4e0b\u6587\u3002\u6267\u884c\u4e0a\u4e0b\u6587\u7684\u521b\u5efa\u8fc7\u7a0b\u4e2d\u4f1a\u6839\u636e\u8bed\u6cd5\u5757\uff08\u5757\u7ea7\u4f5c\u7528\u57df\uff09\u521b\u5efa\u5bf9\u5e94\u7684\u73af\u5883\u8bb0\u5f55\u3002\n\n\u8bcd\u6cd5\u4f5c\u7528\u57df\u4f1a\u901a\u8fc7\u73af\u5883\u8bb0\u5f55\uff08Environment Record\uff09\u6765\u5b58\u50a8\u6807\u8bc6\u7b26\u4e0e\u5b9e\u9645\u53d8\u91cf\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u548c\u4e00\u4e2a\u5916\u90e8\u73af\u5883\uff08OuterEnv\uff09\u6765\u5f15\u7528\u4e0a\u7ea7\u4f5c\u7528\u57df\u3002\n\n\u73af\u5883\u8bb0\u5f55\u5305\u62ec\u8bcd\u6cd5\u73af\u5883\uff08Lexical Environment\uff09\u548c\u53d8\u91cf\u73af\u5883\uff08Variable Environment\uff09\uff0c\u5757\u7ea7\u58f0\u660e\u5982 let/function \u7b49\u5b58\u4e8e\u8bcd\u6cd5\u73af\u5883\uff0c\u5176\u5b83\u58f0\u660e\u5982 var \u7b49\u5b58\u4e8e\u53d8\u91cf\u73af\u5883\u3002\n\n\u4ee3\u7801\u6267\u884c\u4e2d\uff0c\u8bcd\u6cd5\u73af\u5883\u53ef\u80fd\u4f1a\u5efa\u7acb\u73af\u5883\u8bb0\u5f55\u4e0e this \u7684\u7ed1\u5b9a\u3002\u89e3\u6790 this \u65f6\uff0c\u4ece\u5f53\u524d\u4e0a\u4e0b\u6587\u4e2d\u5f00\u59cb\u76f4\u5230\u5168\u5c40\uff0c\u8fd4\u56de\u6700\u8fd1\u53ef\u7528\u7684 this \u7ed1\u5b9a\u3002", "answer": "", "biz_type": 1, "qid": 500, "subject": "", "title": "\u8bcd\u6cd5\u4f5c\u7528\u57df\u548c this \u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9177\u5bb6\u4e50"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 6, "qid": 541, "qtype": "short_answer", "short_answer": {"analysis": "### \u533a\u522b\n\nfor-in \u662fjavaScript\u4e2d\u6700\u5e38\u89c1\u7684\u8fed\u4ee3\u8bed\u53e5\uff0c\u5e38\u5e38\u7528\u6765\u679a\u4e3e\u5bf9\u8c61\u7684\u5c5e\u6027\u3002\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u6309\u7167\u968f\u673a\u987a\u5e8f\u904d\u5386\u6570\u7ec4\u5143\u7d20\uff1b\n\n\u800cObject\u6784\u9020\u5668\u6709\u4e00\u4e2a\u5b9e\u4f8b\u5c5e\u6027keys\uff0c\u5219\u53ef\u4ee5\u8fd4\u56de\u4ee5\u5bf9\u8c61\u7684\u5c5e\u6027\u4e3a\u5143\u7d20\u7684\u6570\u7ec4\u3002\u6570\u7ec4\u4e2d\u5c5e\u6027\u540d\u7684\u987a\u5e8f\u8ddf\u4f7f\u7528for-in\u904d\u5386\u8fd4\u56de\u7684\u987a\u5e8f\u662f\u4e00\u6837\u7684\u3002\n\nfor-in\u5faa\u73af\u4f1a\u679a\u4e3e\u5bf9\u8c61\u539f\u578b\u94fe\u4e0a\u7684\u53ef\u679a\u4e3e\u5c5e\u6027\uff0c\u800cObject.keys\u4e0d\u4f1a", "answer": "", "biz_type": 1, "qid": 541, "subject": "", "title": "for..in \u548c object.keys \u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f1a\u5c0f\u4e8c"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 9, "qid": 615, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n\u4fdd\u8bc1\u4e00\u4e2a\u7c7b\u4ec5\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u8bbf\u95ee\u5b83\u7684\u5168\u5c40\u8bbf\u95ee\u70b9\uff0c\u4e00\u822c\u8d2d\u7269\u8f66\u3001\u767b\u5f55\u7b49\u90fd\u662f\u4e00\u4e2a\u5355\u4f8b\u3002\n\n#### 1.\u4ee3\u7801\u793a\u4f8b\n\n```js\n// es6\nclass SingleManage {\n constructor({ name, level }) {\n if (!SingleManage.instance) {\n this.name = name;\n this.level = level\n SingleManage.instance = this;\n }\n return SingleManage.instance\n }\n}\n\nlet boss = new SingleManage({name:\"Jokul\", level:\"1\"})\nlet boss2 = new SingleManage({name:\"Jokul2\", level:\"2\"})\nconsole.log(boss === boss2)\n\n// es5\nfunction SingleManage(manage) {\n this.name = manage.name\n this.level = manage.level\n this.info = function () {\n console.warn(\"Boss's name is \" + this.name + \" and level is \" + this.level)\n }\n}\nSingleManage.getInstance = function (manage) {\n if (!this.instance) {\n this.instance = new SingleManage(manage)\n }\n return this.instance\n}\nvar boss = SingleManage.getInstance({ name: \"Jokul\", level: \"1\" })\nvar boss2 = SingleManage.getInstance({ name: \"Jokul2\", level: \"2\" })\nboss.info()\nboss2.info()\n```\n\n#### 2.\u5e94\u7528\u5b9e\u4f8b\n\n- \u5b9e\u73b0\u4e00\u4e2astorage\n\n```js\n// \u5148\u5b9e\u73b0\u4e00\u4e2a\u57fa\u7840\u7684StorageBase\u7c7b\uff0c\u628agetItem\u548csetItem\u65b9\u6cd5\u653e\u5728\u5b83\u7684\u539f\u578b\u94fe\u4e0a\nfunction StorageBase () {}\nStorageBase.prototype.getItem = function (key){\n return localStorage.getItem(key)\n}\nStorageBase.prototype.setItem = function (key, value) {\n return localStorage.setItem(key, value)\n}\n\n// \u4ee5\u95ed\u5305\u7684\u5f62\u5f0f\u521b\u5efa\u4e00\u4e2a\u5f15\u7528\u81ea\u7531\u53d8\u91cf\u7684\u6784\u9020\u51fd\u6570\nconst Storage = (function(){\n let instance = null\n return function(){\n // \u5224\u65ad\u81ea\u7531\u53d8\u91cf\u662f\u5426\u4e3anull\n if(!instance) {\n // \u5982\u679c\u4e3anull\u5219new\u51fa\u552f\u4e00\u5b9e\u4f8b\n instance = new StorageBase()\n }\n return instance\n }\n})()\n\n// \u8fd9\u91cc\u5176\u5b9e\u4e0d\u7528 new Storage \u7684\u5f62\u5f0f\u8c03\u7528\uff0c\u76f4\u63a5 Storage() \u4e5f\u4f1a\u6709\u4e00\u6837\u7684\u6548\u679c \nconst storage1 = new Storage()\nconst storage2 = new Storage()\n\nstorage1.setItem('name', 'yd')\n// yd\nstorage1.getItem('name')\n// \u4e5f\u662fyd\nstorage2.getItem('name')\n\n// \u8fd4\u56detrue\nstorage1 === storage2\n```", "answer": "", "biz_type": 1, "qid": 615, "subject": "", "title": "JavaScript \u5199\u4e00\u4e2a\u5355\u4f8b\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5177\u4f53\u5230\u67d0\u4e00\u4e2a\u573a\u666f"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u817e\u8baf\u5e94\u7528\u5b9d"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 5, "qid": 633, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n```js\nFunction.prototype.mycall = function (context, ...argus) {\n if (typeof this !== 'function') {\n throw new TypeError('not funciton')\n }\n const fn = this\n let result = null\n\n context = context || window\n context.fn = fn\n result = context.fn(...argus)\n delete context.fn\n\n return result\n}\n```", "answer": "", "biz_type": 1, "qid": 633, "subject": "", "title": "\u624b\u5199\u5b9e\u73b0 call"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 10, "qid": 555, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n```js\nclass PersonGenerator {\n taskQueue = [];\n constructor(name) {\n this.taskQueue.push(() => this.sayHi(name));\n this.runTaskQueue();\n }\n nextTask = () => {\n if (this.taskQueue.length > 0) {\n const task = this.taskQueue.shift();\n if (typeof task === \"function\") {\n task();\n this.nextTask();\n }\n if (typeof task === \"number\") {\n console.log(`Sleep ${task} seconds \\n`);\n setTimeout(() => this.nextTask(), task * 1000);\n }\n }\n };\n\n runTaskQueue = () => {\n setTimeout(() => this.nextTask());\n };\n\n sayHi(name) {\n console.log(`Hi! This is ${name}! \\n`);\n return this;\n }\n\n sleep(seconds) {\n this.taskQueue.push(seconds);\n return this;\n }\n\n sleepFirst(seconds) {\n this.taskQueue.splice(-1, 0, seconds);\n return this;\n }\n\n eat(food) {\n this.taskQueue.push(() => console.log(`Eat ${food}~ \\n`));\n return this;\n }\n}\n\nconst Person = name => new PersonGenerator(name);\n\nPerson(\"helloWorld\").sleepFirst(3).sleep(3).eat(\"little_cute\");\n```", "answer": "", "biz_type": 1, "qid": 555, "subject": "```js\nPerson(\"Li\");\n// \u8f93\u51fa\uff1a Hi! This is Li!\n\nPerson(\"Dan\").sleep(10).eat(\"dinner\");\n// \u8f93\u51fa\uff1a\n// Hi! This is Dan!\n// \u7b49\u5f8510\u79d2..\n// Wake up after 10\n// Eat dinner~\n\nPerson(\"Jerry\").eat(\"dinner\").eat(\"supper\");\n// \u8f93\u51fa\uff1a\n// Hi This is Jerry!\n// Eat dinner~\n// Eat supper~\n\nPerson(\"Smith\").sleepFirst(5).eat(\"supper\");\n// \u8f93\u51fa\uff1a\n// \u7b49\u5f855\u79d2\n// Wake up after 5\n// Hi This is Smith!\n// Eat supper\n```\n", "title": "\u6309\u7167\u8c03\u7528\u5b9e\u4f8b\uff0c\u5b9e\u73b0\u4e0b\u9762\u7684 Person \u65b9\u6cd5"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 8, "qid": 586, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n- \u5b9e\u73b0\u4e00\uff1a\u76f4\u63a5eval\n\n```js\nfunction parse(jsonStr) {\n return eval('(' + jsonStr + ')');\n}\n```\n\n> \u907f\u514d\u5728\u4e0d\u5fc5\u8981\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528 eval\uff0ceval() \u662f\u4e00\u4e2a\u5371\u9669\u7684\u51fd\u6570\uff0c \u5b83\u6267\u884c\u7684\u4ee3\u7801\u62e5\u6709\u7740\u6267\u884c\u8005\u7684\u6743\u5229\u3002\u5982\u679c\u4f60\u7528 eval() \u8fd0\u884c\u7684\u5b57\u7b26\u4e32\u4ee3\u7801\u88ab\u6076\u610f\u65b9\uff08\u4e0d\u6000\u597d\u610f\u7684\u4eba\uff09\u64cd\u63a7\u4fee\u6539\uff0c\u4ed6\u53ef\u80fd\u4f1a\u5728\u4f60\u5199\u7684\u7684\u7f51\u9875/\u6269\u5c55\u7a0b\u5e8f\u7684\u6743\u9650\u4e0b\uff0c\u5728\u7528\u6237\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u6076\u610f\u4ee3\u7801\u3002\n\n- \u5b9e\u73b0\u65b9\u5f0f\u4e8c:Function\n\n\u6838\u5fc3\uff1aFunction \u4e0e eval \u6709\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u53c2\u6570\u7279\u6027\u3002\n\n```js\nfunction parse(jsonStr){\n\treturn new Function('return'+jsonStr)();\n}\nvar jsonStr='{\"age\":20,\"name\":\"jack\"}';\nparse(jsonStr);\n```\n\neval \u4e0e Function \u90fd\u6709\u7740\u52a8\u6001\u7f16\u8bd1 js \u4ee3\u7801\u7684\u4f5c\u7528\uff0c\u4f46\u662f\u5728\u5b9e\u9645\u7684\u7f16\u7a0b\u4e2d\u5e76\u4e0d\u63a8\u8350\u4f7f\u7528", "answer": "", "biz_type": 1, "qid": 586, "subject": "", "title": "\u8bf7\u5b9e\u73b0\u4e00\u4e2a JSON.parse"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f1a\u5c0f\u4e8c"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 9, "qid": 613, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n#### 1.\u7b80\u5355\u76f4\u63a5\n\n```js\n const count = (start, end) => {\n console.log(start)\n let timer = setInterval(\n ()=>{\n if(start<end) console.log(start+=1);\n },100)\n return {cancel:()=>{clearInterval(timer)}}\n}\nconst fn = count(1,100);\nfn.cancel();\n```\n\n#### 2.\u5b8c\u5584\u4e00\u4e9b\n\n```js\nfunction tapper (start, end) {\n if (start > end || Number.isNaN(Number(start)) || Number.isNaN(Number(end))) throw new Error('invalid input');\n const interval = 100;\n let isCancel = false;\n const init = async () => {\n console.info(start);\n start++;\n while (start <= end && !isCancel) {\n await new Promise((resolve) => {\n setTimeout(() => {\n if (!isCancel) {\n console.info(start);\n start++;\n }\n resolve();\n }, interval)\n })\n }\n };\n\n const cancel = () => {\n isCancel = true;\n };\n\n return {\n init,\n cancel\n }\n}\n\nconst demo = tapper(1, 100);\ndemo.init();\nsetTimeout(() => {\n demo.cancel();\n}, 2000);\n```\n\n", "answer": "", "biz_type": 1, "qid": 613, "subject": "```js\n/* \n 1.\u4ecestart\u81f3end,\u6bcf\u9694100\u6beb\u79d2console.log\u4e00\u4e2a\u6570\u5b57\uff0c\u6bcf\u6b21\u6570\u5b57\u589e\u5e45\u4e3a1\n 2.\u8fd4\u56de\u7684\u5bf9\u8c61\u4e2d\u9700\u8981\u5305\u542b\u4e00\u4e2acancel\u65b9\u6cd5\uff0c\u7528\u4e8e\u505c\u6b62\u5b9a\u65f6\u64cd\u4f5c\n 3.\u7b2c\u4e00\u4e2a\u6570\u5b57\u9700\u8981\u7acb\u5373\u8f93\u51fa\n*/\n```\n", "title": "\u5b9e\u73b0\u4e00\u4e2a\u6253\u70b9\u8ba1\u65f6\u5668"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u963f\u91cc"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 10, "qid": 565, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n#### 1.\u5b9e\u73b0\u65b9\u5f0f\u4e00\n\n```js\n// es6 \u4e32\u884c\nconst mergePromise = (ajaxArray) => {\n return (async function () {\n let ret = []\n let idx = 0\n let len = ajaxArray.length\n while(idx < len) {\n let data = await ajaxArray[idx]()\n ret.push(data)\n idx++\n }\n return ret\n })()\n}\n\nmergePromise([ajax1, ajax2, ajax3]).then(data => {\n console.log('done')\n console.log(data)\n})\n```\n\n#### 2.\u5b9e\u73b0\u65b9\u5f0f\u4e8c\n\n```js\n// \u4e32\u884c\nconst mergePromise = (ajaxArray) => {\n return new Promise((resolve, reject) => {\n let tem = []\n let promise = ajaxArray.map(ajax => () => ajax().then(data => tem.push(data)))\n .reduce((memo, cur) => {\n return () => {\n return memo().then(cur)\n }\n })\n promise().then(() => {\n resolve(tem)\n })\n })\n}\n\nmergePromise([ajax1, ajax2, ajax3]).then(data => {\n console.log('done')\n console.log(data)\n})\n```\n\n#### 3.\u5b9e\u73b0\u65b9\u5f0f\u4e09\n\n```js\nconst mergePromise = (ajaxArray) => {\n //\u4e32\u884c\n return new Promise((resolve, reject) => {\n let len = ajaxArray.length\n let idx = 0\n let tem = []\n function next() {\n if (idx === len) return resolve(tem)\n ajaxArray[idx]().then((data) => {\n tem.push(data)\n idx++\n next()\n }).catch(reject)\n }\n next()\n })\n}\n\nmergePromise([ajax1, ajax2, ajax3]).then(data => {\n console.log('done')\n console.log(data)\n})\n```\n\n#### 4.\u5b9e\u73b0\u65b9\u5f0f\u56db\n\n```js\nfunction mergePromise(promiselist) {\n var result = []\n mergePromise.then = function (callback) {\n function fn(i) {\n // console.log(promiselist[i]())\n promiselist[i]().then((data) => {\n result.push(data)\n if (i !== promiselist.length - 1) {\n fn(++i)\n }\n if (result.length === promiselist.length) {\n callback(result)\n }\n })\n }\n fn(0)\n }\n return mergePromise\n}\nmergePromise([ajax1, ajax2, ajax3]).then((data) => {\n console.log('done')\n console.log(data) // data \u4e3a[1,2,3]\n})\n// \u6267\u884c\u7ed3\u679c\u4e3a\uff1a1 2 3 done [1,2,3]\n```\n", "answer": "", "biz_type": 1, "qid": 565, "subject": "```js\nconst timeout = (ms) =>\n new Promise((resolve, reject) => {\n setTimeout(() => {\n resolve();\n }, ms);\n });\nconst ajax1 = () =>\n timeout(2000).then(() => {\n console.log(\"1\");\n return 1;\n });\nconst ajax2 = () =>\n timeout(1000).then(() => {\n console.log(\"2\");\n return 2;\n });\nconst ajax3 = () =>\n timeout(2000).then(() => {\n console.log(\"3\");\n return 3;\n });\nconst mergePromise = (ajaxArray) => {\n // 1,2,3 done [1,2,3] \u6b64\u5904\u5199\u4ee3\u7801 \u8bf7\u5199\u51faES6\u3001ES3 2\u4e2d\u89e3\u6cd5\n};\nmergePromise([ajax1, ajax2, ajax3]).then((data) => {\n console.log(\"done\");\n console.log(data); // data \u4e3a[1,2,3]\n});\n// \u6267\u884c\u7ed3\u679c\u4e3a\uff1a1 2 3 done [1,2,3]\n```\n", "title": "\u6309\u8981\u6c42\u5b8c\u6210\u4ee3\u7801"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 9, "qid": 625, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n#### 1.\u5b9e\u73b0\u65b9\u5f0f\u4e00\n\n- \u58f0\u660e\u4e00\u4e2a\u51fd\u6570\u9a6c\u4e0a\u8c03\u7528\n\n```js\nvar oli = document.getElementsByTagName('li');\nfor (var i = 0; len = oli.length, i < len; i++) {\n oli[i].onclick = (function (n) {\n return function () {\n alert(n)\n }\n })(i)\n}\n```\n\n#### 2.\u5b9e\u73b0\u65b9\u5f0f\u4e8c\n\n- \u628a\u4e0b\u6807i\u53d8\u6210\u4e00\u4e2ali\u7684\u5c5e\u6027\n\n```js\nvar oli = document.getElementsByTagName('li');\nfor (var i = 0; len = oli.length, i < len; i++) {\n oli[i].index = i;\n oli[i].onclick = function () {\n alert('\u4f60\u70b9\u51fb\u7684\u5217\u8868\u7684\u4e0b\u6807\u662f\uff1a' + this.index); //\u5217\u8868\u4e0b\u6807\u4ece0\u5f00\u59cb \u00a0\n };\n}\n```\n\n#### 3.\u5b9e\u73b0\u65b9\u5f0f\u4e09\n\n- forEach\n\n```js\nvar lis = document.getElementsByTagName('li');\nlis = Array.prototype.slice.call(lis, 0);\nlis.forEach(function (v, i) {\n v.onclick = function () {\n alert(i);\n }\n})\n```\n\n#### 4.\u5b9e\u73b0\u65b9\u5f0f\u56db\n\n- let\n\n```js\nvar oli = document.getElementsByTagName('li');\nfor (let i = 0; len = oli.length, i < len; i++) {\n console.log(oli[i]);\n oli[i].onclick = function () {\n alert(i)\n }\n}\n```\n\n", "answer": "", "biz_type": 1, "qid": 625, "subject": "", "title": "\u5728\u4e00\u4e2a ul \u91cc\u6709 10 \u4e2a li,\u5b9e\u73b0\u70b9\u51fb\u5bf9\u5e94\u7684 li,\u8f93\u51fa\u5bf9\u5e94\u7684\u4e0b\u6807"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 8, "qid": 583, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n#### 1.Object.assign\n\nES6\u4e2d\u62f7\u8d1d\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u63a5\u53d7\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u62f7\u8d1d\u7684\u76ee\u6807target\uff0c\u5269\u4e0b\u7684\u53c2\u6570\u662f\u62f7\u8d1d\u7684\u6e90\u5bf9\u8c61sources\uff08\u53ef\u4ee5\u662f\u591a\u4e2a\uff09\n\n```js\nlet target = {};\nlet source = {a:'123',b:{name:'yd'}};\nObject.assign(target ,source);\nconsole.log(target);\n// {a: \"123\", b: {name: \"yd\"}}\n\n```\n\nObject.assign\u6ce8\u610f\u4e8b\u9879\n\n- \u53ea\u62f7\u8d1d\u6e90\u5bf9\u8c61\u7684\u81ea\u8eab\u5c5e\u6027\uff08\u4e0d\u62f7\u8d1d\u7ee7\u627f\u5c5e\u6027\uff09\n- \u4e0d\u4f1a\u62f7\u8d1d\u5bf9\u8c61\u4e0d\u53ef\u679a\u4e3e\u7684\u5c5e\u6027\n- undefined\u548cnull\u65e0\u6cd5\u8f6c\u6210\u5bf9\u8c61\uff0c\u5b83\u4eec\u4e0d\u80fd\u4f5c\u4e3aObject.assign\u53c2\u6570\uff0c\u4f46\u662f\u53ef\u4ee5\u4f5c\u4e3a\u6e90\u5bf9\u8c61\n- \u5c5e\u6027\u540d\u4e3aSymbol \u503c\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u88abObject.assign\u62f7\u8d1d\u3002\n\n#### 2.Array.prototype.slice\n\n```js\nlet array = [{a: 1}, {b: 2}];\nlet array1 = array.slice(0);\nconsole.log(array1);\n```\n\n- slice:\u4ece\u5df2\u6709\u7684\u6570\u7ec4\u4e2d\u8fd4\u56de\u9009\u5b9a\u7684\u5143\u7d20\n\n#### 3.Array.prototype.concat\n\n```js\nlet array = [{a: 1}, {b: 2}];\nlet array1 = [].concat(array);\nconsole.log(array1);\n```\n\n#### 4.\u6269\u5c55\u8fd0\u7b97\u7b26\n\n```js\nlet obj = {a:1,b:{c:1}}\nlet obj2 = {...obj};\nconsole.log(obj2);\n```\n\n#### 5.\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a\n\n**1\uff09\u5b9e\u73b0\u539f\u7406**\n\n\u65b0\u7684\u5bf9\u8c61\u590d\u5236\u5df2\u6709\u5bf9\u8c61\u4e2d\u975e\u5bf9\u8c61\u5c5e\u6027\u7684\u503c\u548c\u5bf9\u8c61\u5c5e\u6027\u7684\u5f15\u7528,\u4e5f\u5c31\u662f\u8bf4\u5bf9\u8c61\u5c5e\u6027\u5e76\u4e0d\u590d\u5236\u5230\u5185\u5b58\u3002\n\n```js\nfunction cloneShallow(source) {\n var target = {};\n for (var key in source) {\n if (Object.prototype.hasOwnProperty.call(source, key)) {\n target[key] = source[key];\n }\n }\n return target;\n}\n```\n\n**2\uff09for in**\n\nfor...in\u8bed\u53e5\u4ee5\u4efb\u610f\u987a\u5e8f\u904d\u5386\u4e00\u4e2a\u5bf9\u8c61\u81ea\u6709\u7684\u3001\u7ee7\u627f\u7684\u3001\u53ef\u679a\u4e3e\u7684\u3001\u975eSymbol\u7684\u5c5e\u6027\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u4e0d\u540c\u7684\u5c5e\u6027\uff0c\u8bed\u53e5\u90fd\u4f1a\u88ab\u6267\u884c\u3002\n\n**3\uff09hasOwnProperty**\n\n\u8be5\u51fd\u6570\u8fd4\u56de\u503c\u4e3a\u5e03\u5c14\u503c\uff0c\u6240\u6709\u7ee7\u627f\u4e86 Object \u7684\u5bf9\u8c61\u90fd\u4f1a\u7ee7\u627f\u5230 hasOwnProperty \u65b9\u6cd5\uff0c\u548c in \u8fd0\u7b97\u7b26\u4e0d\u540c\uff0c\u8be5\u51fd\u6570\u4f1a\u5ffd\u7565\u6389\u90a3\u4e9b\u4ece\u539f\u578b\u94fe\u4e0a\u7ee7\u627f\u5230\u7684\u5c5e\u6027\u548c\u81ea\u8eab\u5c5e\u6027\u3002\n\n", "answer": "", "biz_type": 1, "qid": 583, "subject": "", "title": "\u8bf7\u624b\u52a8\u5b9e\u73b0\u4e00\u4e2a\u6d45\u62f7\u8d1d"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 8, "qid": 628, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n#### 1.ES5\u5b9e\u73b0\n\n```js\nfunction Person(name,age,gender){\n this.name = name;\n this.age = age;\n this.gender = gender\n}\nPerson.prototype.run = function(){\n console.log(this.name + ' can run fast !')\n}\nlet lilin = new Person('lilin',25,'women');\nlet lc = new Person('lc',25,'men');\nconsole.log(lilin,lc);\nlilin.run();\n```\n\n#### 2.ES6\u5b9e\u73b0\n\n```js\nclass Person{\n constructor(name,age,gender){\n this.name = name;\n this.age = age;\n this.gender = gender\n }\n run(){\n console.log(this.name + ' can run fast !')\n }\n}\nlet lilin = new Person('lilin',25,'women');\nlet lc = new Person('lc',25,'men');\nconsole.log(lilin,lc);\nlilin.run();\n```", "answer": "", "biz_type": 1, "qid": 628, "subject": "", "title": "\u7f16\u5199\u4e00\u4e2a Person \u7c7b\uff0c\u5e76\u521b\u5efa\u4e24\u4e2a\u4e0d\u540c\u7684 Person \u5bf9\u8c61"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6d77\u98ce\u6559\u80b2"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 550, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ec0\u4e48\u662f\u8303\u5f0f\n\n\u7f16\u7a0b\u8303\u578b\u3001\u7f16\u7a0b\u8303\u5f0f\u6216\u7a0b\u5e8f\u8bbe\u8ba1\u6cd5\uff08\u82f1\u8bed\uff1aProgramming paradigm\uff09\uff0c\uff08\u8303\u5373\u6a21\u8303\u3001\u5178\u8303\u4e4b\u610f\uff0c\u8303\u5f0f\u5373\u6a21\u5f0f\u3001\u65b9\u6cd5\uff09\uff0c\u662f\u4e00\u7c7b\u5178\u578b\u7684\u7f16\u7a0b\u98ce\u683c\uff0c\u662f\u6307\u4ece\u4e8b\u8f6f\u4ef6\u5de5\u7a0b\u7684\u4e00\u7c7b\u5178\u578b\u7684\u98ce\u683c\uff08\u53ef\u4ee5\u5bf9\u7167\u65b9\u6cd5\u5b66\uff09\u3002\u5982\uff1a\u51fd\u6570\u5f0f\u7f16\u7a0b\u3001\u8fc7\u7a0b\u5f0f\u7f16\u7a0b\u3001\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3001\u6307\u4ee4\u5f0f\u7f16\u7a0b\u7b49\u7b49\u4e3a\u4e0d\u540c\u7684\u7f16\u7a0b\u8303\u578b\u3002\n\n\u7f16\u7a0b\u8303\u578b\u63d0\u4f9b\u4e86\uff08\u540c\u65f6\u51b3\u5b9a\u4e86\uff09\u7a0b\u5e8f\u5458\u5bf9\u7a0b\u5e8f\u6267\u884c\u7684\u770b\u6cd5\u3002\u4f8b\u5982\uff0c\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u7a0b\u5e8f\u5458\u8ba4\u4e3a\u7a0b\u5e8f\u662f\u4e00\u7cfb\u5217\u76f8\u4e92\u4f5c\u7528\u7684\u5bf9\u8c61\uff0c\u800c\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u4e00\u4e2a\u7a0b\u5e8f\u4f1a\u88ab\u770b\u4f5c\u662f\u4e00\u4e2a\u65e0\u72b6\u6001\u7684\u51fd\u6570\u8ba1\u7b97\u7684\u5e8f\u5217\u3002\n\n### JavaScript\n\nJavaScript\u00ae \uff08\u901a\u5e38\u7b80\u5199\u4e3aJS\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7684\u3001\u89e3\u91ca\u6027\u7684\u3001\u9762\u5411\u5bf9\u8c61\u7684\u5934\u7b49\u51fd\u6570\u8bed\u8a00\uff0c\u5176\u6700\u5e7f\u4e3a\u4eba\u77e5\u7684\u5e94\u7528\u662f\u4f5c\u4e3a\u7f51\u9875\u7684\u811a\u672c\u8bed\u8a00\uff0c\u4f46\u540c\u65f6\u5b83\u4e5f\u5728\u5f88\u591a\u975e\u6d4f\u89c8\u5668\u73af\u5883\u4e0b\u4f7f\u7528\u3002**JS\u662f\u4e00\u79cd\u52a8\u6001\u7684\u57fa\u4e8e\u539f\u578b\u548c\u591a\u8303\u5f0f\u7684\u811a\u672c\u8bed\u8a00\uff0c\u652f\u6301\u9762\u5411\u5bf9\u8c61\u3001\u547d\u4ee4\u5f0f\u548c\u51fd\u6570\u5f0f\u7684\u7f16\u7a0b\u98ce\u683c\u3002**", "answer": "", "biz_type": 1, "qid": 550, "subject": "", "title": "JavaScript \u662f\u4ec0\u4e48\u8303\u5f0f\u8bed\u8a00"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fae\u533b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 6, "qid": 497, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e3a\u4ec0\u4e48\u8981\u533a\u5206\u5fae\u4efb\u52a1\u548c\u5b8f\u4efb\u52a1\n\n\n\u533a\u5206\u5fae\u4efb\u52a1\u548c\u5b8f\u4efb\u52a1\u662f\u4e3a\u4e86\u5c06\u5f02\u6b65\u961f\u5217\u4efb\u52a1\u5212\u5206\u4f18\u5148\u7ea7\uff0c\u901a\u4fd7\u7684\u7406\u89e3\u5c31\u662f\u4e3a\u4e86\u63d2\u961f\u3002\n\n\u4e00\u4e2aEvent Loop\uff0cMicrotask \u662f\u5728 Macrotask \u4e4b\u540e\u8c03\u7528\uff0cMicrotask \u4f1a\u5728\u4e0b\u4e00\u4e2aEvent Loop \u4e4b\u524d\u6267\u884c\u8c03\u7528\u5b8c\uff0c\u5e76\u4e14\u5176\u4e2d\u4f1a\u5c06 Microtask \u6267\u884c\u5f53\u4e2d\u65b0\u6ce8\u518c\u7684 Microtask \u4e00\u5e76\u8c03\u7528\u6267\u884c\u5b8c\uff0c\u7136\u540e\u624d\u5f00\u59cb\u4e0b\u4e00\u6b21 Event loop\uff0c\u6240\u4ee5\u5982\u679c\u6709\u65b0\u7684 Macrotask \u5c31\u9700\u8981\u4e00\u76f4\u7b49\u5f85\uff0c\u7b49\u5230\u4e0a\u4e00\u4e2a Event loop \u5f53\u4e2d Microtask \u88ab\u6e05\u7a7a\u4e3a\u6b62\u3002\u7531\u6b64\u53ef\u89c1\uff0c \u6211\u4eec\u53ef\u4ee5\u5728\u4e0b\u4e00\u6b21 Event loop \u4e4b\u524d\u8fdb\u884c\u63d2\u961f\u3002\n\n\u5982\u679c\u4e0d\u533a\u5206 Microtask \u548c Macrotask\uff0c\u90a3\u5c31\u65e0\u6cd5\u5728\u4e0b\u4e00\u6b21 Event loop \u4e4b\u524d\u8fdb\u884c\u63d2\u961f\uff0c\u5176\u4e2d\u65b0\u6ce8\u518c\u7684\u4efb\u52a1\u5f97\u7b49\u5230\u4e0b\u4e00\u4e2a Macrotask \u5b8c\u6210\u4e4b\u540e\u624d\u80fd\u8fdb\u884c\uff0c\u8fd9\u4e2d\u95f4\u53ef\u80fd\u4f60\u9700\u8981\u7684\u72b6\u6001\u5c31\u65e0\u6cd5\u5728\u4e0b\u4e00\u4e2a Macrotask \u4e2d\u5f97\u5230\u540c\u6b65\u3002\n\n\n", "answer": "", "biz_type": 1, "qid": 497, "subject": "", "title": "JavaScript \u4e3a\u4ec0\u4e48\u8981\u533a\u5206\u5fae\u4efb\u52a1\u548c\u5b8f\u4efb\u52a1"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f1a\u5c0f\u4e8c"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 9, "qid": 609, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001\u80cc\u666f\u77e5\u8bc6\n\nJavaScript\u4e0d\u652f\u6301\u91cd\u8f7d\u7684\u8bed\u6cd5\uff0c\u5b83\u6ca1\u6709\u91cd\u8f7d\u6240\u9700\u8981\u7684\u51fd\u6570\u7b7e\u540d\u3002\nECMAScript\u51fd\u6570\u4e0d\u80fd\u50cf\u4f20\u7edf\u610f\u4e49\u4e0a\u90a3\u6837\u5b9e\u73b0\u91cd\u8f7d\u3002\u800c\u5728\u5176\u4ed6\u8bed\u8a00\uff08\u5982 Java\uff09\u4e2d\uff0c\u53ef\u4ee5\u4e3a\u4e00\u4e2a\u51fd\u6570\u7f16\u5199\u4e24\u4e2a\u5b9a\u4e49\uff0c\u53ea\u8981\u8fd9\u4e24\u4e2a\u5b9a\u4e49\u7684\u7b7e\u540d\uff08\u63a5\u53d7\u7684\u53c2\u6570\u7684\u7c7b\u578b\u548c\u6570\u91cf\uff09\u4e0d\u540c\u5373\u53ef\u3002\u5982\u524d\u6240\u8ff0\uff0cECMAScirpt\u51fd\u6570\u6ca1\u6709\u7b7e\u540d\uff0c\u56e0\u4e3a\u5176\u53c2\u6570\u662f\u7531\u5305\u542b\u96f6\u6216\u591a\u4e2a\u503c\u7684\u6570\u7ec4\u6765\u8868\u793a\u7684\u3002\u800c\u6ca1\u6709\u51fd\u6570\u7b7e\u540d\uff0c\u771f\u6b63\u7684\u91cd\u8f7d\u662f\u4e0d\u53ef\u80fd\u505a\u5230\u7684\u3002 \u2014 JavaScript\u9ad8\u7ea7\u7a0b\u5e8f\u8bbe\u8ba1\uff08\u7b2c3\u7248\uff09\n\n### \u4e8c\u3001\u4ec0\u4e48\u662f\u51fd\u6570\u91cd\u8f7d\n\n\u91cd\u8f7d\u51fd\u6570\u662f\u51fd\u6570\u7684\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\uff0c\u4e3a\u65b9\u4fbf\u4f7f\u7528\uff0c\u5141\u8bb8\u5728\u540c\u4e00\u8303\u56f4\u4e2d\u58f0\u660e\u51e0\u4e2a\u529f\u80fd\u7c7b\u4f3c\u7684\u540c\u540d\u51fd\u6570\uff0c\u4f46\u662f\u8fd9\u4e9b\u540c\u540d\u51fd\u6570\u7684\u5f62\u5f0f\u53c2\u6570\uff08\u6307\u53c2\u6570\u7684\u4e2a\u6570\u3001\u7c7b\u578b\u6216\u8005\u987a\u5e8f\uff09\u5fc5\u987b\u4e0d\u540c\uff0c\u4e5f\u5c31\u662f\u8bf4\u7528\u540c\u4e00\u4e2a\u51fd\u6570\u5b8c\u6210\u4e0d\u540c\u7684\u529f\u80fd\u3002\u8fd9\u5c31\u662f\u91cd\u8f7d\u51fd\u6570\n\n### \u4e09\u3001\u6a21\u62df\u5b9e\u73b0\n\n#### 1.\u501f\u52a9\u6d41\u7a0b\u63a7\u5236\u8bed\u53e5\n\n\u901a\u8fc7\u5224\u65ad\u4f20\u5165\u53c2\u6570\u7684\u4e2a\u6570\uff0c\u6267\u884c\u76f8\u5e94\u7684\u4ee3\u7801\u5757\u3002\n\n```js\nfunction toDo(){\n switch(arguments.length){\n case 0:\n /* \u4ee3\u7801\u57570 */\n break;\n ...\n case n:\n /* \u4ee3\u7801\u5757n */\n break;\n }\n}\n```\n\n#### 2.\u5229\u7528\u95ed\u5305\u7279\u6027\n\naddMethod\u51fd\u6570\u63a5\u65363\u4e2a\u53c2\u6570\uff1a\u76ee\u6807\u5bf9\u8c61\u3001\u76ee\u6807\u65b9\u6cd5\u540d\u3001\u51fd\u6570\u4f53\uff0c\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff1a\n\n1. \u5148\u5c06\u76ee\u6807object[name]\u7684\u503c\u5b58\u5165\u53d8\u91cfold\u4e2d\uff0c\u56e0\u6b64\u8d77\u521dold\u4e2d\u7684\u503c\u53ef\u80fd\u4e0d\u662f\u4e00\u4e2a\u51fd\u6570\uff1b\n2. \u63a5\u7740\u5411object[name]\u8d4b\u503c\u4e00\u4e2a\u4ee3\u7406\u51fd\u6570\uff0c\u5e76\u4e14\u7531\u4e8e\u53d8\u91cfold\u3001fnt\u5728\u4ee3\u7406\u51fd\u6570\u4e2d\u88ab\u5f15\u7528\uff0c\u6240\u4ee5old\u3001fnt\u5c06\u5e38\u9a7b\u5185\u5b58\u4e0d\u88ab\u56de\u6536\u3002\n\n```js\nfunction addMethod(object, name, fnt) {\n var old = object[name]; // \u4fdd\u5b58\u524d\u4e00\u4e2a\u503c\uff0c\u4ee5\u4fbf\u540e\u7eed\u8c03\u7528\n object[name] = function(){ // \u5411object[name]\u8d4b\u503c\u4e00\u4e2a\u4ee3\u7406\u51fd\u6570\n // \u5224\u65adfnt\u671f\u671b\u63a5\u6536\u7684\u53c2\u6570\u4e0e\u4f20\u5165\u53c2\u6570\u4e2a\u6570\u662f\u5426\u4e00\u81f4\n if (fnt.length === arguments.length)\n // \u82e5\u662f\uff0c\u5219\u8c03\u7528fnt\n return fnt.apply(this, arguments)\n else if (typeof old === 'function') // \u82e5\u5426\uff0c\u5219\u5224\u65adold\u7684\u503c\u662f\u5426\u4e3a\u51fd\u6570\n // \u82e5\u662f\uff0c\u5219\u8c03\u7528old\n return old.apply(this, arguments);\n };\n}\n//\u6a21\u62df\u91cd\u8f7dadd\nvar methods = {};\n//\u6dfb\u52a0\u65b9\u6cd5\uff0c\u987a\u5e8f\u65e0\u5173\naddMethod(methods, 'add', function(){return 0});\naddMethod(methods, 'add', function(a,b){return a + b});\naddMethod(methods, 'add', function(a,b,c){return a + b + c});\n//\u6267\u884c\nconsole.log(methods.add()); //0\nconsole.log(methods.add(10,20)); //30\nconsole.log(methods.add(10,20,30)); //60\n```\n\n\n\n", "answer": "", "biz_type": 1, "qid": 609, "subject": "", "title": "JavaScript \u4e2d\u5982\u4f55\u6a21\u62df\u5b9e\u73b0\u65b9\u6cd5\u7684\u91cd\u8f7d"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 5, "qid": 639, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001\u540c\u6e90\u9875\u9762\u4e4b\u95f4\u7684\u901a\u4fe1\n\n- BroadCast Channel\n- Service Worker\n- LocalStorage\n- Shared Worker\n- IndexedDB\n- window.open + window.opener\n\n### \u4e8c\u3001\u975e\u540c\u6e90\u9875\u9762\u4e4b\u95f4\u7684\u901a\u4fe1\n\n\u5bf9\u4e8e\u975e\u540c\u6e90\u9875\u9762\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5165\u540c\u6e90 iframe \u4f5c\u4e3a\u201c\u6865\u201d\uff0c\u5c06\u975e\u540c\u6e90\u9875\u9762\u901a\u4fe1\u8f6c\u6362\u4e3a\u540c\u6e90\u9875\u9762\u901a\u4fe1\u3002\n\n### \u4e09\u3001\u603b\u7ed3\n\n- \u5e7f\u64ad\u6a21\u5f0f\uff1aBroadcast Channe / Service Worker / LocalStorage + StorageEvent\n- \u5171\u4eab\u5b58\u50a8\u6a21\u5f0f\uff1aShared Worker / IndexedDB / cookie\n- \u53e3\u53e3\u76f8\u4f20\u6a21\u5f0f\uff1awindow.open + window.opener\n- \u57fa\u4e8e\u670d\u52a1\u7aef\uff1aWebsocket / Comet / SSE \u7b49", "answer": "", "biz_type": 1, "qid": 639, "subject": "", "title": "\u9664\u4e86 jsonp\u3001postmessage \u540e\u7aef\u63a7\u5236\uff0c\u600e\u4e48\u5b9e\u73b0\u8de8\u9875\u9762\u901a\u8baf"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u8611\u83c7\u8857"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 5, "qid": 529, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001\u539f\u578b\u4f18\u7f3a\u70b9\u7b80\u5355\u5206\u6790\n\n1. \u901a\u8fc7\u539f\u578b\u94fe\u7ee7\u627f\u7684\u65b9\u5f0f\uff0c\u539f\u5148\u5b58\u5728\u7236\u7c7b\u578b\u7684\u5b9e\u4f8b\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u73b0\u5728\u4e5f\u80fd\u5b58\u5728\u4e8e\u5b50\u7c7b\u578b\u7684\u539f\u578b\u4e2d\u4e86\n2. \u5728\u901a\u8fc7\u539f\u578b\u94fe\u5b9e\u73b0\u7ee7\u627f\u65f6\uff0c\u539f\u578b\u5b9e\u9645\u4e0a\u4f1a\u6210\u4e3a\u53e6\u4e00\u4e2a\u7c7b\u578b\u7684\u5b9e\u4f8b\u3002\u6240\u4ee5\u7236\u7c7b\u7684\u5b9e\u4f8b\u5c5e\u6027\u5b9e\u9645\u4e0a\u4f1a\u6210\u4e3a\u5b50\u7c7b\u7684\u539f\u578b\u5c5e\u6027\u3002\u7ed3\u679c\u5c31\u662f\u6240\u6709\u7684\u5b50\u7c7b\u7684\u5b9e\u4f8b\u90fd\u4f1a\u5171\u4eab\u7236\u7c7b\u7684\u5b9e\u4f8b\u5c5e\u6027\uff08\u5f15\u7528\u7c7b\u578b\u7684\uff09\u3002\n3. \u5728\u521b\u5efa\u5b50\u7c7b\u578b\u7684\u5b9e\u4f8b\u65f6\uff0c\u6ca1\u6709\u529e\u6cd5\u5728\u4e0d\u5f71\u54cd\u6240\u6709\u5b9e\u4f8b\u7684\u60c5\u51b5\u4e0b\uff0c\u5411\u7236\u7c7b\u578b\u7684\u6784\u9020\u51fd\u6570\u4f20\u9012\u53c2\u6570\u3002\n\n### \u4e8c\u3001\u539f\u578b\u8be6\u7ec6\u5206\u6790\n\n#### 2.1 \u539f\u578b\u7684\u597d\u5904\n\n- javascript \u91c7\u7528\u539f\u578b\u7f16\u7a0b\uff0c\u6240\u6709\u7684\u5bf9\u8c61\u90fd\u80fd\u5171\u4eab\u539f\u578b\u4e0a\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u6784\u9020\u51fd\u6570\u751f\u6210\u7684\u5b9e\u4f8b\u6240\u62e5\u6709\u7684\u65b9\u6cd5\u90fd\u6307\u5411\u4e00\u4e2a\u51fd\u6570\u7684\u7d22\u5f15\uff0c\u8fd9\u6837\u53ef\u4ee5\u8282\u7701\u5185\u5b58\uff0c\u8282\u7701\u5185\u5b58\uff0c\u5982\u679c\u4e0d\u4f7f\u7528\u539f\u578b\u6cd5\u5c31\u4f1a\u9020\u6210\u6bcf\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u5c31\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5185\u5b58\u5730\u5740\u3002\n- \u65b9\u4fbf\u5b9e\u73b0\u7ee7\u627f\n\n#### 2.2 \u539f\u578b\u94fe\n\n\u539f\u578b\u94fe\u662f\u4e00\u79cd\u673a\u5236\uff0c\u6307\u7684\u662f JavaScript \u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u5185\u7f6e\u7684 **proto** \u5c5e\u6027\u6307\u5411\u521b\u5efa\u5b83\u7684\u6784\u9020\u51fd\u6570\u7684 prototype\uff08\u539f\u578b\uff09\u5c5e\u6027\u3002\u539f\u578b\u94fe\u7684\u4f5c\u7528\u662f\u4e3a\u4e86\u5b9e\u73b0\u5bf9\u8c61\u7684\u7ee7\u627f\uff0c\u8981\u7406\u89e3\u539f\u578b\u94fe\uff0c\u9700\u8981\u5148\u4ece**\u51fd\u6570\u5bf9\u8c61\u3001constructor\u3001new\u3001prototype\u3001proto** \u8fd9\u4e94\u4e2a\u6982\u5ff5\u5165\u624b\n\n\n**2.2.1 \u51fd\u6570\u5bf9\u8c61**\n\n\n\u5728 JavaScript \u91cc\uff0c\u51fd\u6570\u5373\u5bf9\u8c61\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u968f\u610f\u64cd\u63a7\u5b83\u4eec\u3002\u6bd4\u5982\uff0c\u53ef\u4ee5\u628a\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u6216\u8005\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u7ed9\u5b83\u4eec\u8bbe\u7f6e\u5c5e\u6027\uff0c\u751a\u81f3\u8c03\u7528\u5b83\u4eec\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u793a\u4f8b\u4ee3\u7801\u5bf9\u300c\u666e\u901a\u5bf9\u8c61\u300d\u548c\u300c\u51fd\u6570\u5bf9\u8c61\u300d\u8fdb\u884c\u4e86\u533a\u5206\u3002\n\n\u666e\u901a\u5bf9\u8c61\uff1a\n\n```js\nvar o1 = {};\nvar o2 = new Object();\n```\n\n\u51fd\u6570\u5bf9\u8c61\uff1a\n\n```js\nfunction f1(){};\nvar f2 = function(){};\nvar f3 = new Function('str','console.log(str)');\n```\n\n\u7b80\u5355\u7684\u8bf4\uff0c\u51e1\u662f\u4f7f\u7528 function \u5173\u952e\u5b57\u6216 Function \u6784\u9020\u51fd\u6570\u521b\u5efa\u7684\u5bf9\u8c61\u90fd\u662f\u51fd\u6570\u5bf9\u8c61\n\n**2.2.2 constructor \u6784\u9020\u51fd\u6570**\n\n\u51fd\u6570\u8fd8\u6709\u4e00\u79cd\u7528\u6cd5\uff0c\u5c31\u662f\u628a\u5b83\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u4f7f\u7528\u3002\u50cf Object \u548c Array \u8fd9\u6837\u7684\u539f\u751f\u6784\u9020\u51fd\u6570\uff0c\u5728\u8fd0\u884c\u65f6\u4f1a\u81ea\u52a8\u51fa\u73b0\u5728\u6267\u884c\u73af\u5883\u4e2d\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u6784\u9020\u51fd\u6570\uff0c\u4ece\u800c\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7c7b\u578b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a\n\n```js\nfunction Person(name, age, job){\n this.name = name;\n this.age = age;\n this.job = job;\n this.sayName = function(){\n console.log(this.name);\n };\n}\n\nvar person1 = new Person(\"alice\", 28, \"Software Engineer\");\nvar person2 = new Person(\"Sophie\", 29, \"English Teacher\");\n```\n\n\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6784\u9020\u51fd\u6570 Person()\uff0c\u5e76\u901a\u8fc7\u8be5\u6784\u9020\u51fd\u6570\u521b\u5efa\u4e86\u4e24\u4e2a\u666e\u901a\u5bf9\u8c61 person1 \u548c person2\uff0c\u8fd9\u4e24\u4e2a\u666e\u901a\u5bf9\u8c61\u5747\u5305\u542b3\u4e2a\u5c5e\u6027\u548c1\u4e2a\u65b9\u6cd5\u3002\n\n\u4f60\u5e94\u8be5\u6ce8\u610f\u5230\u51fd\u6570\u540d Person \u4f7f\u7528\u7684\u662f\u5927\u5199\u5b57\u6bcd P\u3002\u6309\u7167\u60ef\u4f8b\uff0c\u6784\u9020\u51fd\u6570\u59cb\u7ec8\u90fd\u5e94\u8be5\u4ee5\u4e00\u4e2a\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff0c\u800c\u975e\u6784\u9020\u51fd\u6570\u5219\u5e94\u8be5\u4ee5\u4e00\u4e2a\u5c0f\u5199\u5b57\u6bcd\u5f00\u5934\u3002\u8fd9\u4e2a\u505a\u6cd5\u501f\u9274\u81ea\u5176\u4ed6\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\uff0c\u4e3b\u8981\u662f\u4e3a\u4e86\u533a\u522b\u4e8e JavaScript \u4e2d\u7684\u5176\u4ed6\u51fd\u6570\uff1b\u56e0\u4e3a\u6784\u9020\u51fd\u6570\u672c\u8eab\u4e5f\u662f\u51fd\u6570\uff0c\u53ea\u4e0d\u8fc7\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5bf9\u8c61\u800c\u5df2\u3002\n\n**2.2.3new\u64cd\u4f5c\u7b26**\n\n\u8981\u521b\u5efa Person \u7684\u65b0\u5b9e\u4f8b\uff0c\u5fc5\u987b\u4f7f\u7528 new \u64cd\u4f5c\u7b26\u3002\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u8c03\u7528\u6784\u9020\u51fd\u6570\u5b9e\u9645\u4e0a\u4f1a\u7ecf\u5386\u4ee5\u4e0b4\u4e2a\u6b65\u9aa4\uff1a\n\n- \u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61\uff1b\n- \u5c06\u6784\u9020\u51fd\u6570\u7684\u4f5c\u7528\u57df\u8d4b\u7ed9\u65b0\u5bf9\u8c61\uff08\u56e0\u6b64 this \u5c31\u6307\u5411\u4e86\u8fd9\u4e2a\u65b0\u5bf9\u8c61\uff09\uff1b\n- \u6267\u884c\u6784\u9020\u51fd\u6570\u4e2d\u7684\u4ee3\u7801\uff08\u4e3a\u8fd9\u4e2a\u65b0\u5bf9\u8c61\u6dfb\u52a0\u5c5e\u6027\uff09\uff1b\n- \u8fd4\u56de\u65b0\u5bf9\u8c61 (return)\n\n\u5c06\u6784\u9020\u51fd\u6570\u5f53\u4f5c\u51fd\u6570\n\n\u6784\u9020\u51fd\u6570\u4e0e\u5176\u4ed6\u51fd\u6570\u7684\u552f\u4e00\u533a\u522b\uff0c\u5c31\u5728\u4e8e\u8c03\u7528\u5b83\u4eec\u7684\u65b9\u5f0f\u4e0d\u540c\u3002\u4e0d\u8fc7\uff0c\u6784\u9020\u51fd\u6570\u6bd5\u7adf\u4e5f\u662f\u51fd\u6570\uff0c\u4e0d\u5b58\u5728\u5b9a\u4e49\u6784\u9020\u51fd\u6570\u7684\u7279\u6b8a\u8bed\u6cd5\u3002\u4efb\u4f55\u51fd\u6570\uff0c\u53ea\u8981\u901a\u8fc7 new \u64cd\u4f5c\u7b26\u6765\u8c03\u7528\uff0c\u90a3\u5b83\u5c31\u53ef\u4ee5\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\uff1b\u800c\u4efb\u4f55\u51fd\u6570\uff0c\u5982\u679c\u4e0d\u901a\u8fc7 new \u64cd\u4f5c\u7b26\u6765\u8c03\u7528\uff0c\u90a3\u5b83\u8ddf\u666e\u901a\u51fd\u6570\u4e5f\u4e0d\u4f1a\u6709\u4ec0\u4e48\u4e24\u6837\u3002\u4f8b\u5982\uff0c\u524d\u9762\u4f8b\u5b50\u4e2d\u5b9a\u4e49\u7684 Person() \u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u4e0b\u5217\u4efb\u4f55\u4e00\u79cd\u65b9\u5f0f\u6765\u8c03\u7528\u3002\n\n```js\n// \u5f53\u4f5c\u6784\u9020\u51fd\u6570\u4f7f\u7528\nvar person = new Person(\"alice\", 28, \"Software Engineer\");\nperson.sayName(); // \"alice\"\n\n// \u4f5c\u4e3a\u666e\u901a\u51fd\u6570\u8c03\u7528\nPerson(\"Sophie\", 29, \"English Teacher\"); // \u6dfb\u52a0\u5230 window\nwindow.sayName(); // \"Sophie\"\n\n// \u5728\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u4f5c\u7528\u57df\u4e2d\u8c03\u7528\nvar o = new Object();\nPerson.call(o, \"Tommy\", 3, \"Baby\");\no.sayName(); // \"Tommy\"\n```\n\n\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u7684\u524d\u4e24\u884c\u4ee3\u7801\u5c55\u793a\u4e86\u6784\u9020\u51fd\u6570\u7684\u5178\u578b\u7528\u6cd5\uff0c\u5373\u4f7f\u7528 new \u64cd\u4f5c\u7b26\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61\u3002\u63a5\u4e0b\u6765\u7684\u4e24\u884c\u4ee3\u7801\u5c55\u793a\u4e86\u4e0d\u4f7f\u7528 new \u64cd\u4f5c\u7b26\u8c03\u7528 Person() \u4f1a\u51fa\u73b0\u4ec0\u4e48\u7ed3\u679c\uff0c\u5c5e\u6027\u548c\u65b9\u6cd5\u90fd\u88ab\u6dfb\u52a0\u7ed9 window \u5bf9\u8c61\u4e86\u3002\u5f53\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u65f6\uff0cthis \u5bf9\u8c61\u603b\u662f\u6307\u5411 Global \u5bf9\u8c61\uff08\u5728\u6d4f\u89c8\u5668\u4e2d\u5c31\u662f window \u5bf9\u8c61\uff09\u3002\u56e0\u6b64\uff0c\u5728\u8c03\u7528\u5b8c\u51fd\u6570\u4e4b\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7 window \u5bf9\u8c61\u6765\u8c03\u7528 sayName() \u65b9\u6cd5\uff0c\u5e76\u4e14\u8fd8\u8fd4\u56de\u4e86 \"Sophie\" \u3002\u6700\u540e\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 call()\uff08\u6216\u8005 apply()\uff09\u5728\u67d0\u4e2a\u7279\u6b8a\u5bf9\u8c61\u7684\u4f5c\u7528\u57df\u4e2d\u8c03\u7528 Person() \u51fd\u6570\u3002\u8fd9\u91cc\u662f\u5728\u5bf9\u8c61 o \u7684\u4f5c\u7528\u57df\u4e2d\u8c03\u7528\u7684\uff0c\u56e0\u6b64\u8c03\u7528\u540e o \u5c31\u62e5\u6709\u4e86\u6240\u6709\u5c5e\u6027\u548c sayName() \u65b9\u6cd5\u3002\n\n\n**2.2.4\u6784\u9020\u51fd\u6570\u7684\u95ee\u9898**\n\n\u6784\u9020\u51fd\u6570\u6a21\u5f0f\u867d\u7136\u597d\u7528\uff0c\u4f46\u4e5f\u5e76\u975e\u6ca1\u6709\u7f3a\u70b9\u3002\u4f7f\u7528\u6784\u9020\u51fd\u6570\u7684\u4e3b\u8981\u95ee\u9898\uff0c\u5c31\u662f\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u8981\u5728\u6bcf\u4e2a\u5b9e\u4f8b\u4e0a\u91cd\u65b0\u521b\u5efa\u4e00\u904d\u3002\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cperson1 \u548c person2 \u90fd\u6709\u4e00\u4e2a\u540d\u4e3a sayName() \u7684\u65b9\u6cd5\uff0c\u4f46\u90a3\u4e24\u4e2a\u65b9\u6cd5\u4e0d\u662f\u540c\u4e00\u4e2a Function \u7684\u5b9e\u4f8b\u3002\u56e0\u4e3a JavaScript\n\n```js\nfunction Person(name, age, job){\n this.name = name;\n this.age = age;\n this.job = job;\n this.sayName = new Function(\"console.log(this.name)\"); // \u4e0e\u58f0\u660e\u51fd\u6570\u5728\u903b\u8f91\u4e0a\u662f\u7b49\u4ef7\u7684\n}\n```\n\n\u4ece\u8fd9\u4e2a\u89d2\u5ea6\u4e0a\u6765\u770b\u6784\u9020\u51fd\u6570\uff0c\u66f4\u5bb9\u6613\u660e\u767d\u6bcf\u4e2a Person \u5b9e\u4f8b\u90fd\u5305\u542b\u4e00\u4e2a\u4e0d\u540c\u7684 Function \u5b9e\u4f8b\uff08sayName() \u65b9\u6cd5\uff09\u3002\u8bf4\u5f97\u660e\u767d\u4e9b\uff0c\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u521b\u5efa\u51fd\u6570\uff0c\u867d\u7136\u521b\u5efa Function \u65b0\u5b9e\u4f8b\u7684\u673a\u5236\u4ecd\u7136\u662f\u76f8\u540c\u7684\uff0c\u4f46\u662f\u4e0d\u540c\u5b9e\u4f8b\u4e0a\u7684\u540c\u540d\u51fd\u6570\u662f\u4e0d\u76f8\u7b49\u7684\uff0c\u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u8bc1\u660e\u8fd9\u4e00\u70b9\u3002\n\n```js\nconsole.log(person1.sayName == person2.sayName); // false\n```\n\n\u7136\u800c\uff0c\u521b\u5efa\u4e24\u4e2a\u5b8c\u6210\u540c\u6837\u4efb\u52a1\u7684 Function \u5b9e\u4f8b\u7684\u786e\u6ca1\u6709\u5fc5\u8981\uff1b\u51b5\u4e14\u6709 this \u5bf9\u8c61\u5728\uff0c\u6839\u672c\u4e0d\u7528\u5728\u6267\u884c\u4ee3\u7801\u524d\u5c31\u628a\u51fd\u6570\u7ed1\u5b9a\u5230\u7279\u5b9a\u5bf9\u8c61\u4e0a\u9762\u3002\u56e0\u6b64\uff0c\u5927\u53ef\u50cf\u4e0b\u9762\u8fd9\u6837\uff0c\u901a\u8fc7\u628a\u51fd\u6570\u5b9a\u4e49\u8f6c\u79fb\u5230\u6784\u9020\u51fd\u6570\u5916\u90e8\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\n\n```js\nfunction Person(name, age, job){\n this.name = name;\n this.age = age;\n this.job = job;\n this.sayName = sayName;\n}\n\nfunction sayName(){\n console.log(this.name);\n}\n\nvar person1 = new Person(\"alice\", 28, \"Software Engineer\");\nvar person2 = new Person(\"Sophie\", 29, \"English Teacher\")\n```\n\n\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u628a sayName() \u51fd\u6570\u7684\u5b9a\u4e49\u8f6c\u79fb\u5230\u4e86\u6784\u9020\u51fd\u6570\u5916\u90e8\u3002\u800c\u5728\u6784\u9020\u51fd\u6570\u5185\u90e8\uff0c\u6211\u4eec\u5c06 sayName \u5c5e\u6027\u8bbe\u7f6e\u6210\u7b49\u4e8e\u5168\u5c40\u7684 sayName \u51fd\u6570\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u7531\u4e8e sayName \u5305\u542b\u7684\u662f\u4e00\u4e2a\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\uff0c\u56e0\u6b64 person1 \u548c person2 \u5bf9\u8c61\u5c31\u5171\u4eab\u4e86\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u5b9a\u4e49\u7684\u540c\u4e00\u4e2a sayName() \u51fd\u6570\u3002\u8fd9\u6837\u505a\u786e\u5b9e\u89e3\u51b3\u4e86\u4e24\u4e2a\u51fd\u6570\u505a\u540c\u4e00\u4ef6\u4e8b\u7684\u95ee\u9898\uff0c\u53ef\u662f\u65b0\u95ee\u9898\u53c8\u6765\u4e86\uff0c\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u5b9e\u9645\u4e0a\u53ea\u80fd\u88ab\u67d0\u4e2a\u5bf9\u8c61\u8c03\u7528\uff0c\u8fd9\u8ba9\u5168\u5c40\u4f5c\u7528\u57df\u6709\u70b9\u540d\u4e0d\u526f\u5b9e\u3002\u800c\u66f4\u8ba9\u4eba\u65e0\u6cd5\u63a5\u53d7\u7684\u662f\uff0c\u5982\u679c\u5bf9\u8c61\u9700\u8981\u5b9a\u4e49\u5f88\u591a\u65b9\u6cd5\uff0c\u90a3\u4e48\u5c31\u8981\u5b9a\u4e49\u5f88\u591a\u4e2a\u5168\u5c40\u51fd\u6570\uff0c\u4e8e\u662f\u6211\u4eec\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u7684\u5f15\u7528\u7c7b\u578b\u5c31\u4e1d\u6beb\u6ca1\u6709\u5c01\u88c5\u6027\u53ef\u8a00\u4e86\u3002\u597d\u5728\uff0c\u8fd9\u4e9b\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u539f\u578b\u6765\u89e3\u51b3\u3002\n\n\n**2.2.5 prototype \u539f\u578b**\n\n\u6211\u4eec\u521b\u5efa\u7684\u6bcf\u4e2a\u51fd\u6570\u90fd\u6709\u4e00\u4e2a prototype\uff08\u539f\u578b\uff09\u5c5e\u6027\u3002\u4f7f\u7528\u539f\u578b\u7684\u597d\u5904\u662f\u53ef\u4ee5\u8ba9\u6240\u6709\u5bf9\u8c61\u5b9e\u4f8b\u5171\u4eab\u5b83\u6240\u5305\u542b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4e0d\u5fc5\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5b9a\u4e49\u5bf9\u8c61\u5b9e\u4f8b\u7684\u4fe1\u606f\uff0c\u800c\u662f\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u4fe1\u606f\u76f4\u63a5\u6dfb\u52a0\u5230\u539f\u578b\u4e2d\uff0c\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\u6240\u793a\u3002\n\n```js\n function Person(){}\n\n Person.prototype.name = \"alice\";\n Person.prototype.age = 28;\n Person.prototype.job = \"Software Engineer\";\n Person.prototype.sayName = function(){\n console.log(this.name);\n };\n\n var person1 = new Person();\n person1.sayName(); // \"alice\"\n\n var person2 = new Person();\n person2.sayName(); // \"alice\"\n\n console.log(person1.sayName == person2.sayName); // true\n```\n\n\u5728\u6b64\uff0c\u6211\u4eec\u5c06 sayName() \u65b9\u6cd5\u548c\u6240\u6709\u5c5e\u6027\u76f4\u63a5\u6dfb\u52a0\u5230\u4e86 Person \u7684 prototype \u5c5e\u6027\u4e2d\uff0c\u6784\u9020\u51fd\u6570\u53d8\u6210\u4e86\u7a7a\u51fd\u6570\u3002\u5373\u4f7f\u5982\u6b64\uff0c\u4e5f\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u65b0\u5bf9\u8c61\uff0c\u800c\u4e14\u65b0\u5bf9\u8c61\u8fd8\u4f1a\u5177\u6709\u76f8\u540c\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4f46\u4e0e\u524d\u9762\u7684\u4f8b\u5b50\u4e0d\u540c\u7684\u662f\uff0c\u65b0\u5bf9\u8c61\u7684\u8fd9\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\u662f\u7531\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u7684\u3002\u6362\u53e5\u8bdd\u8bf4\uff0cperson1 \u548c person2 \u8bbf\u95ee\u7684\u90fd\u662f\u540c\u4e00\u7ec4\u5c5e\u6027\u548c\u540c\u4e00\u4e2a sayName() \u51fd\u6570\u3002\n\n\n**\u7406\u89e3\u539f\u578b\u5bf9\u8c61**,\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u539f\u578b\u5bf9\u8c61\u90fd\u4f1a\u81ea\u52a8\u83b7\u5f97\u4e00\u4e2a constructor\uff08\u6784\u9020\u51fd\u6570\uff09\u5c5e\u6027\uff0c\u8fd9\u4e2a\u5c5e\u6027\u5305\u542b\u4e00\u4e2a\u6307\u5411 prototype \u5c5e\u6027\u6240\u5728\u51fd\u6570\u7684\u6307\u9488\u3002\u5c31\u62ff\u524d\u9762\u7684\u4f8b\u5b50\u6765\u8bf4\uff0cPerson.prototype.constructor \u6307\u5411 Person\u3002\u800c\u901a\u8fc7\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u6211\u4eec\u8fd8\u53ef\u7ee7\u7eed\u4e3a\u539f\u578b\u5bf9\u8c61\u6dfb\u52a0\u5176\u4ed6\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u53ea\u6709\u5bf9\u8c61\u65b9\u6cd5\u624d\u4f1a\u6709prototype.\n\n\u867d\u7136\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u5b9e\u4f8b\u8bbf\u95ee\u4fdd\u5b58\u5728\u539f\u578b\u4e2d\u7684\u503c\uff0c\u4f46\u5374\u4e0d\u80fd\u901a\u8fc7\u5bf9\u8c61\u5b9e\u4f8b\u91cd\u5199\u539f\u578b\u4e2d\u7684\u503c\u3002\u5982\u679c\u6211\u4eec\u5728\u5b9e\u4f8b\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u5c5e\u6027\uff0c\u800c\u8be5\u5c5e\u6027\u4e0e\u5b9e\u4f8b\u539f\u578b\u4e2d\u7684\u4e00\u4e2a\u5c5e\u6027\u540c\u540d\uff0c\u90a3\u6211\u4eec\u5c31\u5728\u5b9e\u4f8b\u4e2d\u521b\u5efa\u8be5\u5c5e\u6027\uff0c\u8be5\u5c5e\u6027\u5c06\u4f1a\u5c4f\u853d\u539f\u578b\u4e2d\u7684\u90a3\u4e2a\u5c5e\u6027\u3002\n\n**\u66f4\u7b80\u5355\u7684\u539f\u578b\u8bed\u6cd5**\uff0c\u524d\u9762\u4f8b\u5b50\u4e2d\u6bcf\u6dfb\u52a0\u4e00\u4e2a\u5c5e\u6027\u548c\u65b9\u6cd5\u5c31\u8981\u6572\u4e00\u904d Person.prototype\u3002\u4e3a\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8f93\u5165\uff0c\u4e5f\u4e3a\u4e86\u4ece\u89c6\u89c9\u4e0a\u66f4\u597d\u5730\u5c01\u88c5\u539f\u578b\u7684\u529f\u80fd\uff0c\u66f4\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u7528\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5bf9\u8c61\u5b57\u9762\u91cf\u6765\u91cd\u5199\u6574\u4e2a\u539f\u578b\u5bf9\u8c61\uff0c\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\u6240\u793a\u3002\n\n\n```js\nfunction Person(){}\n\nPerson.prototype = {\n name : \"alice\",\n age : 28,\n job: \"Software Engineer\",\n sayName : function () {\n console.log(this.name);\n }\n};\n```\n\n\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06 Person.prototype \u8bbe\u7f6e\u4e3a\u7b49\u4e8e\u4e00\u4e2a\u4ee5\u5bf9\u8c61\u5b57\u9762\u91cf\u5f62\u5f0f\u521b\u5efa\u7684\u65b0\u5bf9\u8c61\u3002\u6700\u7ec8\u7ed3\u679c\u76f8\u540c\uff0c\u4f46\u6709\u4e00\u4e2a\u4f8b\u5916\uff1aconstructor \u5c5e\u6027\u4e0d\u518d\u6307\u5411 Person \u4e86\u3002\u524d\u9762\u66fe\u7ecf\u4ecb\u7ecd\u8fc7\uff0c\u6bcf\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u5c31\u4f1a\u540c\u65f6\u521b\u5efa\u5b83\u7684 prototype \u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u4e5f\u4f1a\u81ea\u52a8\u83b7\u5f97 constructor \u5c5e\u6027\u3002\u800c\u6211\u4eec\u5728\u8fd9\u91cc\u4f7f\u7528\u7684\u8bed\u6cd5\uff0c\u672c\u8d28\u4e0a\u5b8c\u5168\u91cd\u5199\u4e86\u9ed8\u8ba4\u7684 prototype \u5bf9\u8c61\uff0c\u56e0\u6b64 constructor \u5c5e\u6027\u4e5f\u5c31\u53d8\u6210\u4e86\u65b0\u5bf9\u8c61\u7684 constructor \u5c5e\u6027\uff08\u6307\u5411 Object \u6784\u9020\u51fd\u6570\uff09\uff0c\u4e0d\u518d\u6307\u5411 Person \u51fd\u6570\u3002\u6b64\u65f6\uff0c\u5c3d\u7ba1 instanceof \u64cd\u4f5c\u7b26\u8fd8\u80fd\u8fd4\u56de\u6b63\u786e\u7684\u7ed3\u679c\uff0c\u4f46\u901a\u8fc7 constructor \u5df2\u7ecf\u65e0\u6cd5\u786e\u5b9a\u5bf9\u8c61\u7684\u7c7b\u578b\u4e86\uff0c\u5982\u4e0b\u6240\u793a\n\n```js\nvar friend = new Person();\n\nconsole.log(friend instanceof Object); // true\nconsole.log(friend instanceof Person); // true\nconsole.log(friend.constructor === Person); // false\nconsole.log(friend.constructor === Object); // true\n```\n\n\u5728\u6b64\uff0c\u7528 instanceof \u64cd\u4f5c\u7b26\u6d4b\u8bd5 Object \u548c Person \u4ecd\u7136\u8fd4\u56de true\uff0c\u4f46 constructor \u5c5e\u6027\u5219\u7b49\u4e8e Object \u800c\u4e0d\u7b49\u4e8e Person \u4e86\u3002\u5982\u679c constructor \u7684\u503c\u771f\u7684\u5f88\u91cd\u8981\u3002\u9700\u8981\u91cd\u65b0\u6307\u5b9a\u4e00\u4e0bconstructor\u7684\u6307\u5411\n\n\n#### 2.3 \u539f\u578b\u7684\u52a8\u6001\u6027\n\n\u7531\u4e8e\u5728\u539f\u578b\u4e2d\u67e5\u627e\u503c\u7684\u8fc7\u7a0b\u662f\u4e00\u6b21\u641c\u7d22\uff0c\u56e0\u6b64\u6211\u4eec\u5bf9\u539f\u578b\u5bf9\u8c61\u6240\u505a\u7684\u4efb\u4f55\u4fee\u6539\u90fd\u80fd\u591f\u7acb\u5373\u4ece\u5b9e\u4f8b\u4e0a\u53cd\u6620\u51fa\u6765\uff0c\u5373\u4f7f\u662f\u5148\u521b\u5efa\u4e86\u5b9e\u4f8b\u540e\u4fee\u6539\u539f\u578b\u4e5f\u7167\u6837\u5982\u6b64\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\u3002\n\n```js\nvar friend = new Person();\n\nPerson.prototype.sayHi = function(){\n console.log(\"hi\");\n};\n\nfriend.sayHi(); // \"hi\"\uff08\u6ca1\u6709\u95ee\u9898\uff01\uff09\n```\n\n\u4ee5\u4e0a\u4ee3\u7801\u5148\u521b\u5efa\u4e86 Person \u7684\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u5728 person \u4e2d\u3002\u7136\u540e\uff0c\u4e0b\u4e00\u6761\u8bed\u53e5\u5728 Person.prototype \u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b9\u6cd5 sayHi()\u3002\u5373\u4f7f person \u5b9e\u4f8b\u662f\u5728\u6dfb\u52a0\u65b0\u65b9\u6cd5\u4e4b\u524d\u521b\u5efa\u7684\uff0c\u4f46\u5b83\u4ecd\u7136\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u4e2a\u65b0\u65b9\u6cd5\u3002\u5176\u539f\u56e0\u53ef\u4ee5\u5f52\u7ed3\u4e3a\u5b9e\u4f8b\u4e0e\u539f\u578b\u4e4b\u95f4\u7684\u677e\u6563\u8fde\u63a5\u5173\u7cfb\u3002\u5f53\u6211\u4eec\u8c03\u7528 person.sayHi() \u65f6\uff0c\u9996\u5148\u4f1a\u5728\u5b9e\u4f8b\u4e2d\u641c\u7d22\u540d\u4e3a sayHi \u7684\u5c5e\u6027\uff0c\u5728\u6ca1\u627e\u5230\u7684\u60c5\u51b5\u4e0b\uff0c\u4f1a\u7ee7\u7eed\u641c\u7d22\u539f\u578b\u3002\u56e0\u4e3a\u5b9e\u4f8b\u4e0e\u539f\u578b\u4e4b\u95f4\u7684\u8fde\u63a5\u53ea\u4e0d\u8fc7\u662f\u4e00\u4e2a\u6307\u9488\uff0c\u800c\u975e\u4e00\u4e2a\u526f\u672c\uff0c\u56e0\u6b64\u5c31\u53ef\u4ee5\u5728\u539f\u578b\u4e2d\u627e\u5230\u65b0\u7684 sayHi \u5c5e\u6027\u5e76\u8fd4\u56de\u4fdd\u5b58\u5728\u90a3\u91cc\u7684\u51fd\u6570\u3002\n\n\u5c3d\u7ba1\u53ef\u4ee5\u968f\u65f6\u4e3a\u539f\u578b\u6dfb\u52a0\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5e76\u4e14\u4fee\u6539\u80fd\u591f\u7acb\u5373\u5728\u6240\u6709\u5bf9\u8c61\u5b9e\u4f8b\u4e2d\u53cd\u6620\u51fa\u6765\uff0c\u4f46\u5982\u679c\u662f\u91cd\u5199\u6574\u4e2a\u539f\u578b\u5bf9\u8c61\uff0c\u90a3\u4e48\u60c5\u51b5\u5c31\u4e0d\u4e00\u6837\u4e86\u3002\u6211\u4eec\u77e5\u9053\uff0c\u8c03\u7528\u6784\u9020\u51fd\u6570\u65f6\u4f1a\u4e3a\u5b9e\u4f8b\u6dfb\u52a0\u4e00\u4e2a\u6307\u5411\u6700\u521d\u539f\u578b\u7684 [[Prototype]] \u6307\u9488\uff0c\u800c\u628a\u539f\u578b\u4fee\u6539\u4e3a\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u5c31\u7b49\u4e8e\u5207\u65ad\u4e86\u6784\u9020\u51fd\u6570\u4e0e\u6700\u521d\u539f\u578b\u4e4b\u95f4\u7684\u8054\u7cfb\u3002\u8bf7\u8bb0\u4f4f\uff1a\u5b9e\u4f8b\u4e2d\u7684\u6307\u9488\u4ec5\u6307\u5411\u539f\u578b\uff0c\u800c\u4e0d\u6307\u5411\u6784\u9020\u51fd\u6570\u3002\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\u3002\n\n```js\nfunction Person(){}\n\nvar friend = new Person();\n\nPerson.prototype = {\n constructor: Person,\n name : \"alice\",\n age : 28,\n job : \"Software Engineer\",\n sayName : function () {\n console.log(this.name);\n }\n};\n\nfriend.sayName(); // Uncaught TypeError: friend.sayName is not a function\n```\n\n\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5148\u521b\u5efa\u4e86 Person \u7684\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u7136\u540e\u53c8\u91cd\u5199\u4e86\u5176\u539f\u578b\u5bf9\u8c61\u3002\u7136\u540e\u5728\u8c03\u7528 friend.sayName() \u65f6\u53d1\u751f\u4e86\u9519\u8bef\uff0c\u56e0\u4e3a friend \u6307\u5411\u7684\u662f\u91cd\u5199\u524d\u7684\u539f\u578b\u5bf9\u8c61\uff0c\u5176\u4e2d\u5e76\u4e0d\u5305\u542b\u4ee5\u8be5\u540d\u5b57\u547d\u540d\u7684\u5c5e\u6027\n\n\n#### 2.4 \u539f\u578b\u5bf9\u8c61\u7684\u95ee\u9898\n\n\u539f\u578b\u6a21\u5f0f\u4e5f\u4e0d\u662f\u6ca1\u6709\u7f3a\u70b9\u3002\u9996\u5148\uff0c\u5b83\u7701\u7565\u4e86\u4e3a\u6784\u9020\u51fd\u6570\u4f20\u9012\u521d\u59cb\u5316\u53c2\u6570\u8fd9\u4e00\u73af\u8282\uff0c\u7ed3\u679c\u6240\u6709\u5b9e\u4f8b\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u90fd\u5c06\u53d6\u5f97\u76f8\u540c\u7684\u5c5e\u6027\u503c\u3002\u867d\u7136\u8fd9\u4f1a\u5728\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u5e26\u6765\u4e00\u4e9b\u4e0d\u65b9\u4fbf\uff0c\u4f46\u8fd8\u4e0d\u662f\u539f\u578b\u7684\u6700\u5927\u95ee\u9898\u3002\u539f\u578b\u6a21\u5f0f\u7684\u6700\u5927\u95ee\u9898\u662f\u7531\u5176\u5171\u4eab\u7684\u672c\u6027\u6240\u5bfc\u81f4\u7684\u3002\n\n\u539f\u578b\u4e2d\u6240\u6709\u5c5e\u6027\u662f\u88ab\u5f88\u591a\u5b9e\u4f8b\u5171\u4eab\u7684\uff0c\u8fd9\u79cd\u5171\u4eab\u5bf9\u4e8e\u51fd\u6570\u975e\u5e38\u5408\u9002\u3002\u5bf9\u4e8e\u90a3\u4e9b\u5305\u542b\u57fa\u672c\u503c\u7684\u5c5e\u6027\u5012\u4e5f\u8bf4\u5f97\u8fc7\u53bb\uff0c\u6bd5\u7adf\uff08\u5982\u524d\u9762\u7684\u4f8b\u5b50\u6240\u793a\uff09\uff0c\u901a\u8fc7\u5728\u5b9e\u4f8b\u4e0a\u6dfb\u52a0\u4e00\u4e2a\u540c\u540d\u5c5e\u6027\uff0c\u53ef\u4ee5\u9690\u85cf\u539f\u578b\u4e2d\u7684\u5bf9\u5e94\u5c5e\u6027\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u5305\u542b\u5f15\u7528\u7c7b\u578b\u503c\u7684\u5c5e\u6027\u6765\u8bf4\uff0c\u95ee\u9898\u5c31\u6bd4\u8f83\u7a81\u51fa\u4e86\u3002\u6765\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\u3002\n\n\n```js\nfunction Person(){}\n\nPerson.prototype = {\n constructor: Person,\n name : \"alice\",\n age : 28,\n job : \"Software Engineer\",\n friends : [\"ZhangSan\", \"LiSi\"],\n sayName : function () {\n console.log(this.name);\n }\n};\n\nvar person1 = new Person();\nvar person2 = new Person();\n\nperson1.friends.push(\"WangWu\");\n\nconsole.log(person1.friends); // \"ZhangSan,LiSi,WangWu\"\nconsole.log(person2.friends); // \"ZhangSan,LiSi,WangWu\"\nconsole.log(person1.friends === person2.friends); // true\n```\n\n\u5728\u6b64\uff0cPerson.prototype \u5bf9\u8c61\u6709\u4e00\u4e2a\u540d\u4e3a friends \u7684\u5c5e\u6027\uff0c\u8be5\u5c5e\u6027\u5305\u542b\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\u3002\u7136\u540e\uff0c\u521b\u5efa\u4e86 Person \u7684\u4e24\u4e2a\u5b9e\u4f8b\u3002\u63a5\u7740\uff0c\u4fee\u6539\u4e86 person1.friends \u5f15\u7528\u7684\u6570\u7ec4\uff0c\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u7531\u4e8e friends \u6570\u7ec4\u5b58\u5728\u4e8e Person.prototype \u800c\u975e person1 \u4e2d\uff0c\u6240\u4ee5\u521a\u521a\u63d0\u5230\u7684\u4fee\u6539\u4e5f\u4f1a\u901a\u8fc7 person2.friends\uff08\u4e0e person1.friends \u6307\u5411\u540c\u4e00\u4e2a\u6570\u7ec4\uff09\u53cd\u6620\u51fa\u6765\u3002\u5047\u5982\u6211\u4eec\u7684\u521d\u8877\u5c31\u662f\u50cf\u8fd9\u6837\u5728\u6240\u6709\u5b9e\u4f8b\u4e2d\u5171\u4eab\u4e00\u4e2a\u6570\u7ec4\uff0c\u90a3\u4e48\u5bf9\u8fd9\u4e2a\u7ed3\u679c\u6211\u6ca1\u6709\u8bdd\u53ef\u8bf4\u3002\u53ef\u662f\uff0c\u5b9e\u4f8b\u4e00\u822c\u90fd\u662f\u8981\u6709\u5c5e\u4e8e\u81ea\u5df1\u7684\u5168\u90e8\u5c5e\u6027\u7684\u3002\n\n#### 2.5\u6784\u9020\u51fd\u6570\u548c\u539f\u578b\u7ed3\u5408(\u5bc4\u751f\u7ec4\u5408\u7ee7\u627f)\n\n```js\nfunction inheritPrototype(subType,superType){\n var prototype = Object.create(superType.prototype);\n prototype.constructor = subType;\n subType.prototype = prototype;\n}\n\nfunction A(name) {\n this.name = name;\n}\n\nA.prototype.getName = function () {\n console.log(this.name)\n}\n\nfunction B(name, age) {\n A.call(this, name);\n this.age = age;\n this.firends = ['\u524d\u7aef', '\u8d44\u6df1'];\n}\n\ninheritPrototype(B,A)\nB.prototype.getFirends = function () {\n console.log(this.firends);\n}\n\n\nconst instance1 = new B('jingcheng', 3);\ninstance1.getName(); // jingcheng\ninstance1.firends.push('React');\nconsole.log(instance1.firends); //['\u524d\u7aef', '\u8d44\u6df1','React']\nconst instance2 = new B('yideng', 4);\ninstance2.getName(); // yideng\nconsole.log(instance2.firends); // ['\u524d\u7aef', '\u8d44\u6df1']\n\nconsole.log(instance1, instance2)\n```\n\n\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9e\u4f8b\u5c5e\u6027\u90fd\u662f\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5b9a\u4e49\u7684\uff0c\u5728\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684\u65f6\u5019\uff0c\u5c06\u5b50\u7c7b\u7684prototype.constructor\u6307\u5411\u81ea\u5df1\uff0c\u5b50\u7c7b\u518d\u5c06\u81ea\u5df1\u7684prototype\u6307\u5411\u7236\u7ea7\u7684prototype\u3002\u65b9\u6cd5getName\u662f\u5728\u539f\u578b\u4e0a\u5b9a\u4e49\u7684,\u3002\u800c\u4fee\u6539\u4e86 instance1.friends\uff08\u5411\u5176\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u5b57\u7b26\u4e32\uff09\uff0c\u5e76\u4e0d\u4f1a\u5f71\u54cd\u5230 instance1.friends\uff0c\u56e0\u4e3a\u5b83\u4eec\u5206\u522b\u5f15\u7528\u4e86\u4e0d\u540c\u7684\u6570\u7ec4\u3002\n\n\u8fd9\u79cd\u6784\u9020\u51fd\u6570\u4e0e\u539f\u578b\u6df7\u6210\u7684\u6a21\u5f0f\uff0c\u662f\u76ee\u524d\u5728 JavaScript \u4e2d\u4f7f\u7528\u6700\u5e7f\u6cdb\u3001\u8ba4\u540c\u5ea6\u6700\u9ad8\u7684\u4e00\u79cd\u521b\u5efa\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u8bf4\uff0c\u8fd9\u662f\u7528\u6765\u5b9a\u4e49\u5f15\u7528\u7c7b\u578b\u7684\u4e00\u79cd\u9ed8\u8ba4\u6a21\u5f0f\u3002\n\n\n#### 2.6 \u539f\u578b\u94fe\n\nJavaScript \u4e2d\u63cf\u8ff0\u4e86\u539f\u578b\u94fe\u7684\u6982\u5ff5\uff0c\u5e76\u5c06\u539f\u578b\u94fe\u4f5c\u4e3a\u5b9e\u73b0\u7ee7\u627f\u7684\u4e3b\u8981\u65b9\u6cd5\u3002\u5176\u57fa\u672c\u601d\u60f3\u662f\u5229\u7528\u539f\u578b\u8ba9\u4e00\u4e2a\u5f15\u7528\u7c7b\u578b\u7ee7\u627f\u53e6\u4e00\u4e2a\u5f15\u7528\u7c7b\u578b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u7b80\u5355\u56de\u987e\u4e00\u4e0b\u6784\u9020\u51fd\u6570\u3001\u539f\u578b\u548c\u5b9e\u4f8b\u7684\u5173\u7cfb\uff1a\u6bcf\u4e2a\u6784\u9020\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u539f\u578b\u5bf9\u8c61\uff0c\u539f\u578b\u5bf9\u8c61\u90fd\u5305\u542b\u4e00\u4e2a\u6307\u5411\u6784\u9020\u51fd\u6570\u7684\u6307\u9488\uff0c\u800c\u5b9e\u4f8b\u90fd\u5305\u542b\u4e00\u4e2a\u6307\u5411\u539f\u578b\u5bf9\u8c61\u7684\u5185\u90e8\u6307\u9488\u3002", "answer": "", "biz_type": 1, "qid": 529, "subject": "", "title": "\u4f7f\u7528\u539f\u578b\u6700\u5927\u7684\u597d\u5904"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 6, "qid": 660, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001\u4f5c\u7528\u57df\n\n\u4f5c\u7528\u57df\u662f\u4ec0\u4e48\u5462\uff1f\u5b83\u6307\u7684\u662f\u4f60\u7684\u53d8\u91cf\u548c\u51fd\u6570\u8fd0\u884c\u5230\u67d0\u4e2a\u5730\u65b9\u7684\u4ee3\u7801\u5904\u80fd\u5426\u88ab\u8bbf\u95ee\u5230\u3002\n\n\u4e3a\u4ec0\u4e48\u9700\u8981\u4f5c\u7528\u57df\u5462\uff1f\u4e3a\u4ec0\u4e48\u8981\u9650\u5236\u53d8\u91cf\u7684\u8bbf\u95ee\u6027\u800c\u975e\u5168\u90e8\u66b4\u9732\u5230\u516c\u5171\u57df\u4e0b\u5462\uff1f\n\n\u8fd9\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u6700\u57fa\u672c\u7684\u6982\u5ff5\u548c\u7406\u5ff5\uff1a\u9694\u79bb\u6027\uff08The Principle of Least Access\uff09\u3002\u4e3a\u4e86\u804c\u8d23\u660e\u786e\uff0c\u4f60\u53ea\u80fd\u521a\u597d\u8bbf\u95ee\u5230\u4f60\u9700\u8981\u7684\u6240\u6709\u4e1c\u897f\uff0c\u4e0d\u591a\u4e5f\u4e0d\u5c11\u3002\u9644\u5e26\u5730\uff0c\u5b83\u5e26\u6765\u4e86\u6a21\u5757\u5316\u3001\u547d\u540d\u7a7a\u95f4\u7b49\u597d\u5904\uff0c\u8ba9\u4f60\u5199\u51fa\u66f4\u6613\u9605\u8bfb\u3001\u66f4\u6613\u7ef4\u62a4\u7684\u4ee3\u7801\u3002\u53ef\u4ee5\u8bf4\uff0c\u4f5c\u7528\u57df\u662f\u8bb8\u591a\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\u90fd\u4ece\u8bed\u8a00\u5c42\u9762\u652f\u6301\u7684\u4e00\u4e2a\u7279\u6027\u3002\n\n\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u7406\u89e3\uff1a\u4f5c\u7528\u57df\u5c31\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u5730\u76d8\uff0c\u8ba9\u53d8\u91cf\u4e0d\u4f1a\u5916\u6cc4\u3001\u66b4\u9732\u51fa\u53bb\u3002\u4e5f\u5c31\u662f\u8bf4\u4f5c\u7528\u57df\u6700\u5927\u7684\u7528\u5904\u5c31\u662f\u9694\u79bb\u53d8\u91cf\uff0c\u4e0d\u540c\u4f5c\u7528\u57df\u4e0b\u540c\u540d\u53d8\u91cf\u4e0d\u4f1a\u6709\u51b2\u7a81\u3002\n\n\n### \u4e8c\u3001\u95ed\u5305\n\n\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u7684\u5f15\u7528\uff0c\u5c31\u5f62\u6210\u4e86\u4e00\u4e2a\u95ed\u5305\u3002\n\n**\u95ed\u5305:** \u5f53\u51fd\u6570\u80fd\u8bb0\u4f4f\u5e76\u8bbf\u95ee\u6240\u5728\u8bcd\u6cd5\u4f5c\u7528\u57df\uff0c\u5373\u4f7f\u51fd\u6570\u5728\u8bcd\u6cd5\u4f5c\u7528\u57df\u8303\u56f4\u4e4b\u5916\uff0c\u6b64\u65f6\u5c31\u4ea7\u751f\u4e86\u95ed\u5305\u3002\n\n\u95ed\u5305\u5c31\u662f\u80fd\u591f\u8bfb\u53d6\u5176\u4ed6\u51fd\u6570\u5185\u90e8\u53d8\u91cf\u7684\u51fd\u6570\u3002\u7531\u4e8e\u5728Javascript\u8bed\u8a00\u4e2d\uff0c\u53ea\u6709\u51fd\u6570\u5185\u90e8\u7684\u5b50\u51fd\u6570\u624d\u80fd\u8bfb\u53d6\u5c40\u90e8\u53d8\u91cf\uff0c\u56e0\u6b64\u53ef\u4ee5\u628a\u95ed\u5305\u7b80\u5355\u7406\u89e3\u6210\"\u5b9a\u4e49\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u7684\u51fd\u6570\"\u3002\u6240\u4ee5\uff0c\u5728\u672c\u8d28\u4e0a\uff0c\u95ed\u5305\u5c31\u662f\u5c06\u51fd\u6570\u5185\u90e8\u548c\u51fd\u6570\u5916\u90e8\u8fde\u63a5\u8d77\u6765\u7684\u4e00\u5ea7\u6865\u6881\u3002\n\n\u4ece\u5b9e\u73b0\u4e0a\u8bb2\uff0c\u95ed\u5305\u662f\u4e00\u6761\u8bb0\u5f55\uff0c\u5b83\u5b58\u50a8\u4e86\u4e00\u4e2a\u51fd\u6570\u4e0e\u5176\u73af\u5883\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u4e3b\u8981\u8bb0\u5f55\u4e86\uff1a\u51fd\u6570\u7684\u6bcf\u4e2a\u81ea\u7531\u53d8\u91cf\uff08\u5728\u5185\u5c42\u51fd\u6570\u4f7f\u7528\uff0c\u4f46\u5728\u5916\u5c42\u51fd\u6570\u5904\u88ab\u5b9a\u4e49\u7684\u53d8\u91cf\uff09\u4e0e\u5176\u88ab\u7ed1\u5b9a\u7684\u503c\u6216\u5f15\u7528\u4e4b\u95f4\u7684\u5173\u8054,\u4e3b\u8981\u662f\u7528\u6765\u5b9e\u73b0\u4fe1\u606f\u5c01\u88c5\u3002\n\n### \u4e09\u3001Let\u5757\u7ea7\u4f5c\u7528\u57df\n\nlet\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u53ea\u80fd\u5728\u5757\u4f5c\u7528\u57df\u91cc\u8bbf\u95ee\uff0c\u4e0d\u80fd\u8de8\u5757\u8bbf\u95ee\uff0c\u4e5f\u4e0d\u80fd\u8de8\u51fd\u6570\u8bbf\u95ee\uff0c\u65e0\u53d8\u91cf\u63d0\u5347\uff0c\u4e0d\u53ef\u4ee5\u91cd\u590d\u58f0\u660e\u3002\n\n#### 1.let \u58f0\u660e\u7684\u53d8\u91cf\u53ea\u5728\u5757\u7ea7\u4f5c\u7528\u57df\u5185\u6709\u6548\n\n```js\n'use strict';\nfunction func(args){\n if(true){\n let i = 6;\n console.log('inside: ' + i); //\u4e0d\u62a5\u9519\n }\n console.log('outside: ' + i); // \u62a5\u9519 \"i is not defined\"\n};\nfunc();\n```\n\n#### 2.\u4e0d\u5b58\u5728\u53d8\u91cf\u63d0\u5347\uff0c\u800c\u662f\u201c\u7ed1\u5b9a\u201d\u5728\u6682\u65f6\u6027\u6b7b\u533a\n\n```js\n// \u4e0d\u5b58\u5728\u53d8\u91cf\u63d0\u5347\n'use strict';\nfunction func(){\n console.log(i);\n let i;\n};\nfunc(); // \u62a5\u9519\n```\n\n\u5728let\u58f0\u660e\u53d8\u91cf\u524d\uff0c\u4f7f\u7528\u8be5\u53d8\u91cf\uff0c\u5b83\u662f\u4f1a\u62a5\u9519\u7684\uff0c\u800c\u4e0d\u662f\u50cfvar\u90a3\u6837\u4f1a\u2018\u53d8\u91cf\u63d0\u5347\u2019\u3002 \u5176\u5b9e\u8bf4let\u6ca1\u6709\u2018\u53d8\u91cf\u63d0\u5347\u2019\u7684\u7279\u6027\uff0c\u4e0d\u592a\u5bf9\u3002\u6216\u8005\u8bf4\u5b83\u63d0\u5347\u4e86\uff0c\u4f46\u662fES6\u89c4\u5b9a\u4e86\u5728let\u58f0\u660e\u53d8\u91cf\u524d\u4e0d\u80fd\u4f7f\u7528\u8be5\u53d8\u91cf\u3002\n\n```js\n'use strict';\nvar test = 1;\nfunction func(){\n console.log(test);\n let test = 2;\n};\nfunc(); // \u62a5\u9519\n```\n\n\u5982\u679clet\u58f0\u660e\u7684\u53d8\u91cf\u6ca1\u6709\u53d8\u91cf\u63d0\u5347\uff0c\u5e94\u8be5\u6253\u5370\u20191\u2019\uff08func\u51fd\u6570\u5916\u7684test\uff09\uff1b\u800c\u5b83\u5374\u62a5\u9519\uff0c\u8bf4\u660e\u5b83\u662f\u63d0\u5347\u4e86\u7684\uff0c\u53ea\u662f\u89c4\u5b9a\u4e86\u4e0d\u80fd\u5728\u5176\u58f0\u660e\u4e4b\u524d\u4f7f\u7528\u800c\u5df2\u3002\u6211\u4eec\u79f0\u8fd9\u7279\u6027\u53eb\u201c\u6682\u65f6\u6027\u6b7b\u533a\uff08temporal dead zone\uff09\u201d\u3002\u4e14\u8fd9\u4e00\u7279\u6027\uff0c\u4ec5\u5bf9\u9075\u5faa\u2018\u5757\u7ea7\u4f5c\u7528\u57df\u2019\u7684\u547d\u4ee4\u6709\u6548\uff08let\u3001const\uff09\u3002\n\n### \u56db\u3001Const\n\nconst\u7528\u6765\u5b9a\u4e49\u5e38\u91cf\uff0c\u4f7f\u7528\u65f6\u5fc5\u987b\u521d\u59cb\u5316(\u5373\u5fc5\u987b\u8d4b\u503c)\uff0c\u53ea\u80fd\u5728\u5757\u4f5c\u7528\u57df\u91cc\u8bbf\u95ee\uff0c\u800c\u4e14\u4e0d\u80fd\u4fee\u6539\uff0c\u65e0\u53d8\u91cf\u63d0\u5347\uff0c\u4e0d\u53ef\u4ee5\u91cd\u590d\u58f0\u660e\u3002\n\nconst \u4e0e let \u7684\u4f7f\u7528\u89c4\u8303\u4e00\u6837\uff0c\u4e0e\u4e4b\u4e0d\u540c\u7684\u662f\uff1aconst \u58f0\u660e\u7684\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u4e14\u8fd9\u4e2a\u5e38\u91cf\u5fc5\u987b\u8d4b\u503c\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002\n\n**\u6ce8\u610f:** const\u5e38\u91cf\uff0c\u6307\u7684\u662f\u5e38\u91cf\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\u4e0d\u5f97\u6539\u53d8\uff0c\u800c\u4e0d\u662f\u5bf9\u5e94\u7684\u503c\u4e0d\u5f97\u6539\u53d8\uff0c\u6240\u6709\u628a\u5e94\u7528\u7c7b\u578b\u7684\u6570\u636e\u8bbe\u7f6e\u4e3a\u5e38\u91cf\uff0c\u5176\u5185\u90e8\u7684\u503c\u662f\u53ef\u4ee5\u6539\u53d8\u7684\uff0c\u4f8b\u5982\uff1aconst a={}; a.b=13;//\u4e0d\u4f1a\u62a5\u9519 const arr=[]; arr.push(123);//\u4e0d\u4f1a\u62a5\u9519\n\n```js\n'use strict';\nfunction func(){\n const PI;\n PI = 3.14;\n console.log(PI);\n};\nfunc(); // \u62a5\u9519\u201cMissing initializer in const declaration\u201d\n```\n\n", "answer": "", "biz_type": 1, "qid": 660, "subject": "", "title": "\u5bf9\u4f5c\u7528\u57df\u548c\u95ed\u5305\u7684\u7406\u89e3\uff0c\u89e3\u91ca\u4e0b let \u548c const \u7684\u5757\u7ea7\u4f5c\u7528\u57df"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9177\u5bb6\u4e50"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 6, "qid": 536, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001\u4efb\u52a1\u673a\u5236\n\n\u4ecb\u7ecd\u5b8f\u4efb\u52a1\u5fae\u4efb\u52a1\u4e4b\u524d\u9700\u8981\u5148\u4e86\u89e3\u4efb\u52a1\u6267\u884c\u673a\u5236\u3002\n\nJavaScript\u662f\u5355\u7ebf\u7a0b\u8bed\u8a00\u3002JavaScript\u4efb\u52a1\u9700\u8981\u6392\u961f\u987a\u5e8f\u6267\u884c\uff0c\u5982\u679c\u4e00\u4e2a\u4efb\u52a1\u8017\u65f6\u8fc7\u957f\uff0c\u540e\u8fb9\u4e00\u4e2a\u4efb\u52a1\u4e5f\u7684\u7b49\u7740\uff0c\u4f46\u662f\uff0c\u5047\u5982\u6211\u4eec\u9700\u8981\u6d4f\u89c8\u65b0\u95fb\uff0c\u4f46\u65b0\u95fb\u5305\u542b\u7684\u8d85\u6e05\u56fe\u7247\u52a0\u8f7d\u5f88\u6162\uff0c\u603b\u4e0d\u80fd\u7f51\u9875\u4e00\u76f4\u5361\u7740\u76f4\u5230\u56fe\u7247\u5b8c\u5168\u51fa\u6765\uff0c\u6240\u4ee5\u5c06\u4efb\u52a1\u8bbe\u8ba1\u6210\u4e86\u4e24\u7c7b\uff1a**\u540c\u6b65\u4efb\u52a1** \u548c **\u5f02\u6b65\u4efb\u52a1**\n\n![\u4efb\u52a1\u673a\u5236](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-536-task.png)\n\n\u540c\u6b65\u548c\u5f02\u6b65\u4efb\u52a1\u5206\u522b\u8fdb\u5165\u4e0d\u540c\u7684\u6267\u884c\u201c\u573a\u6240\u201d\uff0c\u540c\u6b65\u8fdb\u5165\u4e3b\u7ebf\u7a0b\uff0c\u5f02\u6b65\u8fdb\u5165Event Table\u5e76\u6ce8\u518c\u51fd\u6570\u3002\u5f53\u6307\u5b9a\u7684\u989d\u4e8b\u60c5\u5b8c\u6210\u65f6\uff0cEvent Table \u4f1a\u5c06\u8fd9\u4e2a\u51fd\u6570\u79fb\u5165Event Queue\u3002\u4e3b\u7ebf\u7a0b\u5185\u7684\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\uff0c\u4f1a\u53bbEvent Queue\u8bfb\u53d6\u5bf9\u5e94\u7684\u51fd\u6570\uff0c\u8fdb\u5165\u4e3b\u7ebf\u7a0b\u3002\n\n\u4e0a\u8ff0\u8fc7\u7a0b\u4f1a\u4e0d\u65ad\u91cd\u590d\uff0c\u4e5f\u5c31\u662f\u5e38\u8bf4\u7684Event Loop\uff08\u4e8b\u4ef6\u5faa\u73af\uff09\u3002\n\n### \u4e8c\u3001\u5f02\u6b65\u4efb\u52a1\u4e2d\u7684\u5b8f\u4efb\u52a1\u5fae\u4efb\u52a1\n\n![task](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-536-async.png)\n\n#### 1.\u5fae\u4efb\u52a1 \uff08microtask\uff09\n\n\u5f53\u524d\uff08\u6b64\u6b21\u4e8b\u4ef6\u5faa\u73af\u4e2d\uff09\u5b8f\u4efb\u52a1\u6267\u884c\u5b8c\uff0c\u5728\u4e0b\u4e00\u4e2a\u5b8f\u4efb\u52a1\u5f00\u59cb\u4e4b\u524d\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1,\u53ef\u4ee5\u7406\u89e3\u4e3a\u56de\u8c03\u4e8b\u4ef6\u3002\n\n\u5b8f\u4efb\u52a1\u4e2d\u7684\u4e8b\u4ef6\u653e\u5728callback queue\u4e2d\uff0c\u7531\u4e8b\u4ef6\u89e6\u53d1\u7ebf\u7a0b\u7ef4\u62a4\uff1b\u5fae\u4efb\u52a1\u7684\u4e8b\u4ef6\u653e\u5728\u5fae\u4efb\u52a1\u961f\u5217\u4e2d\uff0c\u7531js\u5f15\u64ce\u7ebf\u7a0b\u7ef4\u62a4\u3002\n\n- Promise\n- Object.observe(\u5df2\u7ecf\u5e9f\u5f03)\n- MutationObserver\n- process.nextTick(nodejs)\n\n\n#### 2.\u5b8f\u4efb\u52a1\uff08macrotask\uff09\n\n\u5f53\u524d\u8c03\u7528\u6808\u4e2d\u6267\u884c\u7684\u4ee3\u7801\u6210\u4e3a\u5b8f\u4efb\u52a1\u3002\n\n- \u4e3b\u4ee3\u7801\u5757\n- setTimeout\n- setInterval\n- I/O\uff08ajax\uff09\n- UI rendering\n- setImmediate(nodejs) \n- \u53ef\u4ee5\u770b\u5230\uff0c\u4e8b\u4ef6\u961f\u5217\u4e2d\u7684\u6bcf\u4e00\u4e2a\u4e8b\u4ef6\u90fd\u662f\u4e00\u4e2a macrotask\uff0c\u73b0\u5728\u79f0\u4e4b\u4e3a\u5b8f\u4efb\u52a1\u961f\u5217", "answer": "", "biz_type": 1, "qid": 536, "subject": "", "title": "\u4ecb\u7ecd\u5b8f\u4efb\u52a1\u548c\u5fae\u4efb\u52a1"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6d77\u5eb7\u5a01\u89c6"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 5, "qid": 524, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001\u4ee3\u7801\u6267\u884c\u7684\u9519\u8bef\u6355\u83b7\n\n**1.try\u2026\u2026catch**\n\n- \u80fd\u6355\u83b7\u5230\u4ee3\u7801\u6267\u884c\u7684\u9519\u8bef\n- \u6355\u83b7\u4e0d\u5230\u8bed\u6cd5\u7684\u9519\u8bef\n- \u65e0\u6cd5\u5904\u7406\u5f02\u6b65\u4e2d\u7684\u9519\u8bef\n- \u4f7f\u7528try... catch \u5305\u88f9\uff0c\u5f71\u54cd\u4ee3\u7801\u53ef\u8bfb\u6027\n\n**2.window.onerror**\n\n- \u65e0\u8bba\u662f\u5f02\u6b65\u8fd8\u662f\u975e\u5f02\u6b65\u9519\u8bef\uff0c`onerror` \u90fd\u80fd\u6355\u83b7\u5230\u8fd0\u884c\u65f6\u9519\u8bef\n- `onerror` \u4e3b\u8981\u662f\u6765\u6355\u83b7\u9884\u6599\u4e4b\u5916\u7684\u9519\u8bef\uff0c\u800c `try-catch` \u5219\u662f\u7528\u6765\u5728\u53ef\u9884\u89c1\u60c5\u51b5\u4e0b\u76d1\u63a7\u7279\u5b9a\u7684\u9519\u8bef\uff0c\u4e24\u8005\u7ed3\u5408\u4f7f\u7528\u66f4\u52a0\u9ad8\u6548\u3002\n- `window.onerror` \u51fd\u6570\u53ea\u6709\u5728\u8fd4\u56de true \u7684\u65f6\u5019\uff0c\u5f02\u5e38\u624d\u4e0d\u4f1a\u5411\u4e0a\u629b\u51fa\uff0c\u5426\u5219\u5373\u4f7f\u662f\u77e5\u9053\u5f02\u5e38\u7684\u53d1\u751f\u63a7\u5236\u53f0\u8fd8\u662f\u4f1a\u663e\u793a `Uncaught Error: xxxxx`\u3002\n- \u5f53\u6211\u4eec\u9047\u5230 `<img src=\"./404.png\">` \u62a5 404 \u7f51\u7edc\u8bf7\u6c42\u5f02\u5e38\u7684\u65f6\u5019\uff0conerror \u662f\u65e0\u6cd5\u5e2e\u52a9\u6211\u4eec\u6355\u83b7\u5230\u5f02\u5e38\u7684\u3002\n\n> **\u7f3a\u70b9:** \u76d1\u542c\u4e0d\u5230\u8d44\u6e90\u52a0\u8f7d\u7684\u62a5\u9519onerror,\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u53ea\u80fd\u58f0\u660e\u4e00\u6b21\uff0c\u4e0d\u4f1a\u91cd\u590d\u6267\u884c\u591a\u4e2a\u56de\u8c03\uff1a\n\n**3.window.addEventListener('error')**\n\n\u53ef\u4ee5\u76d1\u542c\u5230\u8d44\u6e90\u52a0\u8f7d\u62a5\u9519\uff0c\u4e5f\u53ef\u4ee5\u6ce8\u518c\u591a\u4e2a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002\n\n`window.addEventListener('error',(msg, url, row, col, error) => {}, true)`\n\n\u4f46\u662f\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u53ef\u4ee5\u6355\u6349\u5230\u7f51\u7edc\u8bf7\u6c42\u7684\u5f02\u5e38,\u5374\u65e0\u6cd5\u5224\u65ad HTTP \u7684\u72b6\u6001\u662f 404 \u8fd8\u662f\u5176\u4ed6\u6bd4\u5982 500 \u7b49\u7b49\uff0c\u6240\u4ee5\u8fd8\u9700\u8981\u914d\u5408\u670d\u52a1\u7aef\u65e5\u5fd7\u624d\u8fdb\u884c\u6392\u67e5\u5206\u6790\u624d\u53ef\u4ee5\u3002\n\n**4.window.addEventListener('unhandledrejection')**\n\n\u6355\u83b7Promise\u9519\u8bef\uff0c\u5f53Promise \u88ab reject \u4e14\u6ca1\u6709 reject \u5904\u7406\u5668\u7684\u65f6\u5019\uff0c\u4f1a\u89e6\u53d1 `unhandledrejection` \u4e8b\u4ef6\uff1b\u8fd9\u53ef\u80fd\u53d1\u751f\u5728 window \u4e0b\uff0c\u4f46\u4e5f\u53ef\u80fd\u53d1\u751f\u5728 Worker \u4e2d\u3002 \u8fd9\u5bf9\u4e8e\u8c03\u8bd5\u56de\u9000\u9519\u8bef\u5904\u7406\u975e\u5e38\u6709\u7528\u3002\n\n### \u4e8c\u3001\u8d44\u6e90\u52a0\u8f7d\u7684\u9519\u8bef\u6355\u83b7\n\n1. `imgObj.onerror()`\n2. `performance.getEntries()`\uff0c\u83b7\u53d6\u5230\u6210\u529f\u52a0\u8f7d\u7684\u8d44\u6e90\uff0c\u5bf9\u6bd4\u53ef\u4ee5\u95f4\u63a5\u7684\u6355\u83b7\u9519\u8bef\n3. `window.addEventListener('error', fn, true)`, \u4f1a\u6355\u83b7\u4f46\u662f\u4e0d\u5192\u6ce1\uff0c\u6240\u4ee5window.onerror \u4e0d\u4f1a\u89e6\u53d1\uff0c\u6355\u83b7\u9636\u6bb5\u53ef\u4ee5\u89e6\u53d1\n\n### \u4e09\u3001Vue\u3001React\u4e2d\n\nVue\u6709 `errorHandler`\uff0cReact\u6709 `componentDidCatch` \u8fdb\u884c\u9519\u8bef\u6355\u83b7", "answer": "", "biz_type": 1, "qid": 524, "subject": "", "title": "\u5982\u4f55\u5904\u7406\u5f02\u5e38\u6355\u83b7"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fae\u533b", "\u7384\u6b66\u79d1\u6280", "\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 6, "qid": 492, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001JavaScript\u5168\u90e8\u6570\u636e\u7c7b\u578b\n\n#### 1.\u5185\u7f6e\u7c7b\u578b\n\n1. **\u7a7a\u503c** null\n2. **\u672a\u5b9a\u4e49** undefined\n3. **\u5e03\u5c14\u503c** boolean\n4. **\u6570\u5b57** Number\n5. **\u5b57\u7b26\u4e32** String\n6. **\u5bf9\u8c61** Object\n7. **\u7b26\u53f7** symbol\n8. **\u957f\u6574\u578b** BigInt\n\n#### 1.\u57fa\u672c\u6570\u636e\u7c7b\u578b\n\nundefined,null,number,boolean,string,symbol\u3002\n\n\u57fa\u672c\u6570\u636e\u7c7b\u578b\u662f\u6309\u503c\u8bbf\u95ee\u7684\uff0c\u5c31\u662f\u8bf4\u6211\u4eec\u53ef\u4ee5\u64cd\u4f5c\u4fdd\u5b58\u5728\u53d8\u91cf\u4e2d\u7684\u5b9e\u9645\u7684\u503c\u3002\n\n1. \u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u503c\u662f\u4e0d\u53ef\u53d8\u7684\n2. \u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0d\u53ef\u4ee5\u6dfb\u52a0\u5c5e\u6027\u548c\u65b9\u6cd5\n3. \u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u8d4b\u503c\u662f\u7b80\u5355\u8d4b\u503c\n4. \u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u6bd4\u8f83\u662f\u503c\u7684\u6bd4\u8f83\n5. \u57fa\u672c\u6570\u636e\u7c7b\u578b\u662f\u5b58\u653e\u5728\u6808\u533a\u7684\n\n#### 2.\u5f15\u7528\u7c7b\u578b\n\nJavaScript\u4e2d\u9664\u4e86\u4e0a\u9762\u7684\u57fa\u672c\u7c7b\u578b\u4e4b\u5916\u5c31\u662f\u5f15\u7528\u7c7b\u578b\u4e86\uff0c\u4e5f\u53ef\u4ee5\u8bf4\u5c31\u662f\u5bf9\u8c61\u4e86\uff0c\u6bd4\u5982\uff1aObject,Array,Function,Data\u7b49\n\n1. \u5f15\u7528\u7c7b\u578b\u7684\u503c\u662f\u53ef\u4ee5\u6539\u53d8\u7684\n2. \u5f15\u7528\u7c7b\u578b\u53ef\u4ee5\u6dfb\u52a0\u5c5e\u6027\u548c\u65b9\u6cd5\n3. \u5f15\u7528\u7c7b\u578b\u7684\u8d4b\u503c\u662f\u5bf9\u8c61\u5f15\u7528\n4. \u5f15\u7528\u7c7b\u578b\u7684\u6bd4\u8f83\u662f\u5f15\u7528\u7684\u6bd4\u8f83\n5. \u5f15\u7528\u7c7b\u578b\u662f\u540c\u65f6\u5b58\u5728\u6808\u533a\u548c\u5806\u533a\u7684\n\n### \u4e8c\u3001\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5f15\u7528\u6570\u636e\u7c7b\u578b\u7684\u533a\u522b\n\n#### 1.\u58f0\u660e\u53d8\u91cf\u65f6\u4e0d\u540c\u7684\u5185\u5b58\u5206\u914d\n\n**\u539f\u59cb\u503c:** \u5b58\u50a8\u5728\u6808\uff08stack\uff09\u4e2d\u7684\u7b80\u5355\u6570\u636e\u6bb5\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u7684\u503c\u76f4\u63a5\u5b58\u50a8\u5728\u53d8\u91cf\u8bbf\u95ee\u7684\u4f4d\u7f6e\u3002\u8fd9\u662f\u56e0\u4e3a\u8fd9\u4e9b\u539f\u59cb\u7c7b\u578b\u5360\u636e\u7684\u7a7a\u95f4\u662f\u56fa\u5b9a\u7684\uff0c\u6240\u4ee5\u53ef\u5c06\u4ed6\u4eec\u5b58\u50a8\u5728\u8f83\u5c0f\u7684\u5185\u5b58\u533a\u57df \u2013 \u6808\u4e2d\u3002\u8fd9\u6837\u5b58\u50a8\u4fbf\u4e8e\u8fc5\u901f\u67e5\u5bfb\u53d8\u91cf\u7684\u503c\u3002\n\n**\u5f15\u7528\u503c:** \u5b58\u50a8\u5728\u5806\uff08heap\uff09\u4e2d\u7684\u5bf9\u8c61\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b58\u50a8\u5728\u53d8\u91cf\u5904\u7684\u503c\u662f\u4e00\u4e2a\u6307\u9488\uff08point\uff09\uff0c\u6307\u5411\u5b58\u50a8\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u3002\u8fd9\u662f\u56e0\u4e3a\uff1a\u5f15\u7528\u503c\u7684\u5927\u5c0f\u4f1a\u6539\u53d8\uff0c\u6240\u4ee5\u4e0d\u80fd\u628a\u5b83\u653e\u5728\u6808\u4e2d\uff0c\u5426\u5219\u4f1a\u964d\u4f4e\u53d8\u91cf\u67e5\u5bfb\u7684\u901f\u5ea6\u3002\u76f8\u53cd\uff0c\u653e\u5728\u53d8\u91cf\u7684\u6808\u7a7a\u95f4\u4e2d\u7684\u503c\u662f\u8be5\u5bf9\u8c61\u5b58\u50a8\u5728\u5806\u4e2d\u7684\u5730\u5740\u3002\u5730\u5740\u7684\u5927\u5c0f\u662f\u56fa\u5b9a\u7684\uff0c\u6240\u4ee5\u628a\u5b83\u5b58\u50a8\u5728\u6808\u4e2d\u5bf9\u53d8\u91cf\u6027\u80fd\u65e0\u4efb\u4f55\u8d1f\u9762\u5f71\u54cd\u3002 \u2003\u2003\u2003\n\n#### 2.\u4e0d\u540c\u7684\u5185\u5b58\u5206\u914d\u673a\u5236\u4e5f\u5e26\u6765\u4e86\u4e0d\u540c\u7684\u8bbf\u95ee\u673a\u5236\n\n\u5728javascript\u4e2d\u662f\u4e0d\u5141\u8bb8\u76f4\u63a5\u8bbf\u95ee\u4fdd\u5b58\u5728\u5806\u5185\u5b58\u4e2d\u7684\u5bf9\u8c61\u7684\uff0c\u6240\u4ee5\u5728\u8bbf\u95ee\u4e00\u4e2a\u5bf9\u8c61\u65f6,\u9996\u5148\u5f97\u5230\u7684\u662f\u8fd9\u4e2a\u5bf9\u8c61\u5728\u5806\u5185\u5b58\u4e2d\u7684\u5730\u5740\uff0c\u7136\u540e\u518d\u6309\u7167\u8fd9\u4e2a\u5730\u5740\u53bb\u83b7\u5f97\u8fd9\u4e2a\u5bf9\u8c61\u4e2d\u7684\u503c\uff0c\u8fd9\u5c31\u662f\u4f20\u8bf4\u4e2d\u7684\u6309\u5f15\u7528\u8bbf\u95ee\u3002\n \u2003\u2003 \u2003\n\u800c\u539f\u59cb\u7c7b\u578b\u7684\u503c\u5219\u662f\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5230\u7684\u3002 \u2003\u2003 \u2003\u2003\n\n#### 3.\u590d\u5236\u53d8\u91cf\u65f6\u7684\u4e0d\u540c\n\n**\u539f\u59cb\u503c:** \u5728\u5c06\u4e00\u4e2a\u4fdd\u5b58\u7740\u539f\u59cb\u503c\u7684\u53d8\u91cf\u590d\u5236\u7ed9\u53e6\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u4f1a\u5c06\u539f\u59cb\u503c\u7684\u526f\u672c\u8d4b\u503c\u7ed9\u65b0\u53d8\u91cf\uff0c\u6b64\u540e\u8fd9\u4e24\u4e2a\u53d8\u91cf\u662f\u5b8c\u5168\u72ec\u7acb\u7684\uff0c\u4ed6\u4eec\u53ea\u662f\u62e5\u6709\u76f8\u540c\u7684value\u800c\u5df2\u3002 \u2003\u2003\n\n**\u5f15\u7528\u503c:** \u5728\u5c06\u4e00\u4e2a\u4fdd\u5b58\u7740\u5bf9\u8c61\u5185\u5b58\u5730\u5740\u7684\u53d8\u91cf\u590d\u5236\u7ed9\u53e6\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u4f1a\u628a\u8fd9\u4e2a\u5185\u5b58\u5730\u5740\u8d4b\u503c\u7ed9\u65b0\u53d8\u91cf\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fd9\u4e24\u4e2a\u53d8\u91cf\u90fd\u6307\u5411\u4e86\u5806\u5185\u5b58\u4e2d\u7684\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4ed6\u4eec\u4e2d\u4efb\u4f55\u4e00\u4e2a\u4f5c\u51fa\u7684\u6539\u53d8\u90fd\u4f1a\u53cd\u6620\u5728\u53e6\u4e00\u4e2a\u8eab\u4e0a\u3002\uff08\u8fd9\u91cc\u8981\u7406\u89e3\u7684\u4e00\u70b9\u5c31\u662f\uff0c\u590d\u5236\u5bf9\u8c61\u65f6\u5e76\u4e0d\u4f1a\u5728\u5806\u5185\u5b58\u4e2d\u65b0\u751f\u6210\u4e00\u4e2a\u4e00\u6a21\u4e00\u6837\u7684\u5bf9\u8c61\uff0c\u53ea\u662f\u591a\u4e86\u4e00\u4e2a\u4fdd\u5b58\u6307\u5411\u8fd9\u4e2a\u5bf9\u8c61\u6307\u9488\u7684\u53d8\u91cf\u7f62\u4e86\uff09\u3002\u591a\u4e86\u4e00\u4e2a\u6307\u9488\n\n#### 4.\u53c2\u6570\u4f20\u9012\u7684\u4e0d\u540c\uff08\u628a\u5b9e\u53c2\u590d\u5236\u7ed9\u5f62\u53c2\u7684\u8fc7\u7a0b\uff09\n\n\u9996\u5148\u6211\u4eec\u5e94\u8be5\u660e\u786e\u4e00\u70b9\uff1aECMAScript\u4e2d\u6240\u6709\u51fd\u6570\u7684\u53c2\u6570\u90fd\u662f\u6309\u503c\u6765\u4f20\u9012\u7684\u3002 \u2003\u2003\n\n\u4f46\u662f\u4e3a\u4ec0\u4e48\u6d89\u53ca\u5230\u539f\u59cb\u7c7b\u578b\u4e0e\u5f15\u7528\u7c7b\u578b\u7684\u503c\u65f6\u4ecd\u7136\u6709\u533a\u522b\u5462\uff1f\u8fd8\u4e0d\u5c31\u662f\u56e0\u4e3a\u5185\u5b58\u5206\u914d\u65f6\u7684\u5dee\u522b\u3002 \n\u2003\u2003\n**\u539f\u59cb\u503c:** \u53ea\u662f\u628a\u53d8\u91cf\u91cc\u7684\u503c\u4f20\u9012\u7ed9\u53c2\u6570\uff0c\u4e4b\u540e\u53c2\u6570\u548c\u8fd9\u4e2a\u53d8\u91cf\u4e92\u4e0d\u5f71\u54cd\u3002 \u2003\u2003\n\n**\u5f15\u7528\u503c:** \u5bf9\u8c61\u53d8\u91cf\u5b83\u91cc\u9762\u7684\u503c\u662f\u8fd9\u4e2a\u5bf9\u8c61\u5728\u5806\u5185\u5b58\u4e2d\u7684\u5185\u5b58\u5730\u5740\uff0c\u8fd9\u4e00\u70b9\u4f60\u8981\u65f6\u523b\u94ed\u8bb0\u5728\u5fc3\uff01\u56e0\u6b64\u5b83\u4f20\u9012\u7684\u503c\u4e5f\u5c31\u662f\u8fd9\u4e2a\u5185\u5b58\u5730\u5740\uff0c\u8fd9\u4e5f\u5c31\u662f\u4e3a\u4ec0\u4e48\u51fd\u6570\u5185\u90e8\u5bf9\u8fd9\u4e2a\u53c2\u6570\u7684\u4fee\u6539\u4f1a\u4f53\u73b0\u5728\u5916\u90e8\u7684\u539f\u56e0\u4e86\uff0c\u56e0\u4e3a\u5b83\u4eec\u90fd\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002", "answer": "", "biz_type": 1, "qid": 492, "subject": "", "title": "\u4ecb\u7ecd JS \u5168\u90e8\u6570\u636e\u7c7b\u578b\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5f15\u7528\u6570\u636e\u7c7b\u578b\u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 664, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001 `if`\u5224\u65ad\u539f\u7406\n\n`if`\u5224\u65ad\u539f\u7406\u662f\u548c`Boolean()`\u8fd9\u4e2a\u51fd\u6570\u6709\u5173\u7684\uff0c\u6240\u4ee5`if(***)`\u4e2d`***`\u7684\u5224\u65ad\u7ed3\u679c\u53d6\u51b3\u4e8e`Boolean(***)`\u7684\u7ed3\u679c\uff0c\u90a3\u4e48`Boolean()`\u51fd\u6570\u5224\u65ad\u7684\u89c4\u5219\u662f\u4ec0\u4e48\u5462\uff1f\n\nBoolean()\u51fd\u6570\u5224\u65ad\u89c4\u5219\n\n- \u6570\u5b57\u7c7b\u578b\uff1aNaN\u30010\u7ed3\u679c\u4e3afalse\uff0c\u5176\u4ed6\u60c5\u51b5\u4e3atrue\n- \u5b57\u7b26\u4e32\u7c7b\u578b\uff1a\u7a7a\u5b57\u7b26\u4e32\u4e3afalse\uff0c\u5176\u4ed6\u60c5\u51b5\u4e3atrue\n- Boolean\u7c7b\u578b\uff1afalse\u4e3afalse\uff0ctrue\u4e3atrue\n- \u5bf9\u8c61\u7c7b\u578b\uff1aundefined\u3001null\u4e3afalse\uff0c\u5176\u4ed6\u60c5\u51b5\u4e3atrue\n\n### \u4e8c\u3001 \u5bbd\u677e\u76f8\u7b49(==)\u6bd4\u8f83\u65f6\u7684\u9690\u58eb\u8f6c\u6362\u89c4\u5219\n\n\u5bbd\u677e\u76f8\u7b49\uff08==\uff09\u548c\u4e25\u683c\u76f8\u7b49\uff08===\uff09\u7684\u533a\u522b\u5728\u4e8e\u5bbd\u677e\u76f8\u7b49\u4f1a\u5728\u6bd4\u8f83\u4e2d\u8fdb\u884c\u9690\u5f0f\u8f6c\u6362\u3002\n\n1\uff09\u5e03\u5c14\u7c7b\u578b\u548c\u5176\u4ed6\u7c7b\u578b\u7684\u76f8\u7b49\u6bd4\u8f83\uff0c\u53ea\u8981\u5e03\u5c14\u7c7b\u578b\u53c2\u4e0e\u6bd4\u8f83\uff0c\u8be5\u5e03\u5c14\u7c7b\u578b\u7684\u503c\u9996\u5148\u4f1a\u88ab\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\n\n2\uff09\u6570\u5b57\u7c7b\u578b\u548c\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u76f8\u7b49\u6bd4\u8f83\uff0c\u5f53\u6570\u5b57\u7c7b\u578b\u548c\u5b57\u7b26\u4e32\u7c7b\u578b\u505a\u76f8\u7b49\u6bd4\u8f83\u65f6\uff0c\u5b57\u7b26\u4e32\u7c7b\u578b\u4f1a\u88ab\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\n\n3\uff09\u5f53\u5bf9\u8c61\u7c7b\u578b\u548c\u539f\u59cb\u7c7b\u578b\u505a\u76f8\u7b49\u6bd4\u8f83\u65f6\uff0c\u5bf9\u8c61\u7c7b\u578b\u4f1a\u4f9d\u7167ToPrimitive\u89c4\u5219\u8f6c\u6362\u4e3a\u539f\u59cb\u7c7b\u578b\n\n4\uff09\u5f53\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f\u5bf9\u8c61\u65f6\uff0cJavaScript\u4f1a\u6bd4\u8f83\u5176\u5185\u90e8\u5f15\u7528\uff0c\u5f53\u4e14\u4ec5\u5f53\u4ed6\u4eec\u7684\u5f15\u7528\u6307\u5411\u5185\u5b58\u4e2d\u7684\u76f8\u540c\u5bf9\u8c61\uff08\u533a\u57df\uff09\u65f6\u624d\u76f8\u7b49\uff0c\u5373\u4ed6\u4eec\u5728\u6808\u5185\u5b58\u4e2d\u7684\u5f15\u7528\u5730\u5740\u76f8\u540c\u3002\n\n5\uff09ECMAScript\u89c4\u8303\u4e2d\u89c4\u5b9anull\u548cundefined\u4e4b\u95f4\u4e92\u76f8\u5bbd\u677e\u76f8\u7b49\uff08==\uff09\uff0c\u5e76\u4e14\u4e5f\u4e0e\u5176\u81ea\u8eab\u76f8\u7b49\uff0c\u4f46\u548c\u5176\u4ed6\u6240\u6709\u7684\u503c\u90fd\u4e0d\u5bbd\u677e\u76f8\u7b49\uff08==\uff09\n\n### \u4e09\u3001\u5b57\u7b26\u4e32\u6790\u6784\u8d4b\u503c\u539f\u7406\n\n> let [a, b, c, d] = 'abcd'\n\n```\nfunction _slicedToArray(arr, i) {\nreturn _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }\n\nfunction _nonIterableRest() { throw new TypeError(\"Invalid attempt to destructure non-iterable instance\"); }\n\nfunction _iterableToArrayLimit(arr, i) {\nvar _arr = [];\nvar _n = true;\nvar _d = false;\nvar _e = undefined;\ntry\n{\nfor (var _i = arrSymbol.iterator, _s; !(_n = (_s = _i.next()).done); _n = true)\n{ _arr.push(_s.value); if (i && _arr.length === i) break; }\n}\ncatch (err) { _d = true; _e = err; }\nfinally {\ntry { if (!_n && _i[\"return\"] != null) _i\"return\"; }\nfinally { if (_d) throw _e; } } return _arr; }\n\nfunction _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }\n\nvar _abcd = \"abcd\",\n_abcd2 = _slicedToArray(_abcd, 4),\na = _abcd2[0],\nb = _abcd2[1],\nc = _abcd2[2],\nd = _abcd2[3];\n```\n\n### \u56db\u3001\u7b54\u6848\u89e3\u6790\n\n```js\n// if([]==0)\n1. Boolean(Number([])==0)\n2. Boolean(0==0)\n\n// [1,2]==\"1,2\"\n1. 1==\"1\"&&2==\"2\"\n2. true&&true\n\n// if([])\n1. Boolean([])\n2. Boolean(true) // []\u662f\u4e00\u4e2a\u590d\u6742\u7c7b\u578b\u5b58\u5728\u5730\u5740\u5f15\u7528\uff0c\u8f6c\u5316\u4e3aBoolean\u503c\u4e3atrue\n\n// []==0\n1. Number([])==0\n2. 0==0\n3. true\n```\n\n### \u4e94\u3001\u77e5\u8bc6\u70b9\n\n**ToString\u3001ToNumber\u3001ToBoolean\u3001ToPrimitive\u8f6c\u6362\u89c4\u5219\uff1a**\n\n#### 5.1 ToString\n\n\u8fd9\u91cc\u6240\u8bf4\u7684ToString\u53ef\u4e0d\u662f\u5bf9\u8c61\u7684toString\u65b9\u6cd5\uff0c\u800c\u662f\u6307\u5176\u4ed6\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u64cd\u4f5c\u3002\n\n\u770b\u4e0bnull\u3001undefined\u3001\u5e03\u5c14\u578b\u3001\u6570\u5b57\u3001\u6570\u7ec4\u3001\u666e\u901a\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u89c4\u5219\uff1a\n\n- \u2460null\uff1a\u8f6c\u4e3a\"null\"\n- \u2461undefined\uff1a\u8f6c\u4e3a\"undefined\"\n- \u2462\u5e03\u5c14\u7c7b\u578b\uff1atrue\u548cfalse\u5206\u522b\u88ab\u8f6c\u4e3a\"true\"\u548c\"false\"\n- \u2463\u6570\u5b57\u7c7b\u578b\uff1a\u8f6c\u4e3a\u6570\u5b57\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\uff0c\u598210\u8f6c\u4e3a\"10\"\uff0c 1e21\u8f6c\u4e3a\"1e+21\"\n- \u2464\u6570\u7ec4\uff1a\u8f6c\u4e3a\u5b57\u7b26\u4e32\u662f\u5c06\u6240\u6709\u5143\u7d20\u6309\u7167\",\"\u8fde\u63a5\u8d77\u6765\uff0c\u76f8\u5f53\u4e8e\u8c03\u7528\u6570\u7ec4\u7684Array.prototype.join()\u65b9\u6cd5\uff0c\u5982[1, 2, 3]\u8f6c\u4e3a\"1,2,3\"\uff0c\u7a7a\u6570\u7ec4[]\u8f6c\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u6570\u7ec4\u4e2d\u7684null\u6216undefined\uff0c\u4f1a\u88ab\u5f53\u505a\u7a7a\u5b57\u7b26\u4e32\u5904\u7406\n- \u2465\u666e\u901a\u5bf9\u8c61\uff1a\u8f6c\u4e3a\u5b57\u7b26\u4e32\u76f8\u5f53\u4e8e\u76f4\u63a5\u4f7f\u7528Object.prototype.toString()\uff0c\u8fd4\u56de\"[object Object]\"\n\n#### 5.2 ToNumber\n\nToNumber\u6307\u5176\u4ed6\u7c7b\u578b\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\u7684\u64cd\u4f5c\u3002\n\n- \u2460null\uff1a \u8f6c\u4e3a0\n- \u2461undefined\uff1a\u8f6c\u4e3aNaN\n- \u2462\u5b57\u7b26\u4e32\uff1a\u5982\u679c\u662f\u7eaf\u6570\u5b57\u5f62\u5f0f\uff0c\u5219\u8f6c\u4e3a\u5bf9\u5e94\u7684\u6570\u5b57\uff0c\u7a7a\u5b57\u7b26\u8f6c\u4e3a0, \u5426\u5219\u4e00\u5f8b\u6309\u8f6c\u6362\u5931\u8d25\u5904\u7406\uff0c\u8f6c\u4e3aNaN\n- \u2463\u5e03\u5c14\u578b\uff1atrue\u548cfalse\u88ab\u8f6c\u4e3a1\u548c0\n- \u2464\u6570\u7ec4\uff1a\u6570\u7ec4\u9996\u5148\u4f1a\u88ab\u8f6c\u4e3a\u539f\u59cb\u7c7b\u578b\uff0c\u4e5f\u5c31\u662fToPrimitive\uff0c\u7136\u540e\u5728\u6839\u636e\u8f6c\u6362\u540e\u7684\u539f\u59cb\u7c7b\u578b\u6309\u7167\u4e0a\u9762\u7684\u89c4\u5219\u5904\u7406\uff0c\n- \u2465\u5bf9\u8c61\uff1a\u540c\u6570\u7ec4\u7684\u5904\u7406\n\n#### 5.3 ToBoolean\n\nToBoolean\u6307\u5176\u4ed6\u7c7b\u578b\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578b\u7684\u64cd\u4f5c\n\njs\u4e2d\u7684\u5047\u503c\u53ea\u6709false\u3001null\u3001undefined\u3001\u7a7a\u5b57\u7b26\u30010\u548cNaN\uff0c\u5176\u5b83\u503c\u8f6c\u4e3a\u5e03\u5c14\u578b\u90fd\u4e3atrue\u3002\n\n#### 5.4 ToPrimitive\n\nToPrimitive\u6307\u5bf9\u8c61\u7c7b\u578b\u7c7b\u578b\uff08\u5982\uff1a\u5bf9\u8c61\u3001\u6570\u7ec4\uff09\u8f6c\u6362\u4e3a\u539f\u59cb\u7c7b\u578b\u7684\u64cd\u4f5c\u3002\n\n- \u2460\u5f53\u5bf9\u8c61\u7c7b\u578b\u9700\u8981\u88ab\u8f6c\u4e3a\u539f\u59cb\u7c7b\u578b\u65f6\uff0c\u5b83\u4f1a\u5148\u67e5\u627e\u5bf9\u8c61\u7684valueOf\u65b9\u6cd5\uff0c\u5982\u679cvalueOf\u65b9\u6cd5\u8fd4\u56de\u539f\u59cb\u7c7b\u578b\u7684\u503c\uff0c\u5219ToPrimitive\u7684\u7ed3\u679c\u5c31\u662f\u8fd9\u4e2a\u503c\n- \u2461\u5982\u679cvalueOf\u4e0d\u5b58\u5728\u6216\u8005valueOf\u65b9\u6cd5\u8fd4\u56de\u7684\u4e0d\u662f\u539f\u59cb\u7c7b\u578b\u7684\u503c\uff0c\u5c31\u4f1a\u5c1d\u8bd5\u8c03\u7528\u5bf9\u8c61\u7684toString\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f\u4f1a\u9075\u5faa\u5bf9\u8c61\u7684ToString\u89c4\u5219\uff0c\u7136\u540e\u4f7f\u7528toString\u7684\u8fd4\u56de\u503c\u4f5c\u4e3aToPrimitive\u7684\u7ed3\u679c\u3002\u5982\u679cvalueOf\u548ctoString\u90fd\u6ca1\u6709\u8fd4\u56de\u539f\u59cb\u7c7b\u578b\u7684\u503c\uff0c\u5219\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\n- \u2462\u6ce8\u610f\uff1a\u5bf9\u4e8e\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u6765\u8bf4\uff0cToPrimitive\u7684\u89c4\u5219\u6709\u6240\u4e0d\u540c\uff0c\u6bd4\u5982Date\u5bf9\u8c61\u4f1a\u5148\u8c03\u7528toString\uff0ca.Number([])\uff0c \u7a7a\u6570\u7ec4\u4f1a\u5148\u8c03\u7528valueOf\uff0c\u4f46\u8fd4\u56de\u7684\u662f\u6570\u7ec4\u672c\u8eab\uff0c\u4e0d\u662f\u539f\u59cb\u7c7b\u578b\uff0c\u6240\u4ee5\u4f1a\u7ee7\u7eed\u8c03\u7528toString\uff0c\u5f97\u5230\u7a7a\u5b57\u7b26\u4e32\uff0c\u76f8\u5f53\u4e8eNumber('')\uff0c\u6240\u4ee5\u8f6c\u6362\u540e\u7684\u7ed3\u679c\u4e3a\"0\",Number(['10'])\u76f8\u5f53\u4e8eNumber('10')\uff0c\u5f97\u5230\u7ed3\u679c10\n\n\n", "answer": "", "biz_type": 1, "qid": 664, "subject": "", "title": "if([] == 0), [1,2] == \"1,2\", if([]), [] == 0 \u5177\u4f53\u662f\u600e\u4e48\u5bf9\u6bd4\u7684"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9177\u5bb6\u4e50"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 5, "qid": 533, "qtype": "short_answer", "short_answer": {"analysis": "### setInterval\n\n```js\nvar intervalID = scope.setInterval(func, delay, [arg1, arg2, ...]);\nvar intervalID = scope.setInterval(code, delay);\n```\n\n- `func`:\u8981\u91cd\u590d\u8c03\u7528\u7684\u51fd\u6570\u3002`A function to be executed every delay milliseconds. The function is not passed any arguments, and no return value is expected.`\n- `code`:\u8fd9\u4e2a\u8bed\u6cd5\u662f\u53ef\u9009\u7684\uff0c\u4f60\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a\u5b57\u7b26\u4e32\u6765\u4ee3\u66ff\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\uff0c\u4f20\u9012\u7684\u5b57\u7b26\u4e32\u4f1a\u88ab\u7f16\u8bd1\u7136\u540e\u6bcf\u4e2a`delay`\u6beb\u79d2\u65f6\u95f4\u5185\u6267\u884c\u4e00\u6b21\u3002\uff08\u4e0d\u88ab\u63a8\u8350\uff09\n- `delay`:\u6bcf\u6b21\u5ef6\u8fdf\u7684\u6beb\u79d2\u6570(\u4e00\u79d2\u7b49\u4e8e1000\u6beb\u79d2)\uff0c\u51fd\u6570\u7684\u6bcf\u6b21\u8c03\u7528\u4f1a\u5728\u8be5\u5ef6\u8fdf\u4e4b\u540e\u53d1\u751f\uff0c\u548csetTimeout\u4e00\u6837\uff0c**\u5b9e\u9645\u7684\u5ef6\u8fdf\u65f6\u95f4\u53ef\u80fd\u4f1a\u957f\u4e00\u70b9**\u3002\u8fd9\u4e2a\u65f6\u95f4\u8ba1\u7b97\u5355\u4f4d\u662f\u6beb\u79d2\uff08\u5343\u5206\u4e4b\u4e00\u79d2\u3002\uff09\u5982\u679c\u8fd9\u4e2a\u53c2\u6570\u503c\u5c0f\u4e8e10\uff0c\u5219\u9ed8\u8ba4\u4f7f\u7528\u503c\u4e3a10\u3002\n\n#### 1.\u8fd4\u56de\u503c\n\nintervalID\u4e3a\u975e0\u6570\u503c\uff0c\u7528\u6765\u6807\u8bc6\u901a\u8fc7`setInterval`\u521b\u5efa\u7684\u8ba1\u65f6\u5668\uff0c\u8fd9\u4e2a\u503c\u53ef\u4ee5\u7528\u6765\u4f5c\u4e3a`clearInterval`\u7684\u53c2\u6570\u6765\u6e05\u695a\u5bf9\u5e94\u503c\n\n#### 2.\u6ce8\u610f\u70b9\n\n1. `setInterval`\u548c`setTimeout`\u5171\u4eab\u4e00\u4e2aID\u6c60\n2. `setInterval`\u9700\u8981\u53ca\u65f6\u6e05\u9664\uff0c\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\n3. \u53c2\u6570`code`\u4f20\u5165\u7684\u503c\u4e3a\u51fd\u6570:`setInterval('app()',200)`\n4. setInterval\u53ef\u80fd\u4e0d\u662f\u7cbe\u786e\u7684\n\n\u8be6\u7ec6\u4ecb\u7ecd\u4e0b\n\n**1\uff09\u88ab\u5ffd\u7565\u7684\u53c2\u6570**\n\nsetInterval \u548c setTimeout \u4e00\u6837\uff0c\u53c2\u6570\u4e3a\u4efb\u610f\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u56de\u8c03\u3002\u7b2c\u4e8c\u4e2a\u662f\u65f6\u95f4\u6233\uff0c\u4ece\u7b2c\u4e09\u4e2a\u53c2\u6570\u5f00\u59cb\uff0c\u53c2\u6570\u4f1a\u88ab\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff08\u4e5f\u5c31\u662f\u56de\u8c03\u51fd\u6570\uff09\u7684\u5f62\u53c2\u4f7f\u7528\uff0c\u7c7b\u4f3c\u4e8e\u95ed\u5305\u4f20\u503c\uff0c\u5f53\u7136\uff0c\u4ece\u7b2c\u4e09\u4e2a\u53c2\u6570\u5f00\u59cb\u4ee5\u53ca\u540e\u9762\u7684\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7406\u89e3\u4e3a rest \u8868\u8fbe\u5f0f\uff08\u53ca...args\uff09,\u4e0b\u9762\u7684\u8fd9\u6bb5\u4ee3\u7801\uff0c\u8db3\u4ee5\u8bf4\u660e\u4e00\u5207\n\n```js\nfunction sum(x,y,z){\n console.log(x+y+z);\n}\nsetTimeout(sum,1000,1,2,3);\n```\n\n**2\uff09setInterval \u4e3a\u4ec0\u4e48\u5bb9\u6613\u9020\u6210\u5185\u5b58\u6cc4\u6f0f**\n\n\u4ec0\u4e48\u662f\u5185\u5b58\u6cc4\u6f0f\uff1a\u901a\u4fd7\u4e00\u4e9b\uff0c\u5c31\u662f\u6211\u4eec\u521b\u5efa\u7684\u53d8\u91cf\u6216\u8005\u5b9a\u4e49\u7684\u5bf9\u8c61\uff0c\u7528\u5b8c\u6216\u8005\u6ca1\u7528\u4e4b\u540e\uff0c\u672a\u80fd\u88ab GC\uff0c\u5bfc\u81f4\u7cfb\u7edf\u5185\u6709\u65b0\u7684\u5185\u5b58\u53ef\u4ee5\u5206\u914d\u7ed9\u540e\u9762\u7684\u53d8\u91cf\uff0c\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\n\n\u6bd4\u5982\u4e00\u822c\u6211\u4eec\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u65f6\u5668\uff0c\u6211\u4eec\u4f1a\u60f3\u5230 setInterval\uff0c\u5982\u679c\u6211\u4eec\u4e0d\u505c\u7684\u6539\u53d8 dom\uff0c\u800c\u4e14\u5185\u90e8\u5f15\u7528\u53d8\u91cf\u5e9e\u5927\uff0c\u5f88\u5bb9\u6613\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\uff0c\u5f53\u7136\uff0c\u751a\u81f3\u8fd8\u6709\u66f4\u6c14\u4eba\u7684\u5f00\u53d1\u8005\uff0c\u8fd8\u662f\u4ee5\u8ba1\u65f6\u5668\u4e3a\u4f8b\uff0c\u70b9\u51fb dom \u5f00\u59cb\u8ba1\u65f6\uff0c\u800c\u6ca1\u6709\u505a\u4efb\u4f55\u7684\u9650\u5236\uff0c\u7528\u6237\u591a\u70b9\u51e0\u4e0b\uff0csetInterval \u88ab\u591a\u6b21\u521b\u5efa\u7b49\u7b49\u95ee\u9898\n\n\u603b\u7ed3:setInterval \u5e76\u4e0d\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\uff0c\u662f\u4f7f\u7528\u8005\u7684\u6ee5\u7528\uff0c\u5bfc\u81f4\u73b0\u5728\u53ea\u8981\u770b\u5230 setInterval \u7b49\u5c31\u4f1a\u89c9\u5f97\u5fc3\u70e6\n\n**3\uff09\u56de\u6536 setInterval**\n\n```js\nconst timer = setInterval(()=>{console.log('i am here')},500);\nclearInterval(timer);\n```\n\n\u770b\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\uff0c\u6211\u4eec\u5728\u4f7f\u7528 clearInterval \u6216 clearTimeout \u6e05\u9664\u4e86 setInterval \u4e4b\u540e\uff0ctimer\u4f1a\u88abGC\u6389\u5417\uff0c\u7b54\u6848\u662f\u4e0d\u4f1a\uff0c\u56e0\u4e3aclearInterval \u6216 clearTimeout\u53ea\u80fd\u6e05\u9664\u5bf9\u5e94id\u7684setInterval(\u6216setTimeout)\uff0c\u6e05\u9664\u4e4b\u540etimer\u4f1a\u88ab\u8d4b\u503c\u4e3aid\uff0c\u6240\u4ee5\u4e0d\u4f1a\u88abGC\uff0c\u5f53\u7136\uff0c\u6211\u4eec\u53ef\u4ee5\u624b\u52a8\u8d4b\u503c\u4e3anull,\u7b49\u5f85\u56de\u6536\n\n**4\uff09this\u6307\u5411\u7684\u95ee\u9898**\n\n\u6765\u770b\u4e00\u4e2a\u6613\u9519\u7684\u4f8b\u5b50\uff0c\u4e0b\u9762\u7528setTimeout\u6765\u770b\n\n```js\nvar i = 0;\nconst o = {\n i: 1;\n fn: function(){\n console.log(this.i);\n }\n}\nsetTimeout(o.fn, 1000); //0\n```\n\n\u7b54\u6848\u4e3a\u4ec0\u4e48\u662f0\uff0cfn\u4e0d\u662f\u5bf9\u8c61o\u7684\u5417\uff0c\u786e\u5b9e\u662fo\u7684\uff0c\u4f46\u662f\u6211\u4eecsetTimeout\u662f\u5728\u5168\u5c40\u73af\u5883(window EC)\u4e0b\u7684\uff0c\u5176\u5b9e\u76f8\u5f53\u4e8e\uff1a\n\n```js\nvar a = o.fn;\na();\n```", "answer": "", "biz_type": 1, "qid": 533, "subject": "", "title": "setInterval \u9700\u8981\u6ce8\u610f\u7684\u70b9"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9177\u5bb6\u4e50"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 540, "qtype": "short_answer", "short_answer": {"analysis": "### Object.defineProperty\n\n`Object.defineProperty()` \u65b9\u6cd5\u4f1a\u76f4\u63a5\u5728\u4e00\u4e2a\u5bf9\u8c61\u4e0a\u5b9a\u4e49\u4e00\u4e2a\u65b0\u5c5e\u6027\uff0c\u6216\u8005\u4fee\u6539\u4e00\u4e2a\u5bf9\u8c61\u7684\u73b0\u6709\u5c5e\u6027\uff0c\u5e76\u8fd4\u56de\u6b64\u5bf9\u8c61\u3002\n\n- \u901a\u8fc7\u5b9a\u4e49\u6216\u4fee\u6539\u5c5e\u6027\u63cf\u8ff0\u7b26\u6765\u63a7\u5236\u5c5e\u6027\u7684\u8bbf\u95ee\n- \u901a\u8fc7 getter/setter \u5bf9\u5c5e\u6027\u8fdb\u884c\u52ab\u6301\uff0c\u6bd4\u5982 VUE \u7684\u54cd\u5e94\u5f0f\n\n#### 1.1\u8bed\u6cd5\n\n`Object.defineProperty(obj, prop, descriptor)`\n\n\u53c2\u6570\u8bf4\u660e\uff1a\n\n- obj:\u8981\u5b9a\u4e49\u5c5e\u6027\u7684\u5bf9\u8c61\n- prop:\u8981\u5b9a\u4e49\u6216\u4fee\u6539\u7684\u5c5e\u6027\u7684\u540d\u79f0\u6216 Symbol \u3002\n- descriptor:\u8981\u5b9a\u4e49\u6216\u4fee\u6539\u7684\u5c5e\u6027\u63cf\u8ff0\u7b26\n\n\u8fd4\u56de\u503c\uff1a\n\n- \u88ab\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5bf9\u8c61\n\n\u9488\u5bf9\u5c5e\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed9\u8fd9\u4e2a\u5c5e\u6027\u8bbe\u7f6e\u4e00\u4e9b\u7279\u6027\uff0c\u6bd4\u5982\u662f\u5426\u53ea\u8bfb\u4e0d\u53ef\u4ee5\u5199\uff1b\u662f\u5426\u53ef\u4ee5\u88abfor..in\u6216Object.keys()\u904d\u5386\u3002\n\n\u7ed9\u5bf9\u8c61\u7684\u5c5e\u6027\u6dfb\u52a0\u7279\u6027\u63cf\u8ff0\uff0c\u76ee\u524d\u63d0\u4f9b\u4e24\u79cd\u5f62\u5f0f\uff1a\u6570\u636e\u63cf\u8ff0\u548c\u5b58\u53d6\u5668\u63cf\u8ff0\u3002\n\n#### 1.2\u6570\u636e\u63cf\u8ff0\n\n\u5f53\u4fee\u6539\u6216\u5b9a\u4e49\u5bf9\u8c61\u7684\u67d0\u4e2a\u5c5e\u6027\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u7ed9\u8fd9\u4e2a\u5c5e\u6027\u6dfb\u52a0\u4e00\u4e9b\u7279\u6027\uff1a\n\n```js\nvar obj = {\n test:\"hello\"\n}\n//\u5bf9\u8c61\u5df2\u6709\u7684\u5c5e\u6027\u6dfb\u52a0\u7279\u6027\u63cf\u8ff0\nObject.defineProperty(obj,\"test\",{\n configurable:true | false,\n enumerable:true | false,\n value:\u4efb\u610f\u7c7b\u578b\u7684\u503c,\n writable:true | false\n});\n//\u5bf9\u8c61\u65b0\u6dfb\u52a0\u7684\u5c5e\u6027\u7684\u7279\u6027\u63cf\u8ff0\nObject.defineProperty(obj,\"newKey\",{\n configurable:true | false,\n enumerable:true | false,\n value:\u4efb\u610f\u7c7b\u578b\u7684\u503c,\n writable:true | false\n});\n```\n- **value** :\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\u7684\u503c\uff0c\u9ed8\u8ba4\u662fundefined\n- **writeable** : \u5c5e\u6027\u503c\u662f\u5426\u53ef\u4ee5\u88ab\u91cd\u5199\u3002\u8bbe\u7f6e\u4e3atrue\u53ef\u4ee5\u88ab\u91cd\u5199\uff0c\u8bbe\u7f6e\u4e3afalse,\u4e0d\u80fd\u88ab\u91cd\u5199\u3002\u9ed8\u8ba4\u4e3afalse\u3002\n- **enumerable** : \u6b64\u5c5e\u6027\u662f\u5426\u53ef\u4ee5\u88ab\u679a\u4e3e\uff08\u4f7f\u7528for...in\u6216Object.keys()\uff09\u3002\u8bbe\u7f6e\u4e3atrue\u53ef\u4ee5\u88ab\u679a\u4e3e\uff1b\u8bbe\u7f6e\u4e3afalse\uff0c\u4e0d\u80fd\u88ab\u679a\u4e3e\u3002\u9ed8\u8ba4\u4e3afalse\u3002\n- **configurable** : \u662f\u5426\u53ef\u4ee5\u5220\u9664\u76ee\u6807\u5c5e\u6027\u6216\u662f\u5426\u53ef\u4ee5\u518d\u6b21\u4fee\u6539\u5c5e\u6027\u7684\u7279\u6027\uff08writable, configurable, enumerable\uff09\u3002\u8bbe\u7f6e\u4e3atrue\u53ef\u4ee5\u88ab\u5220\u9664\u6216\u53ef\u4ee5\u91cd\u65b0\u8bbe\u7f6e\u7279\u6027\uff1b\u8bbe\u7f6e\u4e3afalse\uff0c\u4e0d\u80fd\u88ab\u53ef\u4ee5\u88ab\u5220\u9664\u6216\u4e0d\u53ef\u4ee5\u91cd\u65b0\u8bbe\u7f6e\u7279\u6027\u3002\u9ed8\u8ba4\u4e3afalse\u3002\n\n\u9664\u4e86\u53ef\u4ee5\u7ed9\u65b0\u5b9a\u4e49\u7684\u5c5e\u6027\u8bbe\u7f6e\u7279\u6027\uff0c\u4e5f\u53ef\u4ee5\u7ed9\u5df2\u6709\u7684\u5c5e\u6027\u8bbe\u7f6e\u7279\u6027\n\n> \u63d0\u793a\uff1a\u4e00\u65e6\u4f7f\u7528Object.defineProperty\u7ed9\u5bf9\u8c61\u6dfb\u52a0\u5c5e\u6027\uff0c\u90a3\u4e48\u5982\u679c\u4e0d\u8bbe\u7f6e\u5c5e\u6027\u7684\u7279\u6027\uff0c\u90a3\u4e48configurable\u3001enumerable\u3001writable\u8fd9\u4e9b\u503c\u90fd\u4e3a\u9ed8\u8ba4\u7684false\n\n#### 1.3 \u5b58\u50a8\u5668\u63cf\u8ff0\n\n\u5f53\u4f7f\u7528\u5b58\u53d6\u5668\u63cf\u8ff0\u5c5e\u6027\u7684\u7279\u6027\u7684\u65f6\u5019\uff0c\u5141\u8bb8\u8bbe\u7f6e\u4ee5\u4e0b\u7279\u6027\u5c5e\u6027\uff1a\n\n```js\nvar obj = {};\nObject.defineProperty(obj,\"newKey\",{\n get:function (){} | undefined,\n set:function (value){} | undefined\n configurable: true | false\n enumerable: true | false\n});\n```\n\n> \u6ce8\u610f\uff1a\u5f53\u4f7f\u7528\u4e86getter\u6216setter\u65b9\u6cd5\uff0c\u4e0d\u5141\u8bb8\u4f7f\u7528writable\u548cvalue\u8fd9\u4e24\u4e2a\u5c5e\u6027\n\n**getter/setter**\n\n\u5f53\u8bbe\u7f6e\u6216\u83b7\u53d6\u5bf9\u8c61\u7684\u67d0\u4e2a\u5c5e\u6027\u7684\u503c\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u63d0\u4f9bgetter/setter\u65b9\u6cd5\u3002\n\n- getter \u662f\u4e00\u79cd\u83b7\u5f97\u5c5e\u6027\u503c\u7684\u65b9\u6cd5\n- setter\u662f\u4e00\u79cd\u8bbe\u7f6e\u5c5e\u6027\u503c\u7684\u65b9\u6cd5\u3002\n\n```js\nvar obj = {};\nvar initValue = 'hello';\nObject.defineProperty(obj,\"newKey\",{\n get:function (){\n //\u5f53\u83b7\u53d6\u503c\u7684\u65f6\u5019\u89e6\u53d1\u7684\u51fd\u6570\n return initValue; \n },\n set:function (value){\n //\u5f53\u8bbe\u7f6e\u503c\u7684\u65f6\u5019\u89e6\u53d1\u7684\u51fd\u6570,\u8bbe\u7f6e\u7684\u65b0\u503c\u901a\u8fc7\u53c2\u6570value\u62ff\u5230\n initValue = value;\n }\n});\n//\u83b7\u53d6\u503c\nconsole.log( obj.newKey ); //hello\n\n//\u8bbe\u7f6e\u503c\nobj.newKey = 'change value';\n\nconsole.log( obj.newKey ); //change value\n```\n\n> \u6ce8\u610f\uff1aget\u6216set\u4e0d\u662f\u5fc5\u987b\u6210\u5bf9\u51fa\u73b0\uff0c\u4efb\u5199\u5176\u4e00\u5c31\u53ef\u4ee5\u3002\u5982\u679c\u4e0d\u8bbe\u7f6e\u65b9\u6cd5\uff0c\u5219get\u548cset\u7684\u9ed8\u8ba4\u503c\u4e3aundefined\n\n#### 1.4 \u5c40\u9650\u6027\n\n**\u68c0\u6d4b\u4e0d\u5230\u5bf9\u8c61\u5c5e\u6027\u7684\u6dfb\u52a0\u3001\u5220\u9664**\n\n```js\nvar data = {_obj:{}}\nobj.defineProperty(obj,\"obj\",{\n get:function(){\n return this._data;\n },\n set:function(newValue){\n console.log(\"\u53d8\u5316\u4e86\")\n }\n})\ndata.obj.name = \"\u5f20\u4e09\";//\u76d1\u6d4b\u4e0d\u5230\n```\n\n**\u68c0\u6d4b\u4e0d\u5230\u6570\u7ec4\u957f\u5ea6\u7684\u53d8\u5316\uff0c\u68c0\u6d4b\u4e0d\u5230\u7ed9\u6570\u7ec4\u7684\u67d0\u4e00\u9879\u8d4b\u503c**\n\n```js\nvar arr = [1,2,3,5];\narr.length = 0;//\u76d1\u6d4b\u4e0d\u5230\narr[0] = 10;//\u76d1\u6d4b\u4e0d\u5230\n```\n\n#### 1.5 \u4f7f\u7528\u573a\u666f\n\n**1.5.1MVVM\u4e2d\u6570\u636e\u7684\u53cc\u5411\u7ed1\u5b9a**\n\n\u5982vue\u7b49\u5927\u90e8\u5206mvvm\u6846\u67b6(angular \u7528\u7684\u810f\u5904\u7406)\u90fd\u662f\u901a\u8fc7Object.defineProperty\u6765\u5b9e\u73b0\u6570\u636e\u7ed1\u5b9a\u7684\u3002\n\n**1.5.2\u4f18\u5316\u5bf9\u8c61\u83b7\u53d6\u548c\u4fee\u6539\u5c5e\u6027\u7684\u65b9\u5f0f**\n\n\u6bd4\u5982\uff0c\u8fc7\u53bb\u8bbe\u7f6edom\u8282\u70b9transform\u65f6\u662f\u8fd9\u6837\u7684\n\n```js\n//\u52a0\u5165\u6709\u4e00\u4e2a\u76ee\u6807\u8282\u70b9\uff0c \u6211\u4eec\u60f3\u8bbe\u7f6e\u5176\u4f4d\u79fb\u65f6\u662f\u8fd9\u6837\u7684\nvar targetDom = document.getElementById('target');\nvar transformText = 'translateX(' + 10 + 'px)';\ntargetDom.style.webkitTransform = transformText;\ntargetDom.style.transform = transformText;\n```\n\n\u5411\u4e0a\u9762\u8fd9\u79cd\u5199\u6cd5\uff0c\u5f53\u9875\u9762\u4e2d\u6709\u5f88\u591a\u52a8\u753b\u65f6\uff0c\u8fd9\u6837\u5199\u662f\u975e\u5e38\u75db\u82e6\u7684\u3002\n\n\u901a\u8fc7Object.defineProperty\u7684\u65b9\u5f0f\n\n```js\n//\u8fd9\u91cc\u53ea\u662f\u7b80\u5355\u8bbe\u7f6e\u4e0btranslateX\u7684\u5c5e\u6027\uff0c\u5176\u4ed6\u5982scale\u7b49\u5c5e\u6027\u53ef\u81ea\u5df1\u53bb\u5c1d\u8bd5\n\nObject.defineProperty(dom, 'translateX', {\nset: function(value) {\n var transformText = 'translateX(' + value + 'px)';\n dom.style.webkitTransform = transformText;\n dom.style.transform = transformText;\n}\n//\u8fd9\u6837\u518d\u540e\u9762\u8c03\u7528\u7684\u65f6\u5019, \u5341\u5206\u7b80\u5355\ndom.translateX = 10;\ndom.translateX = -10;\n//\u751a\u81f3\u53ef\u4ee5\u62d3\u5c55\u8bbe\u7f6e\u5982scale, originX, translateZ,\u7b49\u5404\u4e2a\u5c5e\u6027\uff0c\u8fbe\u5230\u4e0b\u9762\u7684\u6548\u679c\ndom.scale = 1.5; //\u653e\u59271.5\u500d\ndom.originX = 5; //\u8bbe\u7f6e\u4e2d\u5fc3\u70b9X\n}\n```\n\n\u8fd9\u6837\u662f\u4e0d\u662f\u5c31\u4f1a\u597d\u5f88\u591a\u4e86\u3002", "answer": "", "biz_type": 1, "qid": 540, "subject": "", "title": "\u4ecb\u7ecd defineProperty \u65b9\u6cd5\uff0c\u4ec0\u4e48\u65f6\u5019\u9700\u8981\u7528\u5230"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fae\u533b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 499, "qtype": "short_answer", "short_answer": {"analysis": "### JavaScript \u6267\u884c\u8fc7\u7a0b\n\nJavaScript \u6267\u884c\u5206\u4e24\u4e2a\u9636\u6bb5\uff1a**\u89e3\u6790\u9636\u6bb5\u3001\u8fd0\u884c\u9636\u6bb5**\n- \u89e3\u6790\u9636\u6bb5\u5206\u4e3a\uff1a\u8bcd\u6cd5\u5206\u6790\u3001\u8bed\u6cd5\u5206\u6790\n- \u8fd0\u884c\u9636\u6bb5\u5206\u4e3a\uff1a\u9884\u89e3\u6790\u3001\u8fd0\u884c\u3002\n\n**\u8bcd\u6cd5\u5206\u6790:** \u5c06\u4ee3\u7801\u4e2d\u7684\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u6709\u610f\u4e49\u7684\u4ee3\u7801\u5757\uff08token\uff09\uff0c\u8fd9\u4e9b\u4ee3\u7801\u5757\u53ef\u79f0\u4e4b\u4e3a\u201c\u8bcd\u6cd5\u5355\u5143\u201d\u3002\u5982 `var a = 1` \u4f1a\u88ab\u5206\u4e3a var\u3001a\u3001=\u30011\uff0c\u8fd9\u4e9b\u96f6\u6563\u7684\u5355\u5143\u4f1a\u7ec4\u6210\u4e00\u4e2a\u8bcd\u6cd5\u5355\u5143\u6d41\u8fdb\u884c\u89e3\u6790\u3002\n\n**\u8bed\u6cd5\u5206\u6790:** \u5c06\u8bcd\u6cd5\u5355\u5143\u6d41\u8f6c\u6362\u6210\u4e00\u9897\u62bd\u8c61\u8bed\u6cd5\u6811\uff08AST\uff09\u3002\n\n**\u9884\u89e3\u6790:** \u5728 JS \u4ee3\u7801\u5728\u6b63\u5f0f\u6267\u884c\u4e4b\u524d\uff0c\u4f1a\u8fdb\u884c\u4e00\u4e9b\u89e3\u6790\u5de5\u4f5c\u3002\u5982\u5bfb\u627e var \u58f0\u660e\u7684\u53d8\u91cf\u548c function\u58f0\u660e\u7684\u51fd\u6570\uff0c\u627e\u5230\u540e\u8fdb\u884c\u63d0\u5347\uff0c\u4f46\u662f\u5728\u53d8\u91cf\u63d0\u5347\u65f6\u4e0d\u4f1a\u8d4b\u503c\uff0c\u56e0\u6b64\u5b83\u7684\u9ed8\u8ba4\u503c\u662f undefined\u3002\u901a\u8fc7\u63d0\u5347\uff0c\u51fd\u6570\u53ef\u4ee5\u5728\u58f0\u660e\u51fd\u6570\u4f53\u4e4b\u4e0a\u8fdb\u884c\u8c03\u7528\uff0c\u53d8\u91cf\u4e5f\u53ef\u4ee5\u5728\u8d4b\u503c\u4e4b\u524d\u8fdb\u884c\u8f93\u51fa\uff0c\u53ea\u662f\u8fd9\u65f6\u8f93\u51fa\u7684\u503c\u4e3a undefined\u3002", "answer": "", "biz_type": 1, "qid": 499, "subject": "", "title": "JavaScript \u6267\u884c\u8fc7\u7a0b\u5206\u4e3a\u54ea\u4e9b\u9636\u6bb5"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 9, "qid": 584, "qtype": "short_answer", "short_answer": {"analysis": "### instanceof\n\ninstanceof \u4e3b\u8981\u7528\u4e8e\u5224\u65ad\u67d0\u4e2a\u5b9e\u4f8b\u662f\u5426\u5c5e\u4e8e\u67d0\u4e2a\u7c7b\u578b\uff0c\u4e5f\u53ef\u7528\u4e8e\u5224\u65ad\u67d0\u4e2a\u5b9e\u4f8b\u662f\u5426\u662f\u5176\u7236\u7c7b\u578b\u6216\u8005\u7956\u5148\u7c7b\u578b\u7684\u5b9e\u4f8b\u3002\n\ninstanceof \u4e3b\u8981\u7684\u5b9e\u73b0\u539f\u7406\u5c31\u662f\u53ea\u8981\u53f3\u8fb9\u53d8\u91cf\u7684 prototype \u5728\u5de6\u8fb9\u53d8\u91cf\u7684\u539f\u578b\u94fe\u4e0a\u5373\u53ef\u3002\u56e0\u6b64\uff0cinstanceof \u5728\u67e5\u627e\u7684\u8fc7\u7a0b\u4e2d\u4f1a\u904d\u5386\u5de6\u8fb9\u53d8\u91cf\u7684\u539f\u578b\u94fe\uff0c\u76f4\u5230\u627e\u5230\u53f3\u8fb9\u53d8\u91cf\u7684 prototype\uff0c\u5982\u679c\u67e5\u627e\u5931\u8d25\uff0c\u5219\u4f1a\u8fd4\u56de false\u3002\n\n\n```js\nfunction instanceof(left, right) {\n const rightVal = right.prototype\n const leftVal = left.__proto__\n // \u82e5\u627e\u4e0d\u5230\u5c31\u5230\u4e00\u76f4\u5faa\u73af\u5230\u7236\u7c7b\u578b\u6216\u7956\u7c7b\u578b\n while (leftVal) {\n if (leftVal === rightVal) {\n return true\n }\n leftVal = leftVal.__proto__ // \u83b7\u53d6\u7956\u7c7b\u578b\u7684__proto__\n }\n return false;\n}\n```", "answer": "", "biz_type": 1, "qid": 584, "subject": "", "title": "\u4ecb\u7ecd instanceof \u539f\u7406\uff0c\u5e76\u624b\u52a8\u5b9e\u73b0"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9177\u5bb6\u4e50"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 6, "qid": 539, "qtype": "short_answer", "short_answer": {"analysis": "### ES5\u7684\u7c7b\n\nfunction\n\n### class\n\n\u7c7b\u58f0\u660e\u521b\u5efa\u4e00\u4e2a\u57fa\u4e8e\u539f\u578b\u7ee7\u627f\u7684\u5177\u6709\u7ed9\u5b9a\u540d\u79f0\u7684\u65b0\u7c7b\n\n### \u533a\u522b\n\n1. \u7c7b\u58f0\u660e\u4e0d\u53ef\u4ee5\u63d0\u5347\n2. \u7c7b\u58f0\u660e\u4e0d\u5141\u8bb8\u518d\u6b21\u58f0\u660e\u5df2\u7ecf\u5b58\u5728\u7684\u7c7b\uff0c\u5426\u5219\u5c06\u4f1a\u629b\u51fa\u4e00\u4e2a\u7c7b\u578b\u9519\u8bef\u3002\n3. class \u5185\u90e8\u662f\u4e25\u683c\u6a21\u5f0f\uff0c\u6784\u9020\u51fd\u6570\u662f\u53ef\u9009\u7684\n4. class \u7684\u9759\u6001\u65b9\u6cd5\u6216\u8005\u539f\u578b\u65b9\u6cd5\u90fd\u4e0d\u53ef\u679a\u4e3e\uff0c\u4e14\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u6ca1\u6709\u539f\u578b\uff0c\u4e0d\u53ef\u88abnew\n5. class \u5fc5\u987b\u4f7f\u7528new \u6765\u8c03\u7528\n6. class \u5185\u90e8\u65e0\u6cd5\u91cd\u5199\u7c7b\u540d\n\n### \u4ee3\u7801\u793a\u4f8b\n\n```js\n/**\n *ES6\u7684\u7c7b\n**/\nconst bar = Symbol('bar');\nconst snaf = Symbol('snaf');\nclass Parent {\n constructor(a) {\n this.a = a;\n this.printName = this.printName.bind(this)\n }\n print() {\n console.log('parent')\n }\n printName(name = 'there') {\n this.print(`Hello ${name}`);\n }\n // \u516c\u6709\u65b9\u6cd5\n foo(baz) {\n this[bar](baz);\n }\n // \u79c1\u6709\u65b9\u6cd5\n [bar](baz) {//[bar]\u7528\u65b9\u62ec\u53f7\u4ee3\u8868\u4ece\u8868\u8fbe\u5f0f\u83b7\u53d6\u7684\u5c5e\u6027\u540d\n return this[snaf] = baz;\n }\n\n}\n```\n\n- Parent\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5\u4e0d\u53ef\u7528Object.keys(Point.prototype)\u679a\u4e3e\u5230\n- \u91cd\u590d\u5b9a\u4e49Parent class\u4f1a\u62a5\u9519;\n- \u53ef\u901a\u8fc7\u5b9e\u4f8b\u7684__proto__\u5c5e\u6027\u5411\u539f\u578b\u6dfb\u52a0\u65b9\u6cd5\n- \u6ca1\u6709\u79c1\u6709\u65b9\u6cd5\u548c\u79c1\u6709\u5c5e\u6027\u7528symbol\u6a21\u62df\n- class\u9759\u6001\u65b9\u6cd5\u4e0e\u9759\u6001\u5c5e\u6027\n - class\u5b9a\u4e49\u7684\u9759\u6001\u65b9\u6cd5\u524d\u52a0static\u5173\u952e\u5b57\n - \u53ea\u80fd\u901a\u8fc7\u7c7b\u540d\u8c03\u7528\n - \u4e0d\u80fd\u901a\u8fc7\u5b9e\u4f8b\u8c03\u7528\n - \u53ef\u4e0e\u5b9e\u4f8b\u65b9\u6cd5\u91cd\u540d\n - \u9759\u6001\u65b9\u6cd5\u4e2d\u7684this\u6307\u5411\u7c7b\u800c\u975e\u5b9e\u4f8b\n - \u9759\u6001\u65b9\u6cd5\u53ef\u88ab\u7ee7\u627f\n - \u5728\u5b50\u7c7b\u4e2d\u53ef\u901a\u8fc7super\u65b9\u6cd5\u8c03\u7528\u7236\u7c7b\u7684\u9759\u6001\u65b9\u6cd5\n - class\u5185\u90e8\u6ca1\u6709\u9759\u6001\u5c5e\u6027\uff0c\u53ea\u80fd\u5728\u5916\u9762\u901a\u8fc7\u7c7b\u540d\u5b9a\u4e49\u3002\n- new target\u5c5e\u6027\u6307\u5411\u5f53\u524d\u7684\u6784\u9020\u51fd\u6570,\u4e0d\u80fd\u5728\u6784\u9020\u51fd\u6570\u5916\u90e8\u8c03\u7528\u4f1a\u62a5\u9519\n\n**\u7b49\u540c\u4e8ees5**\n\n```js\nParent.prototype = {\n constructor() { },\n print() { },\n}\n```\n\n- function\u6784\u9020\u5668\u539f\u578b\u65b9\u6cd5\u53ef\u88abObject.keys(Point.prototype)\u679a\u4e3e\u5230\uff0c\u9664\u8fc7constructor\n- \u91cd\u590d\u5b9a\u4e49Parent function ,\u4e4b\u524d\u7684\u4f1a\u88ab\u8986\u76d6\u6389\n- \u53ef\u901a\u8fc7\u5b9e\u4f8b\u7684__proto__\u5c5e\u6027\u5411\u539f\u578b\u6dfb\u52a0\u65b9\u6cd5\n- \u6ca1\u6709\u79c1\u6709\u5c5e\u6027\n\n\u6240\u6709\u539f\u578b\u65b9\u6cd5\u5c5e\u6027\u90fd\u53ef\u7528Object.getOwnPropertyNames(Point.prototype)\u8bbf\u95ee\u5230", "answer": "", "biz_type": 1, "qid": 539, "subject": "", "title": "\u4ecb\u7ecd class \u548c ES5 \u7684\u7c7b\u4ee5\u53ca\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fae\u533b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 493, "qtype": "short_answer", "short_answer": {"analysis": "### Array\n\n\u770b\u4e0b\u4ee3\u7801\u4f8b\u5b50\n\n```js\nvar a=[ ]\na instanceof Array\n// \u8f93\u51fa Array\n```\n\n\n```js\na.constructor == Array\n// \u8f93\u51fa\uff1a true\n```\n\n\u53bba\u7684\u539f\u578b\u94fe\u4e0a\u770b\u770b\u5230\u5e95\u7ee7\u627f\u4e86\u4ec0\u4e48\u4e1c\u897f\uff1a\n\n```js\na.__proto__.constructor\n// \u8f93\u51fa\uff1a\u0192 Array() { [native code] }\n```\n\n```js\na.__proto__.__proto__.constructor\n// \u8f93\u51fa\uff1a\u0192 Object() { [native code] }\n```\n\n```js\na.__proto__.__proto__.__proto__\n// \u8f93\u51fanull\n```\n\n\u7531\u6b64\u53ef\u4ee5\u770b\u51fa\uff1aa\u7684\u521b\u5efa\u8fc7\u7a0b\u662f `null->Object->Array`\uff0c\u6240\u4ee5\u8bf4 Array \u5c5e\u4e8eObject;\n\n\n\n\n", "answer": "", "biz_type": 1, "qid": 493, "subject": "", "title": "Array \u662f Object \u7c7b\u578b\u5417"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9177\u5bb6\u4e50"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 5, "qid": 535, "qtype": "short_answer", "short_answer": {"analysis": "### \u533a\u522b\n\n#### 1.\u8fd4\u56de\u503ctimeoutID \u4e0d\u4e00\u6837\n\n\u6bcf\u4e2asetTimeout \u4f1a\u5bf9\u5e94\u4e00\u4e2a timeoutID ,\u5373\u5b9a\u65f6\u5668\u7684\u7f16\u53f7\u3002\u8fd9\u4e2atimeoutID\u53ef\u4ee5\u4f20\u9012\u7ed9clearTimeout() \u6765\u53d6\u6d88\u5b9a\u65f6\u5668\u3002\n\n#### 2.code \u503c\u4e0d\u4e00\u6837\n\nsetTimeout \u8bed\u6cd5\n\n```js\nvar timeoutID = scope.setTimeout(function[, delay, arg1, arg2, ...]);\nvar timeoutID = scope.setTimeout(function[, delay]); \nvar timeoutID = scope.setTimeout(code[, delay]);\n```\n\n\u9664\u4e86\u53ef\u4ee5\u63a5\u53d7function,\u8fd8\u53ef\u4ee5\u63a5\u53d7code\u3002\n\n\u8fd9\u662f\u4e00\u4e2a\u53ef\u9009\u8bed\u6cd5\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u800c\u4e0d\u662ffunction \uff0c\u5728delay\u6beb\u79d2\u4e4b\u540e\u7f16\u8bd1\u548c\u6267\u884c\u5b57\u7b26\u4e32\u3002\n\n> \u4f46\u662f\u9700\u8981\u6ce8\u610f\uff0c\u4f7f\u7528\u8be5\u8bed\u6cd5\u662f\u4e0d\u63a8\u8350\u7684, \u539f\u56e0\u548c\u4f7f\u7528 eval()\u4e00\u6837\uff0c\u6709\u5b89\u5168\u98ce\u9669\u3002\n\n#### 3. \u6267\u884c\u7684\u65f6\u95f4\u4e0d\u4e00\u6837\n\n\u867d\u7136\u5728\u4e0d\u8bbe\u5b9adelay\u5ef6\u65f6\u65f6\u95f4\u7684\u60c5\u51b5\u4e0b\uff0c\u90fd\u662f\u9ed8\u8ba4\u5ef6\u65f60\u6beb\u79d2\u6267\u884c\uff0c\u4f46\u662f\nsetTimeout \u6709\u4e00\u4e2a\u663e\u8457\u7684\u7f3a\u9677\u5728\u4e8e\u65f6\u95f4\u662f\u4e0d\u7cbe\u786e\u7684\uff0csetTimeout \u53ea\u80fd\u4fdd\u8bc1\u5ef6\u65f6\u6216\u95f4\u9694\u4e0d\u5c0f\u4e8e\u8bbe\u5b9a\u7684\u65f6\u95f4\u3002\u56e0\u4e3a\u5b83\u4eec\u5b9e\u9645\u4e0a\u53ea\u662f\u628a\u4efb\u52a1\u6dfb\u52a0\u5230\u4e86\u4efb\u52a1\u961f\u5217\u4e2d\uff0c\u4f46\u662f\u5982\u679c\u524d\u9762\u7684\u4efb\u52a1\u8fd8\u6ca1\u6709\u6267\u884c\u5b8c\u6210\uff0c\u5b83\u4eec\u5fc5\u987b\u8981\u7b49\u5f85\u3002\n\n\n", "answer": "", "biz_type": 1, "qid": 535, "subject": "", "title": "setTimeout(1)\u548c setTimeout(2)\u4e4b\u95f4\u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fae\u533b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 5, "qid": 496, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 496, "subject": "", "title": "Async/Await \u600e\u4e48\u5b9e\u73b0"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fae\u533b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 502, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 502, "subject": "", "title": "ES6 \u4e2d let \u5757\u4f5c\u7528\u57df\u662f\u600e\u4e48\u5b9e\u73b0\u7684"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5b9d\u5b9d\u6811"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 515, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 515, "subject": "", "title": "formData \u548c\u539f\u751f\u7684 ajax \u6709\u4ec0\u4e48\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5b9d\u5b9d\u6811"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 516, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 516, "subject": "", "title": "\u4ecb\u7ecd\u4e0b\u8868\u5355\u63d0\u4ea4\uff0c\u548c formData \u6709\u4ec0\u4e48\u5173\u7cfb"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5b9d\u5b9d\u6811"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 521, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 521, "subject": "", "title": "\u5982\u4f55\u5bf9\u76f8\u5bf9\u8def\u5f84\u5f15\u7528\u8fdb\u884c\u4f18\u5316"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6d77\u5eb7\u5a01\u89c6"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 527, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 527, "subject": "", "title": "\u4ecb\u7ecd localstorage \u7684 api"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9177\u5bb6\u4e50"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 5, "qid": 542, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 542, "subject": "", "title": "\u4f7f\u7528\u95ed\u5305\u7279\u6743\u51fd\u6570\u7684\u4f7f\u7528\u573a\u666f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6d77\u98ce\u6559\u80b2"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 5, "qid": 552, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 552, "subject": "", "title": "Promise \u548c setTimeout \u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 556, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 556, "subject": "```js\nvar yideng = {\n bar: function () {\n return this.baz;\n },\n baz: 1,\n};\n(function () {\n console.log(typeof arguments[0]());\n})(yideng.bar);\n```\n", "title": "\u8bf7\u5199\u51fa\u6b63\u786e\u7684\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 557, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 557, "subject": "```js\nfunction test() {\n console.log(\"out\");\n}\n(function () {\n if (false) {\n function test() {\n console.log(\"in\");\n }\n test();\n }\n})();\n```\n", "title": "\u8bf7\u5199\u51fa\u6b63\u786e\u7684\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 558, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 558, "subject": "```js\nvar x = [typeof x, typeof y][1];\ntypeof x;\n```\n", "title": "\u8bf7\u5199\u51fa\u6b63\u786e\u7684\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 559, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 559, "subject": "```js\n(function (x) {\n delete x;\n return x;\n})(1);\n```\n", "title": "\u8bf7\u5199\u51fa\u6b63\u786e\u7684\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 560, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 560, "subject": "```js\nvar x = 1;\nif (function f() {}) {\n x += typeof f;\n}\nx;\n```\n", "title": "\u8bf7\u5199\u51fa\u6b63\u786e\u7684\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 561, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 561, "subject": "```js\nfunction f() {\n return f;\n}\nnew f() instanceof f;\n```\n", "title": "\u8bf7\u5199\u51fa\u6b63\u786e\u7684\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 562, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 562, "subject": "```js\nObject.prototype.a = \"a\";\nFunction.prototype.a = \"a1\";\nfunction Person() {}\nvar yideng = new Person();\nconsole.log(yideng.a);\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6b63\u786e\u6267\u884c\u7ed3\u679c\uff0c\u5e76\u89e3\u91ca\u539f\u56e0"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 563, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 563, "subject": "```js\nvar yideng = [0];\nif (yideng) {\n console.log(yideng == true);\n} else {\n console.log(\"yideng\");\n}\n```\n", "title": "\u8bf7\u5199\u51fa\u6b63\u786e\u7684\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 564, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 564, "subject": "```js\nfunction yideng() {\n return;\n {\n a: 1;\n }\n}\nvar result = yideng();\nconsole.log(result.a);\n```\n", "title": "\u8bf7\u5199\u51fa\u6b63\u786e\u7684\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 566, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 566, "subject": "```html\n<script>\n //\u4f7f\u7528\u672a\u5b9a\u4e49\u7684\u53d8\u91cfyideng\n yideng;\n console.log(1);\n</script>\n<script>\n console.log(2);\n</script>\n```\n", "title": "\u8bf7\u5199\u51fa\u6b63\u786e\u7684\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 567, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 567, "subject": "```js\nvar yideng = Array(3);\nyideng[0] = 2;\nvar result = yideng.map(function (elem) {\n return \"1\";\n});\nconsole.log(result);\n```\n", "title": "\u8bf7\u5199\u51fa\u6b63\u786e\u7684\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 570, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 570, "subject": "```js\n[1 < 2 < 3, 3 < 2 < 1];\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6b63\u786e\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 571, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 571, "subject": "```js\n2 == [[[2]]];\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6b63\u786e\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 572, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 572, "subject": "```js\nconsole.log(\"\u2708\ufe0f\".length);\n// 1.\u8ba1\u7b97\u4ee5\u4e0a\u5b57\u8282\u6bcf\u4f4d\u2708\ufe0f\u7684\u8d77\u7801\u70b9\n// 2.\u63cf\u8ff0\u8fd9\u4e9b\u5b57\u8282\u7684\u8d77\u7801\u70b9\u4ee3\u8868\u4ec0\u4e48\n```\n", "title": "\u8ba1\u7b97\u4ee5\u4e0a\u5b57\u8282\u6bcf\u4f4d \u2708\ufe0f \u7684\u8d77\u7801\u70b9\uff0c\u5e76\u63cf\u8ff0\u8fd9\u4e9b\u5b57\u8282\u7684\u8d77\u7801\u70b9\u4ee3\u8868\u4ec0\u4e48"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 573, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 573, "subject": "```js\nvar yidenga = Function.length,\n yidengb = new Function().length;\nconsole.log(yidenga === yidengb);\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6b63\u786e\u6267\u884c\u7ed3\u679c\uff0c\u5e76\u89e3\u91ca\u539f\u56e0"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 574, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 574, "subject": "```js\nvar length = 10;\nfunction fn() {\n console.log(this.length);\n}\nvar yideng = {\n length: 5,\n method: function (fn) {\n fn();\n arguments[0]();\n },\n};\nyideng.method(fn, 1);\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6b63\u786e\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 575, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 575, "subject": "```js\nvar yi = new Date(\"2018-08-20\"),\n deng = new Date(2018, 08, 20);\n[yi.getDay() === deng.getDay(), yi.getMonth() === deng.getMonth()];\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6b63\u786e\u6267\u884c\u7ed3\u679c\uff0c\u5e76\u89e3\u91ca\u539f\u56e0"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 576, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 576, "subject": "```js\nfor (\n let i = (setTimeout(() => console.log(\"a->\", i)), 0);\n setTimeout(() => console.log(\"b->\", i)), i < 2;\n i++\n) {\n i++;\n}\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6b63\u786e\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 577, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 577, "subject": "```js\n[typeof null, null instanceof Object];\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6b63\u786e\u6267\u884c\u7ed3\u679c\uff0c\u5e76\u89e3\u91ca\u539f\u56e0"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 578, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 578, "subject": "```html\n<textarea maxlength=\"10\" id=\"yideng\"></textarea>\n<script>\n document.getElementById(\"yideng\").value = \"a\".repeat(10) + \"b\";\n</script>\n```\n", "title": "\u8bf7\u95ee\u5f53\u524d textarea \u6587\u672c\u6846\u5c55\u793a\u7684\u5185\u5bb9\u662f\u4ec0\u4e48\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 579, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 579, "subject": "```js\nfunction sidEffecting(ary) {\n arr[0] = arr[2];\n}\nfunction yideng(a, b, c = 3) {\n c = 10;\n sidEffecting(arguments);\n return a + b + c;\n}\nyideng(1, 1, 1);\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6b63\u786e\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 580, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 580, "subject": "```js\nyideng();\nvar flag = true;\nif (flag) {\n function yideng() {\n console.log(\"yideng1\");\n }\n} else {\n function yideng() {\n console.log(\"yideng2\");\n }\n}\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6b63\u786e\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 581, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 581, "subject": "```js\nvar min = Math.min(),\n max = Math.max();\nconsole.log(min < max);\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6b63\u786e\u6267\u884c\u7ed3\u679c\uff0c\u5e76\u89e3\u91ca\u4e3a\u4ec0\u4e48"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f1a\u5c0f\u4e8c"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 593, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 593, "subject": "```js\nconsole.log(\"hello\" + (1 < 2) ? \"word\" : \"me\");\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6b63\u786e\u6267\u884c\u7ed3\u679c\uff0c\u5e76\u89e3\u91ca\u539f\u56e0\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f1a\u5c0f\u4e8c"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 594, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 594, "subject": "```js\nvar a = (b = 1);\n(function () {\n var a = (b = 2);\n})();\nconsole.log(a, b);\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6b63\u786e\u6267\u884c\u7ed3\u679c\uff0c\u5e76\u89e3\u91ca\u539f\u56e0\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f1a\u5c0f\u4e8c"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 595, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 595, "subject": "```js\nif ([] instanceof Object) {\n console.log(typeof null);\n} else {\n console.log(typeof undefined);\n}\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6b63\u786e\u6267\u884c\u7ed3\u679c\uff0c\u5e76\u89e3\u91ca\u539f\u56e0\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f1a\u5c0f\u4e8c"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 596, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 596, "subject": "```js\nvar obj = {};\nobj.name = \"first\";\nvar peo = obj;\npeo.name = \"second\";\nconsole.log(obj.name);\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6b63\u786e\u6267\u884c\u7ed3\u679c\uff0c\u5e76\u89e3\u91ca\u539f\u56e0\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f1a\u5c0f\u4e8c"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 597, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 597, "subject": "```js\nfunction say(word) {\n let word = \"hello\";\n console.log(word);\n}\nsay(\"hello Lili\");\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u6b63\u786e\u6267\u884c\u7ed3\u679c\uff0c\u5e76\u89e3\u91ca\u539f\u56e0\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f1a\u5c0f\u4e8c"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 602, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 602, "subject": "```js\nfunction fun(n, o) {\n console.log(o);\n return {\n fun: function (m) {\n return fun(m, n);\n },\n };\n}\nvar b = fun(0).fun(1).fun(2).fun(3);\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee3\u7801\u7684\u6b63\u786e\u6267\u884c\u7ed3\u679c\uff0c\u5e76\u89e3\u91ca\u539f\u56e0\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f1a\u5c0f\u4e8c"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 617, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 617, "subject": "", "title": "\u600e\u4e48\u5224\u65ad\u5f15\u7528\u7c7b\u578b\u6570\u636e\uff0c\u517c\u5bb9\u5224\u65ad\u539f\u59cb\u7c7b\u578b\u6570\u636e\u5462\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 8, "qid": 626, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 626, "subject": "", "title": "\u5206\u522b\u5bf9\u4ee5\u4e0b\u6570\u7ec4\u8fdb\u884c\u53bb\u91cd\uff0c1:[1,'1',2,'2',3]\uff0c2:[1,[1,2,3['1','2','3'],4],5,6]"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 627, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 627, "subject": "", "title": "\u7b80\u8ff0 JavaScript \u4e2d\u7684\u51fd\u6570\u7684\u51e0\u79cd\u8c03\u7528\u65b9\u5f0f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 9, "qid": 643, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 643, "subject": "", "title": "\u8bf4\u4e00\u4e0b let\u3001const \u7684\u5b9e\u73b0\uff0c\u52a8\u624b\u5b9e\u73b0\u4e00\u4e0b"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 649, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 649, "subject": "", "title": "scrollview \u5982\u4f55\u8fdb\u884c\u53c8\u80fd\u4f18\u5316(\u4f8b\u5982 page=100 \u65f6\uff0c\u5f80\u4e0a\u6eda\u52a8)"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 650, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 650, "subject": "", "title": "\u539f\u751f JavaScript \u83b7\u53d6 ul \u4e2d\u7684\u7b2c\u4e8c\u4e2a li \u91cc\u8fb9\u7684 p \u6807\u7b7e\u7684\u5185\u5bb9"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 657, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 657, "subject": "", "title": "\u6570\u7ec4\u622a\u53d6\u63d2\u5165 splice\uff0cpush \u8fd4\u56de\u503c\uff0c\u6570\u7ec4\u7684\u6808\u65b9\u6cd5\u3001\u961f\u5217\u65b9\u6cd5\u3001\u6392\u5e8f\u65b9\u6cd5\u3001\u64cd\u4f5c\u65b9\u6cd5\u3001\u8fed\u4ee3\u65b9\u6cd5\u8bf4\u4e00\u4e0b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 658, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 658, "subject": "", "title": "\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u5199\u4e2a\u65b9\u6cd5\u7528 Object.prototype.toString \u5224\u65ad\u4f20\u5165\u6570\u636e\u7684\u7c7b\u578b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fc3\u5a31"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 661, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 661, "subject": "```js\nsetTimeout(function () {\n console.log(1);\n}, 0);\nnew Promise(function executor(resolve) {\n console.log(2);\n for (var i = 0; i < 10000; i++) {\n i == 9999 && resolve();\n }\n console.log(3);\n}).then(function () {\n console.log(4);\n});\nconsole.log(5);\n```\n", "title": "\u4ee5\u4e0b\u4ee3\u7801\u8f93\u51fa\u4ec0\u4e48\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 662, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 662, "subject": "", "title": "switch case\uff0ccase \u5177\u4f53\u662f\u600e\u4e48\u6bd4\u8f83\u7684\uff0c\u54ea\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u8d70\u5230 default"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 663, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 663, "subject": "", "title": "\u8bf4\u4e0b typeof()\u5404\u79cd\u7c7b\u578b\u7684\u8fd4\u56de\u503c\uff1finstanceof \u5462\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4", "58"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 6, "qid": 666, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 666, "subject": "", "title": "genertor \u7684\u5b9e\u73b0\u539f\u7406"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 5, "qid": 673, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 673, "subject": "", "title": "\u5224\u65ad\u662f\u5426\u662f\u6570\u7ec4\u7684\u65b9\u6cd5"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u8001\u864e"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 679, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 679, "subject": "```js\nvar s = \"laohu\";\ns[0] = 1;\nconsole.log(s); //laohu\nvar s = \"laohu\";\ns += 2020;\nconsole.log(s); // laohu2020\n// \u4e0a\u9762\u4e24\u884c\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u8f93\u51fa\n```\n", "title": "\u7ed9\u51fa\u7684\u4e24\u884c\u4ee3\u7801\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u8f93\u51fa"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9177\u72d7"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 684, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 684, "subject": "", "title": "\u5bf9 service worker \u7684\u7406\u89e3"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9177\u5bb6\u4e50"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 537, "qtype": "short_answer", "short_answer": {"analysis": "\n- \u6784\u9020\u51fd\u6570\u91cc\u9762\u662f\u540c\u6b65\u6267\u884c\u7684\uff0c\u65e0\u6cd5\u53d6\u6d88\n- then\u91cc\u9762\u662f\u5f02\u6b65\u7684\uff0c\u5c5e\u4e8e\u5fae\u4efb\u52a1\uff1b\n\n\u770b\u4e2a\u4f8b\u5b50\n\n```js\nnew Promise(function (resolve, reject) {\n // \u8fd9\u91cc\u9762\u5c5e\u4e8e\u5b8f\u4efb\u52a1\uff0c\u662f\u540c\u6b65\u6267\u884c\u7684\n console.log('macrotask');\n resolve('result');\n}).then(function (value) {\n // `then`\u4e2d\u7684\u56de\u8c03\u51fd\u6570\u5c5e\u4e8e\u5fae\u4efb\u52a1\uff0c\u5728`resolve`\u6267\u884c\u540e\u88ab\u63a8\u5230\u5fae\u4efb\u52a1\u961f\u5217\u7b49\u5f85\u6267\u884c\n console.log('microtask');\n console.log(value === 'result'); // true\n});\n```\n\n\n", "answer": "", "biz_type": 1, "qid": 537, "subject": "", "title": "promise \u91cc\u9762\u548c then \u91cc\u9762\u6267\u884c\u6709\u4ec0\u4e48\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9177\u72d7"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 680, "qtype": "short_answer", "short_answer": {"analysis": "\n#### 1.Chrome \u63d0\u4f9b\u7ed9\u5f00\u53d1\u8005\u7684\u529f\u80fd\u5341\u5206\u5f3a\u5927\uff0c\u5728\u5f00\u53d1\u8005\u5de5\u5177\u4e2d\uff0c\u6211\u4eec\u8fdb\u884c\u5982\u4e0b\u9009\u62e9\u8c03\u51fa FPS meter \u9009\u9879\uff1a\n \n![](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-680-dev1.png)\n\n\u901a\u8fc7\u8fd9\u4e2a\u9009\u9879\uff0c\u53ef\u4ee5\u5f00\u542f\u9875\u9762\u5b9e\u65f6 Frame Rate (\u5e27\u7387) \u89c2\u6d4b\u53ca\u9875\u9762 GPU \u4f7f\u7528\u7387\u3002\u4f46\u662f\u7f3a\u70b9\u592a\u591a\u4e86\uff0c\u8fd9\u4e2a\u53ea\u80fd\u4e00\u6b21\u89c2\u6d4b\u4e00\u5230\u51e0\u4e2a\u9875\u9762\uff0c\u800c\u4e14\u9700\u8981\u4eba\u5de5\u5b9e\u65f6\u89c2\u6d4b\u3002\u6570\u636e\u53ea\u80fd\u662f\u4e3b\u89c2\u611f\u53d7\uff0c\u5e76\u6ca1\u6709\u4e00\u4e2a\u5341\u5206\u7cbe\u786e\u7684\u6570\u636e\u4e0d\u65ad\u4e0a\u62a5\u6216\u8005\u88ab\u6536\u96c6\n\n\n#### 2.\u501f\u52a9 Frame Timing API\n\nFrame Timing API \u662f Web Performance Timing API \u6807\u51c6\u4e2d\u7684\u5176\u4e2d\u4e00\u4f4d\u6210\u5458\u3002\u662f\u901a\u8fc7\u4e00\u4e2a\u63a5\u53e3\u83b7\u53d6\u5e27\u76f8\u5173\u7684\u6027\u80fd\u6570\u636e\uff0c\u4f8b\u5982\u6bcf\u79d2\u5e27\u6570\u548cTTF.\n\n\u4ee5 Navigation Timing, Performance Timeline, Resource Timing \u4e3a\u4f8b\u5b50\uff0c\u5bf9\u4e8e\u517c\u5bb9\u5b83\u7684\u6d4f\u89c8\u5668\uff0c\u5b83\u4ee5\u53ea\u8bfb\u5c5e\u6027\u7684\u5f62\u5f0f\u5bf9\u5916\u66b4\u9732\u6302\u8f7d\u5728 window.performance \u4e0a\u3002\n\n\u5176\u4e2dTiming\u4e2d\u7684\u5c5e\u6027\u5bf9\u5e94\u65f6\u95f4\u70b9\u5982\u4e0b\uff1a\n\n![](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-680-frame.png)\n\n\u901a\u8fc7window.performance.timing\uff0c\u5c31\u53ef\u4ee5\u7edf\u8ba1\u51fa\u9875\u9762\u6bcf\u4e2a\u91cd\u8981\u8282\u70b9\u7684\u8017\u65f6\n\n\u501f\u52a9 Web Performance Timing API \u4e2d\u7684 Frame Timing API\uff0c\u53ef\u4ee5\u8f7b\u677e\u7684\u62ff\u5230\u6bcf\u4e00\u5e27\u4e2d\uff0c\u4e3b\u7ebf\u7a0b\u4ee5\u53ca\u5408\u6210\u7ebf\u7a0b\u7684\u65f6\u95f4\u3002\u6216\u8005\u66f4\u52a0\u5bb9\u6613\uff0c\u76f4\u63a5\u62ff\u5230\u6bcf\u4e00\u5e27\u7684\u8017\u65f6\u3002\n\n\u83b7\u53d6 Render \u4e3b\u7ebf\u7a0b\u548c\u5408\u6210\u7ebf\u7a0b\u7684\u8bb0\u5f55\uff0c\u6bcf\u6761\u8bb0\u5f55\u5305\u542b\u7684\u4fe1\u606f\u57fa\u672c\u5982\u4e0b\uff0c\u4ee3\u7801\u793a\u610f\uff0c\uff08\u53c2\u8003\u81f3Developer feedback needed: Frame Timing API\uff09\uff1a\n\n```js\nvar rendererEvents = window.performance.getEntriesByType(\"renderer\");\nvar compositeThreadEvents = window.performance.getEntriesByType(\"composite\");\n\n//\u6216\u8005\nvar observer = new PerformanceObserver(function(list) {\n var perfEntries = list.getEntries();\n for (var i = 0; i < perfEntries.length; i++) {\n console.log(\"frame: \", perfEntries[i]);\n }\n});\n \n// subscribe to Frame Timing\nobserver.observe({entryTypes: ['frame']});\n\n// \u7ed3\u679c\n// {\n// sourceFrameNumber: 120,\n// startTime: 1342.549374253\n// cpuTime: 6.454313323\n// }\n\n//\u6bcf\u4e2a\u8bb0\u5f55\u90fd\u5305\u62ec\u552f\u4e00\u7684 Frame Number\u3001Frame \u5f00\u59cb\u65f6\u95f4\u4ee5\u53ca cpuTime \u65f6\u95f4\u3002\u901a\u8fc7\u8ba1\u7b97\u6bcf\u4e00\u6761\u8bb0\u5f55\u7684 startTime \uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7b97\u51fa\u6bcf\u4e24\u5e27\u95f4\u7684\u95f4\u9694\uff0c\u4ece\u800c\u5f97\u5230\u52a8\u753b\u7684\u5e27\u7387\u662f\u5426\u80fd\u591f\u8fbe\u5230 60 FPS\u3002\n```\n \n**\u4f46\u662f\u3002(\u91cd\u70b9\u6765\u4e86) \u73b0\u5728 Frame Timing API \u7684\u517c\u5bb9\u6027\u4e0d\u592a\u53cb\u597d\uff0c\u8fd8\u6ca1\u6709\u4efb\u4f55\u6d4f\u89c8\u5668\u652f\u6301\uff0c\u5c5e\u4e8e\u5b8f\u89c2\u8bd5\u9a8c\u6027\u9636\u6bb5\uff0c\u62ac\u8d70\u4e0b\u4e00\u4e2a**\n\n#### 3.requestAnimationFrame API\n\nrequestAnimationFrame \u544a\u8bc9\u6d4f\u89c8\u5668\u60a8\u5e0c\u671b\u6267\u884c\u52a8\u753b\u5e76\u8bf7\u6c42\u6d4f\u89c8\u5668\u8c03\u7528\u6307\u5b9a\u7684\u51fd\u6570\u5728\u4e0b\u4e00\u6b21\u91cd\u7ed8\u4e4b\u524d\u66f4\u65b0\u52a8\u753b\u3002\n\n\u5f53\u51c6\u5907\u597d\u66f4\u65b0\u5c4f\u5e55\u753b\u9762\u65f6\u4f60\u5c31\u5e94\u7528\u6b64\u65b9\u6cd5\u3002\u8fd9\u4f1a\u8981\u6c42\u52a8\u753b\u51fd\u6570\u5728\u6d4f\u89c8\u5668\u4e0b\u6b21\u91cd\u7ed8\u524d\u6267\u884c\u3002\u56de\u8c03\u7684\u6b21\u6570\u5e38\u662f\u6bcf\u79d2 60 \u6b21\uff0c\u5927\u591a\u6570\u6d4f\u89c8\u5668\u901a\u5e38\u5339\u914d W3C \u6240\u5efa\u8bae\u7684\u5237\u65b0\u7387\u3002\n\n**\u4f7f\u7528 requestAnimationFrame \u8ba1\u7b97 FPS \u539f\u7406**\n\n\u539f\u7406\u662f\uff0c\u6b63\u5e38\u800c\u8a00 requestAnimationFrame \u8fd9\u4e2a\u65b9\u6cd5\u5728\u4e00\u79d2\u5185\u4f1a\u6267\u884c 60 \u6b21\uff0c\u4e5f\u5c31\u662f\u4e0d\u6389\u5e27\u7684\u60c5\u51b5\u4e0b\u3002\u5047\u8bbe\u52a8\u753b\u5728\u65f6\u95f4 A \u5f00\u59cb\u6267\u884c\uff0c\u5728\u65f6\u95f4 B \u7ed3\u675f\uff0c\u8017\u65f6 x ms\u3002\u800c\u4e2d\u95f4 requestAnimationFrame \u4e00\u5171\u6267\u884c\u4e86 n \u6b21\uff0c\u5219\u6b64\u6bb5\u52a8\u753b\u7684\u5e27\u7387\u5927\u81f4\u4e3a\uff1a`n / (B - A)`\u3002\n\n\u4ee3\u7801\u5982\u4e0b\uff0c\u80fd\u8fd1\u4f3c\u8ba1\u7b97\u6bcf\u79d2\u9875\u9762\u5e27\u7387\uff0c\u4ee5\u53ca\u6211\u4eec\u989d\u5916\u8bb0\u5f55\u4e00\u4e2a allFrameCount\uff0c\u7528\u4e8e\u8bb0\u5f55 rAF \u7684\u6267\u884c\u6b21\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u6bcf\u6b21\u52a8\u753b\u7684\u5e27\u7387 \uff1a\n\n```js\nvar rAF = function () {\n return (\n window.requestAnimationFrame ||\n window.webkitRequestAnimationFrame ||\n function (callback) {\n window.setTimeout(callback, 1000 / 60);\n }\n );\n}();\n\nvar frame = 0;\nvar allFrameCount = 0;\nvar lastTime = Date.now();\nvar lastFameTime = Date.now();\n\nvar loop = function () {\n var now = Date.now();\n var fs = (now - lastFameTime);\n var fps = Math.round(1000 / fs);\n\n lastFameTime = now;\n // \u4e0d\u7f6e 0\uff0c\u5728\u52a8\u753b\u7684\u5f00\u5934\u53ca\u7ed3\u5c3e\u8bb0\u5f55\u6b64\u503c\u7684\u5dee\u503c\u7b97\u51fa FPS\n allFrameCount++;\n frame++;\n\n if (now > 1000 + lastTime) {\n var fps = Math.round((frame * 1000) / (now - lastTime));\n console.log(`${new Date()} 1S\u5185 FPS\uff1a`, fps);\n frame = 0;\n lastTime = now;\n };\n\n rAF(loop);\n}\n\nloop();\n```\n\n\u5728\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5f88\u597d\u7684\u5f97\u51fa Web \u52a8\u753b\u7684\u5e27\u7387\u3002\n\n\u5982\u679c\u9700\u8981\u7edf\u8ba1\u67d0\u4e2a\u7279\u5b9a\u52a8\u753b\u8fc7\u7a0b\u7684\u5e27\u7387\uff0c\u53ea\u9700\u8981\u5728\u52a8\u753b\u5f00\u59cb\u548c\u7ed3\u5c3e\u4e24\u5904\u5206\u522b\u8bb0\u5f55 allFrameCount \u8fd9\u4e2a\u6570\u503c\u5927\u5c0f\uff0c\u518d\u9664\u4ee5\u4e2d\u95f4\u6d88\u8017\u7684\u65f6\u95f4\uff0c\u4e5f\u53ef\u4ee5\u5f97\u51fa\u7279\u5b9a\u52a8\u753b\u8fc7\u7a0b\u7684 FPS \u503c\u3002\n\n\u8fd9\u4e2a\u65b9\u6cd5\u8ba1\u7b97\u7684\u7ed3\u679c\u548c\u771f\u5b9e\u7684\u5e27\u7387\u662f\u5b58\u5728\u8bef\u5dee\u7684\uff0c\u56e0\u4e3a\u5b83\u662f\u5c06\u6bcf\u4e24\u6b21\u4e3b\u7ebf\u7a0b\u6267\u884c javascript \u7684\u65f6\u95f4\u95f4\u9694\u5f53\u6210\u4e00\u5e27\uff0c\u800c\u975e\u4e0a\u9762\u8bf4\u7684\u4e3b\u7ebf\u7a0b\u52a0\u5408\u6210\u7ebf\u7a0b\u6240\u6d88\u8017\u7684\u65f6\u95f4\u4e3a\u4e00\u5e27\u3002\u4f46\u662f\u5bf9\u4e8e\u73b0\u9636\u6bb5\u800c\u8a00\uff0c\u7b97\u662f\u4e00\u79cd\u53ef\u53d6\u7684\u65b9\u6cd5\u3002", "answer": "", "biz_type": 1, "qid": 680, "subject": "", "title": "\u52a8\u753b\u6027\u80fd\u5982\u4f55\u68c0\u6d4b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 635, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u89e3\u51b3\u65b9\u6848\n\n#### 1.\u7f13\u5b58DOM\u5bf9\u8c61\n\n\u6bd4\u5982\u5728\u5faa\u73af\u4e4b\u524d\u5c31\u5c06\u8282\u70b9\u83b7\u53d6\u5e76\u7f13\u5b58\u5230\u5185\u5b58\uff0c\u5728\u5faa\u73af\u5185\u90e8\u76f4\u63a5\u5f15\u7528\uff0c\u800c\u4e0d\u662f\u91cd\u65b0\u67e5\u8be2\uff1b\n\n#### 2.\u6587\u6863\u788e\u7247\n\n\uff081\uff09document.createDocumentFragment() \u521b\u5efa\u7684\u6587\u6863\u788e\u7247\u662f\u4e2a\u865a\u62df\u8282\u70b9\u5bf9\u8c61\uff0c\u5bf9\u5b83\u7684\u64cd\u4f5c\u4e0d\u4f1a\u5f71\u54cd\u771f\u5b9edom\uff0c\u5bf9\u5b83\u8fdb\u884c\u9891\u7e41\u64cd\u4f5c\uff0c\u64cd\u4f5c\u5b8c\u6210\u518d\u4e00\u6b21\u6027\u6dfb\u52a0\u5230\u771f\u5b9e\u7684DOM\u4e2d\uff1b\n\n\uff082\uff09\u628a\u9700\u8981\u590d\u6742\u64cd\u4f5c\u7684\u5143\u7d20\uff0c\u5148\u4ece\u9875\u9762\u79fb\u9664\u518d\u8fdb\u884c\u64cd\u4f5c\uff0c\u64cd\u4f5c\u5b8c\u6210\u518d\u6dfb\u52a0\u56de\u6765\uff1b\n\n\uff083\uff09\u628a\u9700\u8981\u590d\u6742\u64cd\u4f5c\u7684\u5143\u7d20\u590d\u5236\u4e00\u4e2a\u526f\u672ccloneNode()\uff0c\u5728\u5185\u5b58\u4e2d\u8fdb\u884c\u64cd\u4f5c\u518d\u66ff\u6362\u65e7\u7684\uff1b\n\n#### 3.\u4f7f\u7528innerHtml\u4ee3\u66ff\u9ad8\u9891\u7684appendChild\uff1b\n\n#### 4.RAF\n\n\u628a\u53ef\u80fd\u5bfc\u81f4\u91cd\u7ed8\u7684\u64cd\u4f5c\u653e\u5230RAF\uff08requestAnimationFrame\uff09\u4e2d\uff0c\u6d4f\u89c8\u5668\u7a7a\u95f2\u7684\u65f6\u5019\u53bb\u5904\u7406\uff1b\n\n#### 5.\u865a\u62dfDOM\n\nVirtual DOM\u672c\u8d28\u662f\u4e00\u4e2aJS\u5bf9\u8c61\uff0cDOM diff\u4e4b\u540e\u6700\u540e\u518d\u6279\u91cf\u7684\u66f4\u65b0\u771f\u5b9e\u7684DOM\u7ed3\u6784\uff1b", "answer": "", "biz_type": 1, "qid": 635, "subject": "", "title": "\u4e00\u4e2a dom \u5fc5\u987b\u8981\u64cd\u4f5c\u51e0\u767e\u6b21\uff0c\u8be5\u5982\u4f55\u89e3\u51b3\uff0c\u5982\u4f55\u4f18\u5316\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9177\u72d7"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 624, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u5f02\u6b65\u7f16\u7a0b\u6a21\u578b\n\nJavascript\u8bed\u8a00\u7684\u6267\u884c\u73af\u5883\u662f\"\u5355\u7ebf\u7a0b\"\u3002\u4e5f\u5c31\u662f\u6307\u4e00\u6b21\u53ea\u80fd\u5b8c\u6210\u4e00\u4ef6\u4efb\u52a1\u3002\u5982\u679c\u6709\u591a\u4e2a\u4efb\u52a1\uff0c\u5c31\u5fc5\u987b\u6392\u961f\uff0c\u4f9d\u6b21\u6267\u884c\u4efb\u52a1\u3002\n\n\u8fd9\u79cd\u6a21\u5f0f\u5b9e\u73b0\u8d77\u6765\u867d\u7136\u76f8\u5bf9\u7b80\u5355\uff0c\u6267\u884c\u73af\u5883\u76f8\u5bf9\u5355\u7eaf\uff0c\u4f46\u53ea\u8981\u6709\u4e00\u4e2a\u4efb\u52a1\u8017\u65f6\u5f88\u957f\uff0c\u540e\u9762\u7684\u4efb\u52a1\u90fd\u5fc5\u987b\u6392\u961f\u7b49\u7740\uff0c\u4f1a\u62d6\u5ef6\u6574\u4e2a\u7a0b\u5e8f\u7684\u6267\u884c\u3002\u5e38\u89c1\u7684\u6d4f\u89c8\u5668\u65e0\u54cd\u5e94\uff08\u5047\u6b7b\uff09\uff0c\u5f80\u5f80\u5c31\u662f\u56e0\u4e3a\u67d0\u4e00\u6bb5Javascript\u4ee3\u7801\u957f\u65f6\u95f4\u8fd0\u884c\uff08\u6bd4\u5982\u6b7b\u5faa\u73af\uff09\uff0c\u5bfc\u81f4\u6574\u4e2a\u9875\u9762\u5361\u5728\u8fd9\u4e2a\u5730\u65b9\uff0c\u5176\u4ed6\u4efb\u52a1\u65e0\u6cd5\u6267\u884c\u3002\n\n\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cJavascript\u8bed\u8a00\u5c06\u4efb\u52a1\u7684\u6267\u884c\u6a21\u5f0f\u5206\u6210\u4e24\u79cd\uff1a\u540c\u6b65\u548c\u5f02\u6b65\u3002\u5e38\u89c1\u7684\u5f02\u6b65\u7f16\u7a0b\u65b9\u6848\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\n- \u56de\u8c03\u51fd\u6570\uff08Callback\uff09\n- \u4e8b\u4ef6\u76d1\u542c\n- \u89c2\u5bdf\u8005\u6a21\u5f0f\uff08\u6d88\u606f\u8ba2\u9605/\u53d1\u5e03\uff09\n- Promise/A+ (es6)\n- \u751f\u6210\u5668Generators/ yield (es6)\n- async/await (es7)\n \n#### \u56de\u8c03\u51fd\u6570\uff08Callback\uff09\n\n\u56de\u8c03\u51fd\u6570\u53ef\u4ee5\u8bf4\u662fJavascript\u5f02\u6b65\u7f16\u7a0b\u6700\u57fa\u672c\u7684\u65b9\u6cd5\u3002\n\n```js\nfunction loadingDo(callback){\n setTimeout(() => {\n callback()\n }, 2000);\n}\nfunction printMe(){\n console.log('\u6211\u662f: \u56de\u8c03\u51fd\u6570\u3002\u3002\u3002')\n}\nloadingDo(printMe)\n```\n\n\u56de\u8c03\u51fd\u6570\u5f80\u5f80\u5c31\u662f\u8c03\u7528\u7528\u6237\u63d0\u4f9b\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5f80\u5f80\u662f\u4ee5\u53c2\u6570\u7684\u5f62\u5f0f\u63d0\u4f9b\u7684\u3002\u5f88\u5bb9\u6613\u5199\u51fa\u56de\u8c03\u5730\u72f1\u5f0f\u4ee3\u7801\u3002\n\n```js\nfn(() => {\n fn1(() => {\n fn2(() => {\n // ...\n })\n })\n})\n```\n\n\u56de\u8c03\u51fd\u6570\u7684\u4f18\u70b9\u662f\u7b80\u5355\u3001\u6613\u7406\u89e3\u548c\u5b9e\u73b0\uff0c\u7f3a\u70b9\u662f\u4e0d\u5229\u4e8e\u4ee3\u7801\u7684\u9605\u8bfb\u548c\u7ef4\u62a4\uff0c\u5404\u4e2a\u90e8\u5206\u4e4b\u95f4\u9ad8\u5ea6\u8026\u5408,\u4f9d\u8d56\u6027\u5f88\u5f3a\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u7ed3\u6784\u6df7\u4e71\u3001\u6d41\u7a0b\u96be\u4ee5\u8ffd\u8e2a\uff08\u5c24\u5176\u662f\u591a\u4e2a\u56de\u8c03\u51fd\u6570\u5d4c\u5957\u7684\u60c5\u51b5\uff09\uff0c\u800c\u4e14\u6bcf\u4e2a\u4efb\u52a1\u53ea\u80fd\u6307\u5b9a\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u3002\u6b64\u5916\u5b83\u4e0d\u80fd\u4f7f\u7528 try catch \u6355\u83b7\u9519\u8bef\uff0c\u4e0d\u80fd\u76f4\u63a5 return\u3002\n\n#### \u4e8b\u4ef6\u76d1\u542c\n\n\u4e8b\u4ef6\u76d1\u542c\u6a21\u5f0f\u4e0b\uff0c\u5f02\u6b65\u4efb\u52a1\u7684\u6267\u884c\u4e0d\u53d6\u51b3\u4e8e\u4ee3\u7801\u7684\u987a\u5e8f\uff0c\u800c\u53d6\u51b3\u4e8e\u67d0\u4e2a\u4e8b\u4ef6\u662f\u5426\u53d1\u751f\u3002\u5982 click\u4e8b\u4ef6\uff0cajax/websocket\u4e8b\u4ef6\u7b49\u3002\n\n```js\n$(\"#btn\").click(function(){\n // ...\n})\n```\n\n\u4e8b\u4ef6\u76d1\u542c\u7684\u4f18\u70b9\u662f\u6bd4\u8f83\u5bb9\u6613\u7406\u89e3\uff0c\u53ef\u4ee5\u7ed1\u5b9a\u591a\u4e2a\u4e8b\u4ef6\uff0c\u6bcf\u4e2a\u4e8b\u4ef6\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u800c\u4e14\u53ef\u4ee5\"\u53bb\u8026\u5408\"\uff0c\u6709\u5229\u4e8e\u5b9e\u73b0\u6a21\u5757\u5316\u3002\u7f3a\u70b9\u662f\u6574\u4e2a\u7a0b\u5e8f\u90fd\u8981\u53d8\u6210\u4e8b\u4ef6\u9a71\u52a8\u578b\uff0c\u8fd0\u884c\u6d41\u7a0b\u4f1a\u53d8\u5f97\u5f88\u4e0d\u6e05\u6670\u3002\u901a\u8fc7\u4ee3\u7801\u4e0d\u80fd\u5f88\u660e\u786e\u7684\u5224\u65ad\u51fa\u4e3b\u6d41\u7a0b\u3002\n\n#### \u89c2\u5bdf\u8005\u6a21\u5f0f\uff08\u6d88\u606f\u8ba2\u9605/\u53d1\u5e03\uff09\n\n\u89c2\u5bdf\u8005\u6a21\u5f0f\uff0c\u53c8\u79f0\u4e3a\u6d88\u606f\u8ba2\u9605/\u53d1\u5e03\u6a21\u5f0f\u3002\u5b83\u7684\u542b\u4e49\u662f\uff0c\u6211\u4eec\u5148\u5047\u8bbe\u6709\u4e00\u4e2a\u201c\u4fe1\u53f7\u4e2d\u5fc3\u201d\uff0c\u5f53\u67d0\u4e2a\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\u5c31\u5411\u4fe1\u53f7\u4e2d\u5fc3\u53d1\u51fa\u4e00\u4e2a\u4fe1\u53f7\uff08\u4e8b\u4ef6\uff09\uff0c\u7136\u540e\u4fe1\u53f7\u4e2d\u5fc3\u6536\u5230\u8fd9\u4e2a\u4fe1\u53f7\u4e4b\u540e\u5c06\u4f1a\u8fdb\u884c\u5e7f\u64ad\u3002\u5982\u679c\u6709\u5176\u4ed6\u4efb\u52a1\u8ba2\u9605\u4e86\u8be5\u4fe1\u53f7\uff0c\u90a3\u4e48\u8fd9\u4e9b\u4efb\u52a1\u5c31\u4f1a\u6536\u5230\u4e00\u4e2a\u901a\u77e5\uff0c\u7136\u540e\u6267\u884c\u4efb\u52a1\u76f8\u5173\u7684\u903b\u8f91\u3002\n\n```js\nmyObserve = {\n tasklist: [],\n <!--\u6dfb\u52a0\u8ba2\u9605-->\n subscribe: function(){\n ...\n },\n <!--\u53d6\u6d88\u8ba2\u9605-->\n unsubscribe: function(){\n ...\n },\n <!--\u5177\u4f53\u6267\u884c-->\n publish: function(){\n ...\n }\n}\nmyObserve.subscribe('done', function () {\n console.log('end');\n});\nsetTimeout(function () {\n myObserve.publish('done')\n}, 2000);\n```\n\n\u89c2\u5bdf\u8005\u6a21\u5f0f\u4e0e\u201c\u4e8b\u4ef6\u76d1\u542c\u201d\u7c7b\u4f3c\uff0c\u4f46\u662f\u660e\u663e\u4f18\u4e8e\u540e\u8005\u3002\u56e0\u4e3a\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u201c\u6d88\u606f\u4e2d\u5fc3\u201d\uff0c\u4e86\u89e3\u5b58\u5728\u591a\u5c11\u4fe1\u53f7\u3001\u6bcf\u4e2a\u4fe1\u53f7\u6709\u591a\u5c11\u8ba2\u9605\u8005\uff0c\u4ece\u800c\u76d1\u63a7\u7a0b\u5e8f\u7684\u8fd0\u884c\u3002\u4f46\u662f\u590d\u6742\u7684\u7cfb\u7edf\u5982\u679c\u8981\u7528\u89c2\u5bdf\u8005\u6a21\u5f0f\u6765\u505a\u903b\u8f91\uff0c\u5fc5\u987b\u8981\u505a\u597d\u4e8b\u4ef6\u8ba2\u9605\u548c\u53d1\u5e03\u7684\u8bbe\u8ba1\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u7684\u8fd0\u884c\u6d41\u7a0b\u6df7\u4e71\u3002\n\n#### Promise/A+ (es6)\n\nPromise\u672c\u610f\u662f\u627f\u8bfa\uff0c\u5728\u7a0b\u5e8f\u4e2d\u7684\u610f\u601d\u5c31\u662f\u627f\u8bfa\u6211\u8fc7\u4e00\u6bb5\u65f6\u95f4\u540e\u4f1a\u7ed9\u4f60\u4e00\u4e2a\u7ed3\u679c\u3002 \u4ec0\u4e48\u65f6\u5019\u4f1a\u7528\u5230\u8fc7\u4e00\u6bb5\u65f6\u95f4\uff1f\u7b54\u6848\u662f\u5f02\u6b65\u64cd\u4f5c\uff0c\u5f02\u6b65\u662f\u6307\u53ef\u80fd\u6bd4\u8f83\u957f\u65f6\u95f4\u624d\u6709\u7ed3\u679c\u7684\u624d\u505a\uff0c\u4f8b\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u8bfb\u53d6\u672c\u5730\u6587\u4ef6\u7b49\u3002\n\n- promise\u6709\u4e09\u79cd\u72b6\u6001\uff1apending\uff08\u7b49\u5f85\uff09\u3001fulfilled\uff08\u6210\u529f\uff09\u3001rejected\uff08\u5931\u8d25\uff09\u3002\u5176\u4e2dpending\u4e3a\u521d\u59cb\u72b6\u6001\u3002\n- promise\u7684\u72b6\u6001\u8f6c\u6362\u53ea\u80fd\u662f\uff1apending->fulfilled\u6216\u8005pending->rejected\u3002\u8f6c\u6362\u65b9\u5411\u4e0d\u53ef\u9006\uff0c\u4e0d\u53ef\u66f4\u6539\u3002\n- promise\u62e5\u6709then\u65b9\u6cd5\u3002then\u65b9\u6cd5\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2apromise\u3002then\u652f\u6301\u94fe\u5f0f\u8c03\u7528\uff0c\u4e14\u56de\u8c03\u7684\u987a\u5e8f\u8ddfthen\u7684\u58f0\u660e\u987a\u5e8f\u4e00\u81f4\u3002\n\n```js\nconst promise = new Promise((resolve, reject) => {\n resolve(\"step1\");\n})\n.then((data) => {\n console.log(\"\u83b7\u53d6\u5230\u6570\u636e\uff1a\", data);\n return \"step2\";\n})\n.then((data) => {\n console.log(\"\u83b7\u53d6\u5230\u6570\u636e\uff1a\", data);\n});\n```\n\n#### \u751f\u6210\u5668Generators/ yield (es6)\n\nES6 \u65b0\u5f15\u5165\u4e86 Generator \u51fd\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7 yield \u5173\u952e\u5b57\uff0c\u628a\u51fd\u6570\u7684\u6267\u884c\u6d41\u6302\u8d77\uff0c\u901a\u8fc7next()\u65b9\u6cd5\u53ef\u4ee5\u5207\u6362\u5230\u4e0b\u4e00\u4e2a\u72b6\u6001\uff0c\u4e3a\u6539\u53d8\u6267\u884c\u6d41\u7a0b\u63d0\u4f9b\u4e86\u53ef\u80fd\uff0c\u4ece\u800c\u4e3a\u5f02\u6b65\u7f16\u7a0b\u63d0\u4f9b\u89e3\u51b3\u65b9\u6848\u3002\n\n```js\nfunction* myGenerator() {\n yield '1'\n yield '2'\n return '3'\n}\n\nconst item = myGenerator(); \nitem.next() //{value: \"1\", done: false}\nitem.next() //{value: \"2\", done: false}\nitem.next() //{value: \"3\", done: true}\n\n```\n\n- Generator \u9700\u8981\u624b\u52a8\u8c03\u7528next()\u5c31\u80fd\u81ea\u52a8\u6267\u884c\u4e0b\u4e00\u6b65\n- Generator \u8fd4\u56de\u7684\u662f\u751f\u6210\u5668\u5bf9\u8c61\n- Generator \u4e0d\u80fd\u591f\u8fd4\u56dePromise\u7684resolve/reject\u7684\u503c\n\n```js\nfunction simpleGenerator(ctx) {\n while (1) {\n switch (ctx.prev = ctx.next) {\n case 0:\n ctx.next = 2;\n return 'step1';\n\n case 2:\n ctx.next = 4;\n return 'step2';\n\n case 4:\n ctx.next = 6;\n return 'step3';\n\n case 6:\n case \"end\":\n return ctx.stop();\n }\n }\n}\n\nlet ctx = {\n next:0,\n prev: 0,\n done: false,\n stop: function stop () {\n this.done = true\n }\n}\n\n\nlet simpleGeneratorLower = function() {\n return {\n next: function() {\n value = ctx.done ? undefined: simpleGenerator(ctx)\n done = ctx.done\n return {\n value,\n done\n }\n }\n }\n}\n\n```\n\nGenerator\u5b9e\u73b0\u7684\u6838\u5fc3\u5728\u4e8e\u4e0a\u4e0b\u6587\u7684\u4fdd\u5b58\uff0c\u6bcf\u4e00\u6b21yield\uff0c\u6267\u884c\u4e00\u904d\u4f20\u5165\u7684\u751f\u6210\u5668\u51fd\u6570\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u95f4\u7528\u4e86\u4e00\u4e2actx\u5bf9\u8c61\u50a8\u5b58\u4e0a\u4e0b\u6587\uff0c\u4f7f\u5f97\u6bcf\u6b21\u6267\u884c\u751f\u6210\u5668\u51fd\u6570\u7684\u65f6\u5019\uff0c\u90fd\u53ef\u4ee5\u4ece\u524d\u4e00\u6b21\u6267\u884c\u7684\u7ed3\u679c\u5f00\u59cb\u6267\u884c\u3002\n\n#### async/await (es7)\n\n\u4f7f\u7528async/await\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8fbe\u6210\u4e4b\u524d\u4f7f\u7528\u751f\u6210\u5668\u548cco\u51fd\u6570\u6240\u505a\u5230\u7684\u5de5\u4f5c,\u5b83\u6709\u5982\u4e0b\u7279\u70b9\uff1a\n\n- async/await\u662f\u57fa\u4e8ePromise\u5b9e\u73b0\u7684\uff0c\u5b83\u4e0d\u80fd\u7528\u4e8e\u666e\u901a\u7684\u56de\u8c03\u51fd\u6570\u3002\n- async/await\u4e0ePromise\u4e00\u6837\uff0c\u662f\u975e\u963b\u585e\u7684\u3002\n- async/await\u4f7f\u5f97\u5f02\u6b65\u4ee3\u7801\u770b\u8d77\u6765\u50cf\u540c\u6b65\u4ee3\u7801\uff0c\u8fd9\u6b63\u662f\u5b83\u7684\u9b54\u529b\u6240\u5728\u3002\n\n```js\nlet fs = require('fs')\nfunction read(file) {\n return new Promise(function(resolve, reject) {\n fs.readFile(file, 'utf8', function(err, data) {\n if (err) reject(err)\n resolve(data)\n })\n })\n}\nfunction readAll() {\n read1()\n read2()//\u8fd9\u4e2a\u51fd\u6570\u540c\u6b65\u6267\u884c\n}\nasync function read1() {\n let r = await read('1.txt','utf8')\n console.log(r)\n}\nasync function read2() {\n let r = await read('2.txt','utf8')\n console.log(r)\n}\nreadAll() // 2.txt 3.txt\n```", "answer": "", "biz_type": 1, "qid": 624, "subject": "", "title": "\u6982\u8ff0\u5f02\u6b65\u7f16\u7a0b\u6a21\u578b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 9, "qid": 569, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n\u56e0\u4e3a JavaScript \u662f\u5355\u7ebf\u7a0b\u6267\u884c\uff0c\u6240\u4ee5\u6267\u884c\u4e0a\u9762\u4ee3\u7801\u4f1a\u5bfc\u81f4\u6b7b\u5faa\u73af\n\n#### 1.Concurrent.Thread.js \u5e93\n\n\u4f7f\u7528\u5f02\u6b65\u6a21\u62df\u591a\u7ebf\u7a0b\n\n```js\nConcurrent.Thread.create(function() {\n while(1){\n console.log(Math.random());\n }\n})\n```\n\n#### 2.Web Worker\n\n\u63d0\u4f9b\u4e86\u5728\u540e\u53f0\u975e\u4e3b\u7ebf\u7a0b\u6267\u884c JavaScript \u4ee3\u7801\u7684\u80fd\u529b\n\n```js\n// main.js\nconst worker = new Worker('worker.js');\n\nworker.onmessage = function(e) {\n // \u63a5\u6536worker\u4f20\u8fc7\u6765\u7684\u6570\u636e\n}\n\n// worker.js\nwhile(1){\n const n = Math.random();\n console.log(n);\n if (n > 0.9) {\n postMessage(n);\n break;\n }\n}\n```", "answer": "", "biz_type": 1, "qid": 569, "subject": "```js\nwhile (1) {\n console.log(Math.random());\n}\n```\n", "title": "\u4fee\u6539\u4ee3\u7801\u4e0d\u9020\u6210\u6b7b\u5faa\u73af"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f1a\u5c0f\u4e8c"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 8, "qid": 611, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n```js\n<style>\n div,span,ul,li{margin: 0;padding: 0;}\n ul,li{list-style: none;}\n #box{width: 200px;height: 30px;}\n span{width: 200px;height: 30px;border: 1px solid black;display: block;line-height: 30px;text-align: center;}\n .list{width: 200px;height: 90px;display: none;}\n .list li{width: 200px;height: 30px;border: 1px solid black;border-top:none;text-align: center;}\n .list .active{background: #66f;color: #fff;}\n</style>\n<body>\n <div id=\"box\">\n <span>\u4e0a\u6d77</span>\n <ul class=\"list\">\n <li class=\"active\">\u4e0a\u6d77</li>\n <li>\u5317\u4eac</li>\n <li>\u5e7f\u5dde</li>\n <li>\u9752\u5c9b</li>\n <li>\u676d\u5dde</li>\n </ul>\n </div>\n</body>\n\n<script>\n var olist = document.querySelector(\".list\")\n var ospan = document.querySelector(\"span\");\n var ali = document.querySelectorAll(\".list li\");\n // 1\u4e3a\u663e\u793a\uff0c2\u4e3a\u9690\u85cf\n var type = 1;\n // \u9ed8\u8ba4\u7d22\u5f15\u6837\u5f0f\n var index = 0;\n clearActive();\n\n ospan.onclick = function(eve){\n var e = eve || window.event;\n if(type === 1){\n olist.style.display = \"block\";\n clearActive();\n type = 2;\n }else{\n olist.style.display = \"none\";\n clearActive();\n type = 1;\n }\n stopBubble(e);\n }\n for (var i = 0; i < ali.length; i++) {\n ali[i].xuhao = i\n ali[i].onclick = function (eve) {\n var e = eve || window.enent;\n ospan.innerHTML = this.innerHTML\n index = this.xuhao\n }\n // \u9f20\u6807\u6ed1\u8fc7\u7684\u6837\u5f0f\n ali[i].onmousemove = function (eve) {\n var e = eve || window.enent;\n index = this.xuhao\n clearActive();\n }\n ali[i].onmouseout = function (eve) {\n var e = eve || window.enent;\n this.className = \"\";\n }\n }\n //\u70b9\u51fb\u7a7a\u767d\n document.onclick = function(){\n olist.style.display = \"none\";\n type = 1;\n }\n\n\n //\u8bbe\u7f6e\u9ed8\u8ba4\u6837\u5f0f\n function clearActive(){\n for(var i=0;i<ali.length;i++) {\n ali[i].className = \"\";\n }\n ali[index].className = \"active\";\n }\n\n function stopBubble(e){\n if(e.stopPropagation){\n e.stopPropagation();\n }else{\n e.cancelBubble = true;\n }\n }\n</script>\n```", "answer": "", "biz_type": 1, "qid": 611, "subject": "", "title": "\u7528 html\u3001css\u3001js \u6a21\u62df\u5b9e\u73b0\u4e00\u4e2a\u4e0b\u62c9\u6846\uff0c\u4f7f\u5f97\u4e0b\u62c9\u6846\u5728\u5404\u4e2a\u6d4f\u89c8\u5668\u4e0b\u7684\u6837\u5f0f\u548c\u884c\u4e3a\u5b8c\u5168\u4e00\u81f4\uff0c\u8bf4\u51fa\u4f60\u7684\u8bbe\u8ba1\u65b9\u6848\uff0c\u5e76\u4e14\u91cd\u70b9\u8bf4\u660e\u529f\u80fd\u8bbe\u8ba1\u65f6\u8981\u8003\u8651\u7684\u56e0\u7d20\u3002"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5b9d\u5b9d\u6811"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 9, "qid": 519, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\nPromise \u662f ES6 \u4e2d\u5f02\u6b65\u7f16\u7a0b\u7684\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\uff0c\u6bd4\u4f20\u7edf\u7684\u89e3\u51b3\u65b9\u6848\u2014\u2014\u56de\u8c03\u51fd\u6570\u548c\u4e8b\u4ef6\u2014\u2014\u66f4\u5408\u7406\u548c\u66f4\u5f3a\u5927\u3002Promise \u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u63d0\u4f9b\u7edf\u4e00\u7684 API\uff0c\u5404\u79cd\u5f02\u6b65\u64cd\u4f5c\u90fd\u53ef\u4ee5\u7528\u540c\u6837\u7684\u65b9\u6cd5\u8fdb\u884c\u5904\u7406\u3002\n\n\u53e6\u5916Promise \u662f\u6709\u7edf\u4e00\u89c4\u8303\u7684\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u8ba9\u5927\u5bb6\u81ea\u5df1\u5b9e\u73b0\u7684 promise \u66f4\u597d\u7684\u5b9e\u73b0\u517c\u5bb9\uff0c\u6240\u4ee5\u51fa\u73b0\u4e86 Promises/A+\u89c4\u8303\uff1b\n\n\u4ee3\u7801\u5b9e\u73b0\n\n```js\nclass Promise {\n constructor(executor) {\n // \u5f62\u53c2\u6821\u9a8c\n if (typeof executor !== 'function') throw new TypeError(`Promise resolver ${executor} is not a function`)\n // \u521d\u59cb\u5316\n this.init();\n\n try {\n executor(this.resolve, this.reject);\n } catch (e) {\n this.reject(e)\n }\n\n }\n\n // \u521d\u59cb\u5316\u503c\n init() {\n this.value = null; // \u7ec8\u503c\n this.reason = null; // \u62d2\u56e0\n this.status = Promise.PENDING; // \u72b6\u6001\n this.onFulfilledCbs = []; // \u6210\u529f\u56de\u8c03\n this.onRejectedCbs = []; // \u5931\u8d25\u56de\u8c03\n // \u5c06 resolve \u548c reject \u4e2d\u7684 this \u7ed1\u5b9a\u5230 Promise \u5b9e\u4f8b\n this.resolve = this.resolve.bind(this)\n this.reject = this.reject.bind(this)\n }\n\n resolve(val) {\n // \u6210\u529f\u540e\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\uff08\u72b6\u6001\u6539\u53d8\uff0c\u6210\u529f\u56de\u8c03\u7684\u6267\u884c\uff09\n if (this.status === Promise.PENDING) {\n this.status = Promise.FULFILLED;\n this.value = val;\n this.onFulfilledCbs.forEach(fn => fn(this.value));\n }\n }\n\n reject(err) {\n // \u5931\u8d25\u540e\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\uff08\u72b6\u6001\u6539\u53d8\uff0c \u5931\u8d25\u56de\u8c03\u7684\u6267\u884c\uff09\n if (this.status === Promise.PENDING) {\n this.status = Promise.REJECTED;\n this.reason = err;\n this.onRejectedCbs.forEach(fn => fn(this.reason));\n }\n }\n\n/* \n then \u63a5\u6536\u4e24\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\n onFulfilled\u662f\u5f53 promise \u5b9e\u4f8b\u72b6\u6001\u53d8\u4e3a fulfilled \u65f6\u8c03\u7528\u7684\u51fd\u6570\uff0c\n onRejected \u662f\u5b9e\u4f8b\u72b6\u6001\u53d8\u4e3a rejected \u65f6\u8c03\u7528\u7684\u51fd\u6570\uff0c\n \u5e76\u4e14 then \u8fd4\u56de\u7684\u662f\u4e00\u4e2a \u65b0\u7684 promise \u5b9e\u4f8b\n*/\n then(onFulfilled, onRejected) {\n // \u53c2\u6570\u6821\u9a8c onFulfilled, onRejected \u4e3a\u53ef\u9009\u5f62\u53c2\n onFulfilled = typeof onFulfilled !== 'function' ? val => val : onFulfilled;\n onRejected = typeof onRejected !== 'function' ? reason => { throw reason } : onRejected;\n\n // \u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\u6765\u5b9e\u73b0\u94fe\u5f0f\u8c03\u7528\n let promise2 = new Promise((resolve, reject) => {\n // \u540c\u6b65\u64cd\u4f5c\uff08\u6700\u5f00\u59cb\u7684\u72b6\u6001\u6539\u53d8\u4e3a\u540c\u6b65\uff09\n if (this.status === Promise.FULFILLED) {\n // setTimeout \u6a21\u62df\u5fae\u4efb\u52a1\u5f02\u6b65 \n // \u53ea\u6709\u5f02\u6b65\u540e \u624d\u80fd\u5728\u91cc\u9762\u53d6\u5230 new \u597d\u7684 promise2\n setTimeout(() => {\n try { // \u91cd\u65b0\u6dfb\u52a0try-catch\uff0csettimeout \u5185\u4e3a\u5f02\u6b65\uff0c\u65e0\u6cd5\u88ab\u5916\u5c42constructor\u4e2d\u7684try-catch\u6355\u83b7\n // \u4ee5\u7ec8\u503c\u4f5c\u4e3a\u53c2\u6570\u6267\u884c onFulfilled \u51fd\u6570\n let x = onFulfilled(this.value);\n // \u5206\u6790\u6267\u884c\u7ed3\u679c x \u4e0e promise \u7684\u5173\u7cfb\n // resolve(x);\n Promise.resolvePromise(promise2, x, resolve, reject);\n } catch (e) { reject(e) }\n })\n }\n\n if (this.status === Promise.REJECTED) {\n // \u4ee5\u62d2\u56e0\u4f5c\u4e3a\u53c2\u6570\u6267\u884c onRejected \u51fd\u6570\n setTimeout(() => {\n try {\n let x = onRejected(this.reason)\n Promise.resolvePromise(promise2, x, resolve, reject);\n } catch (e) { reject(e) }\n });\n }\n\n // \u5f02\u6b65\u64cd\u4f5c\uff08\u6700\u5f00\u59cb\u72b6\u6001\u6539\u53d8\u4e3a\u5f02\u6b65\uff0c\u5982 settimeout \u5185\u5305\u542b resolve\uff09\u4f7f\u7528\u53d1\u5e03\u8ba2\u9605\n if (this.status === Promise.PENDING) {\n this.onFulfilledCbs.push(value => {\n setTimeout(() => {\n try {\n let x = onFulfilled(value)\n Promise.resolvePromise(promise2, x, resolve, reject);\n } catch (e) { reject(e) }\n })\n });\n this.onRejectedCbs.push((reason) => {\n setTimeout(() => {\n try {\n let x = onRejected(reason)\n Promise.resolvePromise(promise2, x, resolve, reject);\n } catch (e) { reject(e) }\n })\n });\n }\n })\n\n return promise2\n }\n\n}\n\nPromise.PENDING = 'pending';\nPromise.FULFILLED = 'fulfilled';\nPromise.REJECTED = 'rejected';\n\n/* \n\u6839\u636e\u89c4\u8303\uff0cresolvePromise \u89c4\u5219\u5927\u81f4\u5982\u4e0b\uff1a\n1.promise2 \u548c x \u6307\u5411\u540c\u4e00\u5bf9\u8c61\uff0c\u629b\u51fa\u4e00\u4e2a\u7c7b\u578b\u9519\u8bef\n2.x \u662f\u4e00\u4e2a Promise \u7c7b\uff0c\u5219\u63a5\u53d7\u5b83\u7684\u72b6\u6001\uff0c\u5e76\u5bf9\u5e94\u6267\u884c\u5176resolve \u6216\u8005 reject \u51fd\u6570\n3.x \u662f\u4e00\u4e2a\u51fd\u6570\u6216\u8005\u5bf9\u8c61\n4.\u4ee5\u4e0a\u90fd\u4e0d\u662f\uff0c\u8c03\u7528 resolve\n*/\nPromise.resolvePromise = function (promise2, x, resolve, reject) {\n // x \u4e0e promise2 \u76f8\u7b49 -> \u62a5\u9519\n if (promise2 == x) {\n return reject(new TypeError('Chaining cycle detected for promise'))\n }\n let called; // \u9632\u6b62\u591a\u6b21\u8c03\u7528 \u6210\u529f \u548c \u5931\u8d25\n // // x \u662f\u5426\u662f promise \u7c7b\n // if (x instanceof Promise) {\n // x.then(value => {\n // Promise.resolvePromise(promise2, value, resolve, reject);\n // }, err => {\n // reject(err)\n // })\n // // x \u662f\u51fd\u6570\u6216\u8005\u5bf9\u8c61\n // } else \n if (x !== null && typeof x == 'object' || typeof x == 'function') {\n try { // \u53d6 x.then \u53ef\u80fd\u62a5\u9519\n // \u5982\u679c x \u6709 then \u65b9\u6cd5\n let then = x.then;\n // \u5982\u679c then \u662f\u4e00\u4e2a\u51fd\u6570\n if (typeof then == 'function') {\n // \u7528 call \u8c03\u7528 then \u65b9\u6cd5\u6307\u5411 x\uff0c\u9632\u6b62\u518d\u6b21\u53d6 x.then \u62a5\u9519\n then.call(x, value => {\n if (called) return;\n called = true;\n Promise.resolvePromise(promise2, value, resolve, reject);\n }, err => {\n if (called) return;\n called = true;\n reject(err)\n })\n } else {\n if (called) return;\n called = true;\n resolve(x);\n }\n } catch (e) {\n if (called) return\n called = true\n reject(e)\n }\n } else {\n // x \u4e3a\u57fa\u672c\u7c7b\u578b\u503c\n resolve(x);\n }\n}\n\n```", "answer": "", "biz_type": 1, "qid": 519, "subject": "", "title": "promise \u5982\u4f55\u5b9e\u73b0 then \u5904\u7406\uff0c\u52a8\u624b\u5b9e\u73b0 then"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 8, "qid": 585, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n- \u5b9e\u73b0\u4e00\n\n```js\nfunction stringify(jsonObj) {\n var result = '',\n curVal;\n if (jsonObj === null) {\n return String(jsonObj);\n }\n switch (typeof jsonObj) {\n case 'number':\n case 'boolean':\n return String(jsonObj);\n case 'string':\n return '\"' + jsonObj + '\"';\n case 'undefined':\n case 'function':\n return undefined;\n }\n\n switch (Object.prototype.toString.call(jsonObj)) {\n case '[object Array]':\n result += '[';\n for (var i = 0, len = jsonObj.length; i < len; i++) {\n curVal = JSON.stringify(jsonObj[i]);\n result += (curVal === undefined ? null : curVal) + \",\";\n }\n if (result !== '[') {\n result = result.slice(0, -1);\n }\n result += ']';\n return result;\n case '[object Date]':\n return '\"' + (jsonObj.toJSON ? jsonObj.toJSON() : jsonObj.toString()) + '\"';\n case '[object RegExp]':\n return \"{}\";\n case '[object Object]':\n result += '{';\n for (i in jsonObj) {\n if (jsonObj.hasOwnProperty(i)) {\n curVal = JSON.stringify(jsonObj[i]);\n if (curVal !== undefined) {\n result += '\"' + i + '\":' + curVal + ',';\n }\n }\n }\n if (result !== '{') {\n result = result.slice(0, -1);\n }\n result += '}';\n return result;\n\n case '[object String]':\n return '\"' + jsonObj.toString() + '\"';\n case '[object Number]':\n case '[object Boolean]':\n return jsonObj.toString();\n }\n }\n```\n\n- \u5b9e\u73b0\u4e8c\n\n```javascript\n(function (NS) {\n\tvar simpleTypes = [\"number\", \"boolean\", \"undefined\", \"string\", \"function\"];\n\n\tfunction stringify(object) {\n\t\tvar type = typeof object;\n\t\tif (indexOf(simpleTypes, type) > -1) {\n\t\t\treturn parseSimpleObject(object);\n\t\t}\n\n\t\tif (object instanceof Array) {\n\t\t\tvar len = object.length;\n\t\t\tvar resArr = [];\n\t\t\tfor (var i = 0; i < len; i++) {\n\t\t\t\tvar itemType = typeof object[i];\n\t\t\t\tif (indexOf(simpleTypes, itemType) > -1) {\n\t\t\t\t\tif (itemType !== \"undefined\") {\n\t\t\t\t\t\tresArr.push(parseSimpleObject(object[i]));\n\t\t\t\t\t} else {\n\t\t\t\t\t\tresArr.push(\"null\");\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tresArr.push(stringify(object[i]));\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn \"[\" + resArr.join(\",\") + \"]\";\n\t\t}\n\n\t\tif (object instanceof Object) {\n\t\t\tif (object == null) {\n\t\t\t\treturn \"null\";\n\t\t\t}\n\t\t\tvar resArr = [];\n\t\t\tfor (var name in object) {\n\t\t\t\tvar itemType = typeof object[name];\n\t\t\t\tif (indexOf(simpleTypes, itemType) > -1) {\n\t\t\t\t\tif (itemType !== \"undefined\") {\n\t\t\t\t\t\tresArr.push('\"' + name + '\":' + parseSimpleObject(object[name]));\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tresArr.push('\"' + name + '\":' + stringify(object[name]));\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn \"{\" + resArr.join(\",\") + \"}\";\n\t\t}\n\t}\n\n\tfunction parseSimpleObject(object) {\n\t\tvar type = typeof object;\n\t\tif (type === \"string\" || type === \"function\") {\n\t\t\treturn '\"' + object.toString().replace('\"', '\\\\\"') + '\"';\n\t\t}\n\t\tif (type === \"number\" || type === \"boolean\") {\n\t\t\treturn object.toString();\n\t\t}\n\t\tif (type == \"undefined\") {\n\t\t\treturn \"undefined\";\n\t\t}\n\t\treturn '\"' + object.toString().replace('\"', '\\\\\"') + '\"';\n\t}\n\n\tfunction indexOf(arr, val) {\n\t\tfor (var i = 0; i < arr.length; i++) {\n\t\t\tif (arr[i] === val) {\n\t\t\t\treturn i;\n\t\t\t}\n\t\t}\n\t\treturn -1;\n\t}\n\n\tNS.stringify = function (object,isEncodeZh) {\n\t\tvar res = stringify(object);\n\t\tif (isEncodeZh) {\n\t\t\tvar encodeRes = \"\";\n\t\t\tfor (var i = 0; i < res.length; i++) {\n\t\t\t\tif (res.charCodeAt(i) < Oxff) {\n\t\t\t\t\tencodeRes += res[i];\n\t\t\t\t} else {\n\t\t\t\t\tencodeRes += \"\\\\u\" + res.charCodeAt(i).toString(16);\n\t\t\t\t}\n\t\t\t}\n\t\t\tres = encodeRes;\n\t\t}\n\t\treturn res;\n\t};\n})(window);\n```\n\n", "answer": "", "biz_type": 1, "qid": 585, "subject": "", "title": "\u8bf7\u5b9e\u73b0\u4e00\u4e2a JSON.stringfy"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761", "\u4e9a\u7f8e\u79d1\u6280"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 11, "qid": 677, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n- `on(event, fn)` \u76d1\u542c\u81ea\u5b9a\u4e49 event \u4e8b\u4ef6\uff0c\u4e8b\u4ef6\u89e6\u53d1\u65f6\u8c03\u7528 fn \u51fd\u6570\uff08addListener\uff09\n- `once(event, fn)` \u4e3a\u6307\u5b9a\u4e8b\u4ef6\u6ce8\u518c\u4e00\u4e2a\u5355\u6b21\u76d1\u542c\u5668\uff0c\u5355\u8bcd\u76d1\u542c\u6700\u591a\u53ea\u89e6\u53d1\u4e00\u6b21\uff0c\u51fa\u53d1\u540e\u7acb\u5373\u89e3\u9664\u76d1\u542c\u5668\n- `emit(event, arg1, arg2, arg3...)` \u89e6\u53d1 event \u4e8b\u4ef6\uff0c\u5e76\u628a\u53c2\u6570\u4f20\u7ed9\u4e8b\u4ef6\u5904\u7406\u51fd\u6570 \uff08trigger\uff09\n- `off(event, fn)` \u505c\u6b62\u76d1\u542c\u67d0\u4e2a\u4e8b\u4ef6 \uff08removeListener\uff09\n\n```js\nfunction checkListener(listener) {\n\tif (typeof listener !== 'function') {\n\t\tthrow 'listener \u4e0d\u662f\u51fd\u6570'\n\t}\n}\n\nfunction EventEmitter() {\n\t/**\n\t * \u7f13\u5b58\u5217\u8868\uff0c\u5b58\u653e\u8ba2\u9605\u8005\u56de\u8c03\u51fd\u6570\n\t */\n\tthis._events = {};\n}\n\n/**\n * \u8ba2\u9605\u4e8b\u4ef6\n * @param {string} event - \u4e8b\u4ef6\u7c7b\u578b\n */\nEventEmitter.prototype.addListener = function addListener(event, listener) {\n\tcheckListener(listener);\n\n\tif (!this._events[event]) {\n\t\tthis._events[event] = [];\n\t}\n\tthis._events[event].push(listener);\n}\nEventEmitter.prototype.on = EventEmitter.prototype.addListener;\n\nEventEmitter.prototype.emit = function emit(event, ...args) {\n\tlet listeners = this._events[event];\n\n\tif (!listeners || !listeners.length) {\n\t\treturn false;\n\t}\n\n\tfor (let i = 0, listener; listener = listeners[i++];) {\n\t\tlistener.apply(this, [event, ...args]);\n\t}\n}\n\nEventEmitter.prototype.removeListener = function removeListener(event, listener) {\n\tlet listeners = this._events[event];\n\n\tif (!listeners) {\n\t\treturn false;\n\t}\n\tif (!listener) {\n\t\tlisteners && (listeners.length = 0);\n\t} else {\n\t\tfor (let i = listeners.length - 1; i >= 0; i--) {\n\t\t\tlet _listener = listeners[i];\n\t\t\tif (_listener === listener) {\n\t\t\t\tlisteners.splice(i, 1);\n\t\t\t}\n\t\t}\n\t}\n}\nEventEmitter.prototype.off = EventEmitter.prototype.removeListener;\n\nEventEmitter.prototype.once = function once(event, listener) {\n\tcheckListener(listener);\n\n\tlet wrap = (...args) => {\n\t\tlistener.apply(this, args);\n\t\tthis.off(event, wrap)\n\t}\n\tthis.on(event, wrap);\n}\n\nvar event = new EventEmitter();\nvar fn1 = function (type, price) {\n\tconsole.log(`fn1 \u8ba2\u9605 ${type} price: ${price}`);\n}\nvar fn2 = function (type, price) {\n\tconsole.log(`fn2 \u8ba2\u9605 ${type} price: ${price}`);\n};\nevent.addListener('sm88', fn1);\nevent.addListener('sm88', fn2);\nevent.addListener('sm100', fn2);\n\nevent.emit('sm88', 100); // 1. fn1-sm88-100, 2. fn2-sm88-100\nevent.emit('sm100', 1000); // 3. fn2-sm100-1000\n\nevent.removeListener('sm88', fn2);\nevent.emit('sm88', 100); // 4. fn1-sm88-100,\n\nevent.once('sm300', fn2);\nevent.emit('sm300', 1000); // 5. fn2-sm200-1000,\nevent.emit('sm300', 1000); //\n```", "answer": "", "biz_type": 1, "qid": 677, "subject": "", "title": "\u624b\u5199 EventEmitter \u5b9e\u73b0"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 9, "qid": 656, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n#### 1.\u5b9e\u73b0\u65b9\u5f0f\u4e00\n\n```js\nvar arr = [1,2,3,4,5]\n\nfunction disorder(arr) {\n return Array.from(arr).sort(()=>Math.random()-0.5)\n}\nvar arr2 = disorder(arr)\nconsole.log(arr2)\nconsole.log(arr)\nconsole.log(arr === arr2)\n```\n\n#### 2.\u5b9e\u73b0\u65b9\u5f0f\u4e8c\n\n```js\nlet arr = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];\nfunction shuffle(arr) {\n let i = arr.length;\n while (i) {\n let j = Math.floor(Math.random() * i--);\n [arr[j], arr[i]] = [arr[i], arr[j]];\n }\n return arr;\n}\nshuffle(arr);\n```", "answer": "", "biz_type": 1, "qid": 656, "subject": "", "title": "\u5199\u4e00\u4e2a\u51fd\u6570\u6253\u4e71\u4e00\u4e2a\u6570\u7ec4\uff0c\u4f20\u5165\u4e00\u4e2a\u6570\u7ec4\uff0c\u8fd4\u56de\u4e00\u4e2a\u6253\u4e71\u7684\u65b0\u6570\u7ec4"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9177\u72d7", "\u6caa\u6c5f", "58"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 5, "qid": 682, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4e00\u3001\u8bbe\u8ba1\u6a21\u5f0f\n\n\u8bbe\u8ba1\u6a21\u5f0f\u662f\u4e00\u5957\u88ab\u53cd\u590d\u4f7f\u7528\u3001\u5206\u7c7b\u7684\u4ee3\u7801\u8bbe\u8ba1\u7ecf\u9a8c\u7684\u603b\u7ed3\u3002\u4e00\u822c\u670923\u4e2d\uff0c\u6309\u7167\u5206\u7c7b\u53ef\u4ee5\u5206\u4e3a\uff1a\n\n#### 1.\u521b\u9020\u578b\u6a21\u5f0f\n\n- \u62bd\u8c61\u5de5\u5382\u6a21\u5f0f\n- \u5de5\u5382\u65b9\u6cd5\u6a21\u5f0f\n- \u5355\u4f8b\u6a21\u5f0f\n- \u6784\u5efa\u6a21\u5f0f\n- \u539f\u578b\u6a21\u5f0f\n\n#### 2.\u7ed3\u6784\u578b\u6a21\u5f0f\n\n- \u4ee3\u7406\u6a21\u5f0f\n- \u88c5\u9970\u8005\u6a21\u5f0f\n- \u7ec4\u5408\u6a21\u5f0f\n- \u6865\u63a5\u6a21\u5f0f\n- \u9002\u914d\u5668\u6a21\u5f0f\n- \u5916\u89c2\u6a21\u5f0f\n- \u4eab\u5143\u6a21\u5f0f\n\n#### 3.\u884c\u4e3a\u578b\u6a21\u5f0f\n\n- \u7b56\u7565\u6a21\u5f0f\n- \u547d\u4ee4\u6a21\u5f0f\n- \u72b6\u6001\u6a21\u5f0f\n- \u8d23\u4efb\u94fe\u6a21\u5f0f\n- \u89e3\u91ca\u5668\u6a21\u5f0f\n- \u89c2\u5bdf\u8005\u6a21\u5f0f\n- \u5907\u5fd8\u5f55\u6a21\u5f0f\n- \u8fed\u4ee3\u5668\u6a21\u5f0f\n- \u6a21\u677f\u65b9\u6cd5\u6a21\u5f0f\n- \u8bbf\u95ee\u8005\u6a21\u5f0f\n- \u4e2d\u4ecb\u6a21\u5f0f\n\n### \u4e8c\u3001\u5e73\u5e38\u4f7f\u7528\u7684\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\n\n**\u5355\u4f8b\u6a21\u5f0f**\n\n\u5355\u4f8b\u6a21\u5f0f\u5c31\u662f\u4fdd\u8bc1\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u3002\n\n```js\nclass Car{\n constructor(){\n this.isSale = false;\n }\n \n sale(){\n if(this.isSale){\n console.log(\"\u5356\u4e86\");\n return;\n }\n this.isSale = true;\n console.log('\u5356\u8f66')\n }\n noSale(){\n if(!this.isSale){\n console.log(\"\u6ca1\u5356\");\n return;\n }\n this.isSale = false;\n console.log(\"\u6ca1\u5356\u5462\")\n }\n}\n\nCar.getInstance = (function(){\n let instance;\n return function(){\n if(!instance){\n instance = new Car();\n }\n return instance;\n }\n})();\n\nlet men1 = Car.getInstance();\nlet men2 = Car.getInstance();\nconsole.log(men1 === men2)\n\n```\n\n**\u5de5\u5382\u6a21\u5f0f**\n\n\u5de5\u5382\u6a21\u5f0f\u5b9a\u4e49\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u7684\u63a5\u53e3\uff0c\u8fd9\u4e2a\u63a5\u53e3\u7531\u5b50\u7c7b\u51b3\u5b9a\u5b9e\u4f8b\u5316\u54ea\u4e00\u4e2a\u7c7b\u3002\n\n```js\nvar factory = {};\nfactory.createCar = function(){\n console.log('create car');\n}\nfactory.createMask = function(){\n console.log('create mask');\n}\n\nfactory.manage = function(create){\n return new factory[create]();\n}\n\nlet car = factory.manage('createCar');\nlet mask = factory.manage('mask');\n```", "answer": "", "biz_type": 1, "qid": 682, "subject": "", "title": "\u5e73\u65f6\u90fd\u7528\u5230\u4e86\u54ea\u4e9b\u8bbe\u8ba1\u6a21\u5f0f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fae\u533b", "\u5bfa\u5e93"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 494, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4e00\u3001\u6808\u548c\u5806\u533a\u522b\n\n**\u6808:** \u7531\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u5206\u914d\u91ca\u653e \uff0c\u5b58\u653e\u51fd\u6570\u7684\u53c2\u6570\u503c\u548c\u5c40\u90e8\u53d8\u91cf\u7684\u503c\u7b49\u3002\u5176\u64cd\u4f5c\u65b9\u5f0f\u7c7b\u4f3c\u4e8e\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u6808\u3002\u7b80\u5355\u7684\u7406\u89e3\u5c31\u662f\u5f53\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u8ba1\u7b97\u673a\u4f1a\u5728\u5185\u5b58\u4e2d\u5f00\u8f9f\u4e00\u5757\u5b58\u50a8\u7a7a\u95f4\u6765\u5b58\u653e\u8fd9\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u8fd9\u5757\u7a7a\u95f4\u5c31\u53eb\u505a\u6808\uff0c\u7136\u800c\u6808\u4e2d\u4e00\u822c\u5b58\u653e\u7684\u662f\u57fa\u672c\u7c7b\u578b\u6570\u636e\uff0c\u6808\u7684\u7279\u70b9\u662f\u5148\u8fdb\u540e\u51fa\uff08\u6216\u540e\u8fdb\u5148\u51fa\uff09\n\n**\u5806:** \u4e00\u822c\u7531\u7a0b\u5e8f\u5458\u5206\u914d\u91ca\u653e\uff0c \u82e5\u7a0b\u5e8f\u5458\u4e0d\u91ca\u653e\uff0c\u7a0b\u5e8f\u7ed3\u675f\u65f6\u53ef\u80fd\u7531OS\u56de\u6536\uff0c\u5206\u914d\u65b9\u5f0f\u5012\u662f\u7c7b\u4f3c\u4e8e\u94fe\u8868\u3002\u5176\u5b9e\u5728\u5806\u4e2d\u4e00\u822c\u5b58\u653e\u53d8\u91cf\u662f\u4e00\u4e9b\u5bf9\u8c61\u7c7b\u578b\n\n#### 1.\u5b58\u50a8\u5927\u5c0f\n\n\u6808\u5185\u5b58\u7684\u5b58\u50a8\u5927\u5c0f\u662f **\u56fa\u5b9a** \u7684\uff0c\u7533\u8bf7\u65f6\u7531\u7cfb\u7edf\u81ea\u52a8\u5206\u914d\u5185\u5b58\u7a7a\u95f4\uff0c\u8fd0\u884c\u7684\u6548\u7387\u6bd4\u8f83\u5feb\uff0c\u4f46\u662f\u56e0\u4e3a\u5b58\u50a8\u7684\u5927\u5c0f\u56fa\u5b9a\uff0c\u6240\u4ee5\u5bb9\u6613\u5b58\u50a8\u7684\u5927\u5c0f\u8d85\u8fc7\u5b58\u50a8\u7684\u5927\u5c0f\uff0c\u5bfc\u81f4\u76ca\u6808\u3002\n\n\u5806\u5185\u5b58\u7684\u5b58\u50a8\u7684\u503c\u7684 **\u5927\u5c0f\u4e0d\u5b9a** \uff0c\u662f\u6709\u7a0b\u5e8f\u5458\u81ea\u5df1\u7533\u8bf7\u5e76\u6307\u660e\u5927\u5c0f\u3002\u56e0\u4e3a\u5806\u5185\u5b58\u662f new \u5206\u914d\u7684\u5185\u5b58\uff0c\u6240\u4ee5\u8fd0\u884c\u6548\u7387\u4f1a\u8f83\u4f4e\u3002\n\n#### 2.\u5b58\u50a8\u5bf9\u8c61\n\n\u6808\u5185\u5b58\u5b58\u50a8\u7684\u662f\u57fa\u7840\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u4e14\u662f\u6309\u503c\u8bbf\u95ee\uff0c\u56e0\u4e3a\u6808\u662f\u4e00\u5757\u8fde\u7eed\u7684\u5185\u5b58\u533a\u57df\uff0c\u4ee5**\u540e\u8fdb\u5148\u51fa**\u7684\u539f\u5219\u5b58\u50a8\u8c03\u7528\u7684\uff0c\u6240\u4ee5\u662f\u8fde\u7eed\u5b58\u50a8\u7684\u3002\n\n\u5806\u5185\u5b58\u662f\u5411\u9ad8\u5730\u5740\u6269\u5c55\u7684\u6570\u636e\u7ed3\u6784\uff0c\u662f\u4e0d\u8fde\u7eed\u7684\u5185\u5b58\u533a\u57df\uff0c\u7cfb\u7edf\u4e5f\u662f\u7528\u94fe\u8868\u6765\u5b58\u50a8\u7a7a\u95f2\u7684\u5185\u5b58\u5730\u5740\uff0c\u6240\u4ee5\u662f\u4e0d\u8fde\u7eed\u7684\u3002\u56e0\u4e3a\u662f\u8bb0\u5f55\u7684\u5185\u5b58\u5730\u5740\uff0c\u6240\u4ee5\u83b7\u53d6\u662f\u901a\u8fc7\u5f15\u7528\uff0c\u5b58\u50a8\u7684\u662f\u5bf9\u8c61\u5c45\u591a\u3002\n\n#### 3.\u56de\u6536\n\n\u6808\u7684\u56de\u6536\u662f\u7cfb\u7edf\u63a7\u5236\u5b9e\u73b0\u7684\u3002\n\n\u5806\u5185\u5b58\u7684\u56de\u6536\u662f\u4eba\u4e3a\u63a7\u5236\u7684\uff0c\u5f53\u7a0b\u5e8f\u7ed3\u675f\u540e\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u56de\u6536\u3002\n\n\n### \u4e09\u3001\u5783\u573e\u56de\u6536\u6808\u548c\u5806\u7684\u533a\u522b\n\n- \u6808\u5185\u5b58\u4e2d\u7684\u6570\u636e\u53ea\u8981\u8fd0\u884c\u7ed3\u675f\uff0c\u5219\u76f4\u63a5\u56de\u6536\u3002\n- \u5806\u5185\u5b58\u4e2d\u7684\u5bf9\u8c61\u56de\u6536\u6807\u51c6\u662f\u5426\u53ef\u8fbe\uff0c\u5728 V8 \u4e2d \u5bf9\u8c61\u5148\u5206\u914d\u5230\u65b0\u751f\u4ee3\u7684 From \u4e2d\uff0c\u5982\u679c\u4e0d\u53ef\u8fbe\u76f4\u63a5\u91ca\u653e\uff0c\u5982\u679c\u53ef\u8fbe\uff0c\u5c31\u590d\u5236\u5230 To \u4e2d\uff0c\u7136\u540e\u5c06 To \u548c From \u4e92\u6362\u3002\u5f53\u591a\u6b21\u590d\u5236\u540e\u4f9d\u7136\u6ca1\u6709\u56de\u6536\uff0c\u5219\u653e\u5165\u8001\u751f\u4ee3\u4e2d\uff0c\u8fdb\u884c\u6807\u8bb0\u56de\u6536\u3002\u4e4b\u540e\u5c06\u5185\u5b58\u788e\u7247\u8fdb\u884c\u6574\u5408\u653e\u5230\u4e00\u7aef\u3002", "answer": "", "biz_type": 1, "qid": 494, "subject": "", "title": "\u8bf4\u4e00\u4e0b\u6808\u548c\u5806\u7684\u533a\u522b\uff0c\u5783\u573e\u56de\u6536\u65f6\u6808\u548c\u5806\u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f1a\u5c0f\u4e8c", "\u5b89\u5c45\u5ba2"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 5, "qid": 616, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4e00\u3001\u57fa\u672c\u6570\u636e\u7c7b\u578b\n\n`undefined,null,number,boolean,string,symbol`\n\n### \u4e8c\u3001typeof\u8fdb\u884c\u5224\u65ad\n\n typeof\u64cd\u4f5c\u7b26\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u672a\u7ecf\u8ba1\u7b97\u7684\u64cd\u4f5c\u6570\u7684\u7c7b\u578b\u3002\n\n ```js\ntypeof undefined === 'undefined'\ntypeof undeclared === 'undefined'\ntypeof true === 'boolean'\ntypeof 22 === 'number'\ntypeof NaN === 'number'\ntypeof '22' === 'string'\ntypeof [] === 'object'\ntypeof {} === 'object'\ntypeof null === 'object'\ntypeof /regex/ === 'object'\ntypeof new Date() === 'object'\ntypeof new String() === 'object'\n...\ntypeof new Function() === 'function'\ntypeof function a(){} === 'function'\n```\n\n **\u9664\u4e86Function\u4e4b\u5916\u7684\u6240\u6709\u6784\u9020\u51fd\u6570\u7684\u7c7b\u578b\u90fd\u662f'object'\u3002**\n\n `undefined`\u662f\u503c\u7684\u4e00\u79cd\uff0c\u4e3a\u672a\u5b9a\u4e49\uff0c\u800c`undeclared`\u5219\u8868\u793a\u53d8\u91cf\u8fd8\u6ca1\u6709\u88ab\u58f0\u660e\u8fc7\u3002\u5728\u6211\u4eec\u8bd5\u56fe\u8bbf\u95ee\u4e00\u4e2a`undeclared` \u53d8\u91cf\u65f6\u4f1a\u8fd9\u6837\u62a5\u9519\uff1a`ReferenceError\uff1aa is not defined`\u3002\u901a\u8fc7typeof\u5bf9`undefined`\u548c`undeclared`\u53d8\u91cf\u90fd\u8fd4\u56de`undefined`\n\n **\u6ce8\u610f**\uff1a\u53d8\u91cf\u6ca1\u6709\u7c7b\u578b\uff0c\u53ea\u6709\u503c\u624d\u6709\u3002\u7c7b\u578b\u5b9a\u4e49\u4e86\u503c\u7684\u884c\u4e3a\u548c\u7279\u5f81\u3002\n\n\n\n### \u4e09\u3001\u77e5\u8bc6\u6269\u5c55\n\n#### 1.instanceof\n\n`instanceof`\u8fd0\u7b97\u6570\u662f\u7528\u4e8e\u68c0\u6d4b`contructor.prototype`\u5c5e\u6027\u662f\u5426\u51fa\u73b0\u5728\u67d0\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u7684\u539f\u578b\u94fe\u4e0a\u3002\n\n```js\nobject instanceof constructor\n\n22 instanceof Number => false\n'22' instanceof String => false\n[] instanceof Object => true\n{} instanceof Object => true\nundefined instanceof Object => false\nnull instanceof Object => false\nnull instanceof null => Uncaught TypeError: Right-hand side of 'instanceof' is not an object\n\nnew String('22') instanceof String => true\nnew Number(22) instanceof Number => true\n```\n\n **instanceOf\u7684\u4e3b\u8981\u5b9e\u73b0\u539f\u7406\u5c31\u662f\u53ea\u8981\u53f3\u8fb9\u53d8\u91cf\u7684prototype\u5728\u5de6\u8fb9\u53d8\u91cf\u7684\u539f\u578b\u94fe\u4e0a\u5373\u53ef**\u3002\u56e0\u6b64\uff0cinstanceof\u5728\u67e5\u627e\u8fc7\u7a0b\u4e2d\u4f1a\u904d\u5386\u5de6\u8fb9\u53d8\u91cf\u7684\u539f\u578b\u94fe\uff0c\u77e5\u9053\u627e\u5230\u53f3\u8fb9\u53d8\u91cf\u7684prototype\uff0c\u5982\u679c\u67e5\u627e\u5931\u8d25\u5c31\u4f1a\u8fd4\u56defalse\u3002\n\n #### 2.Object.prototype.toString.call()\n\n ```js\nObject.prototype.toString.call(22) => \"[object Number]\"\nObject.prototype.toString.call('22') => \"[object String]\"\nObject.prototype.toString.call({}) => \"[object Object]\"\nObject.prototype.toString.call([]) => \"[object Array]\"\nObject.prototype.toString.call(true) => \"[object Boolean]\"\nObject.prototype.toString.call(Math) => \"[object Math]\"\nObject.prototype.toString.call(new Date) => \"[object Date]\"\nObject.prototype.toString.call(Symbol(22))=> \"[object Symbol]\"\nObject.prototype.toString.call(() => {}) => \"[object Function]\"\nObject.prototype.toString.call(null) => \"[object Null]\"\nObject.prototype.toString.call(undefined) => \"[object Undefined]\"\n```\n\n### \u56db\u3001\u603b\u7ed3\n\n\n \u7528 typeof \u6765\u5224\u65ad\u53d8\u91cf\u7c7b\u578b\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u6ce8\u610f\uff0c\u6700\u597d\u662f\u7528 typeof \u6765\u5224\u65ad\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u5305\u62ecsymbol\uff09\uff0c\u907f\u514d\u5bf9 null \u7684\u5224\u65ad\u3002\u4e0d\u8fc7\u9700\u8981\u6ce8\u610f\u5f53\u7528typeof\u6765\u5224\u65adnull\u7c7b\u578b\u65f6\u7684\u95ee\u9898\uff0c\u5982\u679c\u60f3\u8981\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u7684\u5177\u4f53\u7c7b\u578b\u53ef\u4ee5\u8003\u8651\u4f7f\u7528instanceof\uff0c\u4f46\u662f\u5f88\u591a\u65f6\u5019\u5b83\u7684\u5224\u65ad\u6709\u5199\u4e0d\u51c6\u786e\u3002\u6240\u4ee5\u5f53\u6211\u4eec\u5728\u8981\u51c6\u786e\u7684\u5224\u65ad\u5bf9\u8c61\u5b9e\u4f8b\u7684\u7c7b\u578b\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528`Object.prototype.toString.call()`\u8fdb\u884c\u5224\u65ad\u3002\u56e0\u4e3a`Object.prototype.toString.call()`\u662f\u5f15\u64ce\u5185\u90e8\u7684\u65b9\u5f0f\u3002\n\n\n", "answer": "", "biz_type": 1, "qid": 616, "subject": "", "title": "JavaScript \u57fa\u672c\u6570\u636e\u7c7b\u578b\u90fd\u6709\u54ea\u4e9b\uff1f\u7528 typeOf \u5224\u65ad\u5206\u522b\u663e\u793a\u4ec0\u4e48\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9177\u5bb6\u4e50"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 531, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4e00\u3001\u5355\u4f8b\u6a21\u5f0f(Singleton)\n\n\u9650\u5236\u4e86\u7c7b\u7684\u5b9e\u4f8b\u5316\u6b21\u6570\u53ea\u80fd\u4e00\u6b21\u3002\u4ece\u7ecf\u5178\u610f\u4e49\u6765\u5c06\uff0c\u5355\u4f8b\u6a21\u5f0f\uff0c**\u5728\u5b9e\u4f8b\u4e0d\u5b58\u5728\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u7c7b\u6765\u5b9e\u73b0\u521b\u5efa\u7c7b\u7684\u65b0\u5b9e\u4f8b\uff1b\u5982\u679c\u5b9e\u4f8b\u5df2\u7ecf\u5b58\u5728\uff0c\u4ed6\u4f1a\u7b80\u5355\u8fd4\u56de\u8be5\u5bf9\u8c61\u7684\u5f15\u7528**\u3002\u5355\u4f8b\u6a21\u5f0f\u4e0d\u540c\u4e8e**\u9759\u6001\u7c7b**\uff0c**\u53ef\u4ee5\u63a8\u8fdf\u5b83\u4eec\u7684\u521d\u59cb\u5316**\uff0c\u8fd9\u901a\u5e38\u662f\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u4e00\u4e9b\u4fe1\u606f\uff0c\u800c\u8fd9\u4e9b\u4fe1\u606f\u5728\u521d\u59cb\u5316\u7684\u65f6\u5019\u53ef\u80fd\u65e0\u6cd5\u83b7\u53d6\u5230\uff1b\u5bf9\u4e8e\u6ca1\u6709\u64e6\u89c9\u5230\u4e4b\u524d\u5f15\u7528\u7684\u4ee3\u7801\uff0c\u4e0d\u4f1a\u63d0\u4f9b\u65b9\u4fbf\u68c0\u7d22\u7684\u65b9\u6cd5\n\n**\u4f18\u70b9**\uff1a \u80fd\u591f\u5355\u72ec\u5212\u5206\u51fa\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\uff0c\u907f\u514d\u548c\u522b\u7684\u5185\u90e8\u53d8\u91cf\u53d1\u751f\u51b2\u7a81\uff0c\u6240\u4ee5\u5355\u4f8b\u53ef\u4ee5\u5206\u4e3a\u7b80\u5355\u5355\u4f8b\u548c\u95ed\u5305\u5355\u4f8b\n\n\u9879\u76ee\u7684\u5b9e\u9645\u7528\u9014\uff1a\n\n#### 1.\u7b80\u5355\u5355\u4f8b\n\n```js\n// \u5224\u65ad\u5b9e\u4f8b\u662f\u5426\u5b58\u5728\uff0c\u5b58\u5728\u5219\u8fd4\u56de\uff0c\u4e0d\u5b58\u5728\u5219\u521b\u5efa\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\nvar testSingle = testSingle||{\n name: 'jack',\n age: 15,\n gender: '1',\n sayName: function(){\n console.log(this.name)\n },\n sayAge: function(){\n console.log(this.age)\n }\n}\n```\n\n#### 2.\u95ed\u5305\u5355\u4f8b\n\n```js\n// \u95ed\u5305\u7684\u4f5c\u7528\u662f\u4fdd\u62a4\u4e00\u4e9b\u79c1\u6709\u5c5e\u6027\uff0c\u4e0d\u88ab\u5916\u754c\u8bbf\u95ee\uff0c\u53ea\u6709return\u5c06\u5c5e\u6027\u66b4\u9732\u624d\u80fd\u88ab\u5916\u754c\u8bbf\u95ee\u5230\nvar testSingle = testSingle||{\n introduction = (function(){\n var _name = 'jack'\n var _age = 15\n var _gender = '1'\n var _sayName = function(){\n console.log(_name)\n }\n var _sayAge = function(){\n console.log(_age)\n }\n return {\n name: _name,\n age: _age,\n gender: _gender,\n sayName: function(){\n return _sayName();\n },\n sayAge: function(){\n return _sayAge()\n }\n }\n }\n}\n```\n\n#### 3.\u5e94\u7528\u573a\u666f\n1. \u5f39\u7a97\uff0c\u65e0\u8bba\u70b9\u51fb\u591a\u5c11\u6b21\uff0c\u5f39\u7a97\u53ea\u5e94\u8be5\u88ab\u521b\u5efa\u4e00\u6b21\uff1b\n2. \u5168\u5c40\u7f13\u5b58\n3. `vuex`\u521b\u5efa\u5168\u5c40\u7684`store`\n4. Redux\u4e2d\u7684Store\n\n\n### \u4e8c\u3001\u5de5\u5382\u6a21\u5f0f\n\nFactory\u6a21\u5f0f\u662f\u4e00\u79cd\u521b\u5efa\u578b\u6a21\u5f0f\uff0c\u6d89\u53ca\u5230\u521b\u5efa\u5bf9\u8c61\u7684\u6982\u5ff5\u3002\u5176\u5206\u7c7b\u4e0d\u540c\u4e8e\u5176\u4ed6\u6a21\u5f0f\u7684\u5730\u65b9\u5728\u4e8e\u5b83\u4e0d\u663e\u5f0f\u5730\u8981\u6c42\u4f7f\u7528\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u3002\u800c`Factory`\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u901a\u7528\u7684\u63a5\u53e3\u6765\u521b\u5efa\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u6211\u4eec\u6240\u5e0c\u671b\u521b\u5efa\u7684\u5de5\u5382\u5bf9\u8c61\u7c7b\u578b\n\n```js\n// \u4e00\u4e2a\u7b80\u5355\u7684\u5de5\u5382\nfunction PersonFactory(name) { // \u5de5\u5382\u51fd\u6570\n let obj = new Object();\n obj.name = name; \n obj.sayName = function(){\n return this.name;\n }\n return obj;\n}\nlet person = new PersonFactory(\"\u5f20\u4e09\");\n\nconsole.log(person.name); // \u5f20\u4e09\nconsole.log(person.sayName()); // \u5f20\u4e09\n```\n#### \u5e94\u7528\u573a\u666f\n\n1. \u521b\u5efa\u5de5\u5177\u5e93\uff0c\u5bfc\u51fa\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u7684\u5f15\u7528\u5982\uff1a`jquery`\u53ef\u4ee5\u4f7f\u7528`$ `,`lodash`\u53ef\u4ee5\u4f7f\u7528`_`\n2. \u7c7b\u4f3cReact.createElement\uff0c\u5c4f\u853d\u4e86\u5f00\u53d1\u8005\u76f4\u63a5\u4f7f\u7528new VNode\uff0c\u7b26\u5408\u5f00\u653e\u5c01\u95ed\u539f\u5219\uff0cVNode\u7684\u5b9e\u73b0\u5bf9\u5f00\u53d1\u8005\u4e0d\u53ef\u89c1\n\n### \u4e09\u3001\u89c2\u5bdf\u8005\u6a21\u5f0f\n\n\u76ee\u6807\u548c\u89c2\u5bdf\u8005\u662f\u57fa\u7c7b\uff0c\u76ee\u6807\u63d0\u4f9b\u7ef4\u62a4\u89c2\u5bdf\u8005\u7684\u4e00\u4e9b\u4e86\u65b9\u6cd5\uff0c\u89c2\u5bdf\u8005\u63d0\u4f9b\u66f4\u65b0\u63a5\u53e3\u3002\u5177\u4f53\u89c2\u5bdf\u8005\u548c\u5177\u4f53\u76ee\u6807\u7ee7\u627f\u5404\u81ea\u7684\u57fa\u7c7b\uff0c\u7136\u540e\u5177\u4f53\u89c2\u5bdf\u8005\u628a\u81ea\u5df1\u6ce8\u518c\u5230\u76ee\u6807\u91cc\uff0c\u5728\u54ed\u557c\u76ee\u6807\u53d1\u751f\u53d8\u5316\u65f6\u5019\uff0c\u8c03\u5ea6\u89c2\u5bdf\u8005\u66f4\u65b0\u65b9\u6cd5\u3002\n\n```js\n// es6\nclass Subject {\n constructor() {\n this.state = 0;\n this.observers = []\n }\n getState() {\n return this.state\n }\n setState(state) {\n this.state = state;\n this.notify();\n }\n notify() {\n this.observers.forEach(observer => {\n observer.update()\n })\n }\n add(observer) {\n this.observers.push(observer)\n }\n}\n\nclass Observer {\n constructor(name, subject) {\n this.name = name;\n this.subject = subject;\n this.subject.add(this)\n }\n update() {\n console.warn(`${this.name} \u88ab\u66f4\u65b0\uff0c\u72b6\u6001\u4e3a${this.subject.getState()}`)\n }\n}\n\nlet sub = new Subject();\nlet ob = new Observer(\"ob\", sub); \n\nsub.setState(1)\n\n// es5\nvar Subject = function () {\n this.state = 0;\n this.observers = []\n}\nSubject.prototype.getState = function () {\n return this.state\n}\nSubject.prototype.setState = function (state) {\n this.state = state;\n this.notify();\n}\nSubject.prototype.notify = function () {\n this.observers.forEach(observer => {\n observer.update()\n })\n}\nSubject.prototype.add = function (observer) {\n this.observers.push(observer)\n}\nvar Observer = function (name, subject) {\n this.name = name;\n this.subject = subject;\n this.subject.add(this)\n}\nObserver.prototype.update = function () {\n console.warn(`${this.name} \u88ab\u66f4\u65b0\uff0c\u72b6\u6001\u4e3a${this.subject.getState()}`)\n}\nvar sub = new Subject();\nvar ob = new Observer(\"ob\", sub);\n\nsub.setState(1)\n```\n\n\u4e00\u4e2a\u5bf9\u8c61\uff08\u79f0\u4e3asubject\uff09\u7ef4\u6301\u4e00\u7cfb\u5217\u4f9d\u8d56\u4e8e\u5b83(\u89c2\u5bdf\u8005)\u7684\u5bf9\u8c61\uff0c\u5c06\u6709\u5173\u72b6\u6001\u7684\u4efb\u4f55\u53d8\u66f4\u81ea\u52a8\u901a\u77e5\u7ed9\u4ed6\u4eec\n\n\u89c2\u5bdf\u8005\u6a21\u5f0f\u4e00\u822c\u4f7f\u7528**Publish/Subscribe\u6a21\u5f0f\u7684\u53d8\u91cf\u6765\u5b9e\u73b0**\n\n```js\nconst event = {\n registerList: [],\n register: (key, fn) => {\n if(typeof fn !== 'function') {\n console.log('\u8bf7\u6dfb\u52a0\u51fd\u6570');\n return;\n }\n if(!this.registerList[key]) {\n this.registerList[key] = [];\n }\n this.registerList.push(fn);\n },\n trigger(key, ...rest) {\n const funList = this.registerList[key];\n if(!(funList &&funList.length)) {\n return false;\n }\n funList.forEach(fn => {\n fn.apply(this.rest);\n });\n }\n}\n\nevent.register('click', () => {console.log('\u6211\u8ba2\u9605\u4e86')});\nevent.register('click', () => {console.log('\u6211\u4e5f\u8ba2\u9605\u4e86')});\nevent.trigger('click');\n```\n\n#### \u5e94\u7528\u573a\u666f\n\n1. onClick\u7684\u4e8b\u4ef6\u7ed1\u5b9a\n2. vue\u4e2d\u7684watch\n3. Promise\n4. jQuery.$callBack\n5. NodeJs\u81ea\u5b9a\u4e49\u4e8b\u4ef6\n", "answer": "", "biz_type": 1, "qid": 531, "subject": "", "title": "\u5355\u4f8b\u3001\u5de5\u5382\u3001\u89c2\u5bdf\u8005\u9879\u76ee\u4e2d\u5b9e\u9645\u573a\u666f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 659, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4e00\u3001Object.prototype.toString \n\nObject.prototype.toString \u65b9\u6cd5\u8fd4\u56de\u5bf9\u8c61\u7684\u7c7b\u578b\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u4e00\u4e2a\u503c\u7684\u7c7b\u578b\u3002\n\n```js\nObject.prototype.toString.call(2) // \"[object Number]\"\nObject.prototype.toString.call('') // \"[object String]\"\nObject.prototype.toString.call(true) // \"[object Boolean]\"\nObject.prototype.toString.call(undefined) // \"[object Undefined]\"\nObject.prototype.toString.call(null) // \"[object Null]\"\nObject.prototype.toString.call(Math) // \"[object Math]\"\nObject.prototype.toString.call({}) // \"[object Object]\"\nObject.prototype.toString.call([]) // \"[object Array]\"\n```\n\n\u5229\u7528\u8fd9\u4e2a\u7279\u6027\uff0c\u53ef\u4ee5\u5199\u51fa\u4e00\u4e2a\u6bd4 typeof \u8fd0\u7b97\u7b26\u66f4\u51c6\u786e\u7684\u7c7b\u578b\u5224\u65ad\u51fd\u6570\n\n```js\nconst type = function (obj) {\n const str = Object.prototype.toString.call(obj);\n return str.match(/\\[object (.*?)\\]/)[1].toLowerCase();\n}\n\ntype(true) // \"boolean\"\ntype(undefined) // \"undefined\"\ntype(null) // \"null\"\ntype({}) // \"object\"\n```\n\n### \u4e8c\u3001Object.prototype.toString.call(Symbol) \n\n```js\nObject.prototype.toString.call(Symbol);\n//\"[object Function]\"\n```", "answer": "", "biz_type": 1, "qid": 659, "subject": "", "title": "\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u5199\u4e2a\u65b9\u6cd5\u7528 Object.prototype.toString \u5224\u65ad\u4f20\u5165\u6570\u636e\u7684\u7c7b\u578b\uff1fObject.prototype.toString.call(Symbol) \u8fd4\u56de\u4ec0\u4e48\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f1a\u5c0f\u4e8c", "\u5b89\u5c45\u5ba2"], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 610, "qtype": "short_answer", "short_answer": {"analysis": "\n### jsonp\u539f\u7406\n\n#### 1.1 \u95ee\u9898\u80cc\u666f\n\n\u7531\u4e8e\u6d4f\u89c8\u5668\u540c\u6e90\u7b56\u7565\uff08\u540c\u4e00\u534f\u8bae\uff0c\u540c\u4e00\u57df\u540d\uff0c\u540c\u4e00\u7aef\u53e3\u53f7\uff0c\u5f53\u5176\u4e2d\u4e00\u4e2a\u4e0d\u6ee1\u8db3\u7684\u65f6\u5019\uff0c\u8bf7\u6c42\u5c31\u4f1a\u53d1\u751f\u8de8\u57df\uff09\u7684\u9650\u5236\uff0c\u975e\u540c\u6e90\u4e0b\u7684\u8bf7\u6c42\uff0c\u90fd\u4f1a\u4ea7\u751f\u8de8\u57df\u95ee\u9898\uff0cjsonp\u5373\u662f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u51fa\u73b0\u7684\u4e00\u79cd\u7b80\u4fbf\u89e3\u51b3\u65b9\u6848\u3002\n\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a\n\n```\nhttp://www.abc.com:3000\u5230https://www.abc.com:3000\u7684\u8bf7\u6c42\u4f1a\u51fa\u73b0\u8de8\u57df\uff08\u57df\u540d\u3001\u7aef\u53e3\u76f8\u540c\u4f46\u534f\u8bae\u4e0d\u540c\uff09\nhttp://www.abc.com:3000\u5230http://www.abc.com:3001\u7684\u8bf7\u6c42\u4f1a\u51fa\u73b0\u8de8\u57df\uff08\u57df\u540d\u3001\u534f\u8bae\u76f8\u540c\u4f46\u7aef\u53e3\u4e0d\u540c\uff09\nhttp://www.abc.com:3000\u5230http://www.def.com:3000\u7684\u8bf7\u6c42\u4f1a\u51fa\u73b0\u8de8\u57df\uff08\u57df\u540d\u4e0d\u540c\uff09\n```\n\n#### 1.2 \u7a81\u7834\u540c\u6e90\u7b56\u7565\n\n\u6d4f\u89c8\u5668\u7684 \u540c\u6e90\u7b56\u7565 \u628a\u8de8\u57df\u8bf7\u6c42\u90fd\u7981\u6b62\u4e86\uff0c\u4f46\u662f\u9875\u9762\u4e2d `link`\u00a0 `script`\u00a0`img`\u00a0`iframe`\u00a0`a`\u00a0\u6807\u7b7e\u662f\u4e2a\u4f8b\u5916\uff0c\u8fd9\u4e9b\u6807\u7b7e\u7684\u5916\u94fe\u662f\u4e0d\u53d7\u540c\u6e90\u7b56\u7565\u9650\u5236\u7684\u3002\n\n`jsonp`\u00a0\u5c31\u662f\u5229\u7528\u4e86\u4e0a\u9762 `script`\u00a0\u6807\u7b7e\u7279\u6027\u6765\u8fdb\u884c\u8de8\u57df\u6570\u636e\u8bbf\u95ee\u3002\n\n#### 1.3 jsonp \u7684\u5b9e\u73b0\u673a\u5236\n\n1. \u4e0e\u670d\u52a1\u7aef\u7ea6\u5b9a\u597d\u4e00\u4e2a \u56de\u8c03\u51fd\u6570\u540d \uff0c\u5728\u5ba2\u6237\u7aef\u5b9a\u4e49\u597d\u8fd9\u4e2a\u51fd\u6570\uff0c\u5728\u8bf7\u6c42url\u4e2d\u6dfb\u52a0 `callback= \u51fd\u6570\u540d`\u00a0\u7684\u67e5\u8be2\u5b57\u7b26\u3002\n1. \u670d\u52a1\u7aef\u63a5\u6536\u5230\u8bf7\u6c42\u4e4b\u540e\uff0c\u5c06\u51fd\u6570\u540d\u548c\u9700\u8981\u8fd4\u56de\u7684\u6570\u636e\uff0c\u62fc\u63a5\u6210 `\"\u51fd\u6570\u540d(data)\"`\u00a0\u51fd\u6570\u6267\u884c\u7684\u5f62\u5f0f\u8fd4\u56de\n1. \u9875\u9762\u63a5\u6536\u5230\u6570\u636e\u540e\uff0c\u89e3\u6790\u5b8c\u76f4\u63a5\u6267\u884c\u4e86\u8fd9\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u8fd9\u65f6\u6570\u636e\u5c31\u6210\u529f\u4f20\u8f93\u5230\u4e86\u5ba2\u6237\u7aef\u3002\n\n- \u5ba2\u6237\u7aef\u4ee3\u7801\uff1a\n\n```js\n var flightHandler = function (data) {\n alert('\u4f60\u67e5\u8be2\u7684\u822a\u73ed\u7ed3\u679c\u662f\uff1a\u7968\u4ef7 ' + data.price + ' \u5143\uff0c' + '\u4f59\u7968 ' + data.tickets + ' \u5f20\u3002');\n };\n \n var url = \"http://localhost:8080/jsonp?callback=flightHandler\";\n var script = document.createElement('script');\n script.setAttribute('src', url);\n document.getElementsByTagName('head')[0].appendChild(script);\n```\n\n- \u670d\u52a1\u7aef\u4ee3\u7801\uff1akoa2\u5b9e\u73b0\u4e00\u4e2a\u670d\u52a1\n\n```js\napp.use(async (ctx, next) => {\n if (ctx.path == '/jsonp' && ctx.querystring) {\n //querystring\u5904\u7406\n let queryArr = ctx.querystring.split(\"&\");\n let queryObj = {};\n queryArr.forEach((item) => {\n let tmp = item.split(\"=\"); \n queryObj[tmp[0]] = tmp[1];\n })\n const callback = queryObj['callback'];\n const obj = {\n price: '18',\n tickets: 33\n }\n const args = JSON.stringify(obj);\n ctx.body = `${callback}(${args})`;\n }\n await next();\n})\n```\n\n#### 1.4 \u603b\u7ed3\n\n\u6240\u4ee5\uff0c\u5c31\u662f\u5229\u7528 `<script>`\u6807\u7b7e\u6ca1\u6709\u8de8\u57df\u9650\u5236\u7684\u201c\u6f0f\u6d1e\u201d\u6765\u8fbe\u5230\u4e0e\u7b2c\u4e09\u65b9\u901a\u8baf\u7684\u76ee\u7684\u3002\n\n\u5f53\u9700\u8981\u901a\u8baf\u65f6\uff0c\u672c\u7ad9\u811a\u672c\u521b\u5efa\u4e00\u4e2a `<script>` \u5143\u7d20\uff0c\u5730\u5740\u6307\u5411\u7b2c\u4e09\u65b9\u7684API\u7f51\u5740\uff0c\u5f62\u5982\uff1a`<script src=\"http://www.example.net/api?param1=1&param2=2\"></script>` \u5e76\u63d0\u4f9b\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u6765\u63a5\u6536\u6570\u636e\uff08\u51fd\u6570\u540d\u53ef\u7ea6\u5b9a\uff0c\u6216\u901a\u8fc7\u5730\u5740\u53c2\u6570\u4f20\u9012\uff09\u3002 \n\n\u7b2c\u4e09\u65b9\u4ea7\u751f\u7684\u54cd\u5e94\u4e3ajson\u6570\u636e\u7684\u5305\u88c5\uff08\u6545\u79f0\u4e4b\u4e3ajsonp\uff0c\u5373json padding\uff09\uff0c\u5f62\u5982\uff1a`allback({\"name\":\"hax\",\"gender\":\"Male\"})` \n\n\u8fd9\u6837\u6d4f\u89c8\u5668\u4f1a\u8c03\u7528callback\u51fd\u6570\uff0c\u5e76\u4f20\u9012\u89e3\u6790\u540ejson\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5b8c\u6210\u4e00\u6b21\u6570\u636e\u4ea4\u4e92\u3002", "answer": "", "biz_type": 1, "qid": 610, "subject": "", "title": "\u8bf7\u89e3\u91ca JSONP \u7684\u5de5\u4f5c\u539f\u7406"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:43 GMT", "favorite_num": 4, "qid": 648, "qtype": "short_answer", "short_answer": {"analysis": "\n### addEventListener\n\naddEventListener \u518d removeListener \u4e0d\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\uff1b\n\n\u9020\u6210\u5185\u5b58\u6cc4\u9732\u7684\u539f\u56e0\u6709\n- \u4e0d\u6070\u5f53\u7684\u7f13\u5b58\n- \u961f\u5217\u6d88\u8d39\u4e0d\u53ca\u65f6\n- \u4f5c\u7528\u57df\u672a\u91ca\u653e\n\n\u5c24\u5176\u662f\u5904\u7406\u51fd\u6570\u4e2d\u53ef\u80fd\u5f15\u7528\u7740\u7236\u7ea7\u4f5c\u7528\u57df\uff0c\u5bfc\u81f4\u4f5c\u7528\u57df\u4e2d\u7684\u6570\u636e\u4e0d\u80fd\u88ab\u56de\u6536\u3002\u6240\u4ee5\u5728\u6070\u5f53\u7684\u65f6\u5019\uff0c\u79fb\u9664\u76d1\u542c\u7684\u4e8b\u4ef6\u662f\u5f88\u5fc5\u8981\u7684\u3002\n\n### \u73b0\u5728\u6d4f\u89c8\u5668\u6dfb\u52a0\u539f\u751f\u4e8b\u4ef6\n\n\u6dfb\u52a0\u539f\u751f\u4e8b\u4ef6\n\n```js\nvar button = document.getElementById('button');\nfunction onClick(event) {\n button.innerHTML = 'text';\n}\nbutton.addEventListener('click', onClick);\n```\n\n\u7ed9\u5143\u7d20button\u6dfb\u52a0\u4e86\u4e00\u4e2a\u4e8b\u4ef6\u5904\u7406\u5668onClick, \u800c\u5904\u7406\u5668\u91cc\u9762\u4f7f\u7528\u4e86button\u7684\u5f15\u7528\u3002\u800c\u8001\u7248\u672c\u7684 IE \u662f\u65e0\u6cd5\u68c0\u6d4b DOM \u8282\u70b9\u4e0e JavaScript \u4ee3\u7801\u4e4b\u95f4\u7684\u5faa\u73af\u5f15\u7528\uff0c\u56e0\u6b64\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002\n\n\u5982\u4eca\uff0c\u73b0\u4ee3\u7684\u6d4f\u89c8\u5668\uff08\u5305\u62ec IE \u548c Microsoft Edge\uff09\u4f7f\u7528\u4e86\u66f4\u5148\u8fdb\u7684\u5783\u573e\u56de\u6536\u7b97\u6cd5\uff0c\u5df2\u7ecf\u53ef\u4ee5\u6b63\u786e\u68c0\u6d4b\u548c\u5904\u7406\u5faa\u73af\u5f15\u7528\u4e86\u3002\u6362\u8a00\u4e4b\uff0c\u56de\u6536\u8282\u70b9\u5185\u5b58\u65f6\uff0c\u4e0d\u5fc5\u975e\u8981\u8c03\u7528 removeEventListener \u4e86\u3002\n", "answer": "", "biz_type": 1, "qid": 648, "subject": "", "title": "addEventListener \u518d removeListener \u4f1a\u4e0d\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 314, "qtype": "short_answer", "short_answer": {"analysis": "- **\u5c01\u88c5\uff1a** \u628a\u5ba2\u89c2\u4e8b\u7269\u5c01\u88c5\u6210\u62bd\u8c61\u7684\u7c7b\uff0c\u5e76\u4e14\u7c7b\u53ef\u4ee5\u628a\u81ea\u5df1\u7684\u6570\u636e\u548c\u65b9\u6cd5\u53ea\u8ba9\u53ef\u4fe1\u7684\u7c7b\u6216\u8005\u5bf9\u8c61\u64cd\u4f5c\uff0c\u5bf9\u4e0d\u53ef\u4fe1\u7684\u8fdb\u884c\u4fe1\u606f\u9690\u85cf\n- **\u7ee7\u627f\uff1a** \u4f7f\u7528\u73b0\u6709\u7c7b\u7684\u6240\u6709\u529f\u80fd\uff0c\u5e76\u5728\u65e0\u9700\u91cd\u65b0\u7f16\u5199\u539f\u6765\u7684\u7c7b\u7684\u60c5\u51b5\u4e0b\u5bf9\u8fd9\u4e9b\u529f\u80fd\u8fdb\u884c\u6269\u5c55\n- **\u591a\u6001\uff1a** \u4e00\u4e2a\u7c7b\u5b9e\u4f8b\u7684\u76f8\u540c\u65b9\u6cd5\u5728\u4e0d\u540c\u60c5\u5f62\u6709\u4e0d\u540c\u8868\u73b0\u5f62\u5f0f\u3002\u591a\u6001\u673a\u5236\u4f7f\u5177\u6709\u4e0d\u540c\u5185\u90e8\u7ed3\u6784\u7684\u5bf9\u8c61\u53ef\u4ee5\u5171\u4eab\u76f8\u540c\u7684\u5916\u90e8\u63a5\u53e3", "answer": "", "biz_type": 1, "qid": 314, "subject": "", "title": "\u9762\u5411\u5bf9\u8c61\u7684\u4e09\u8981\u7d20\u662f\u5565\uff1f\u90fd\u662f\u5565\u610f\u601d\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u817e\u8baf\u5e94\u7528\u5b9d", "\u5b89\u5c45\u5ba2"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 310, "qtype": "short_answer", "short_answer": {"analysis": "### \u7ee7\u627f\n\n\u7ee7\u627f\u662fOO\u8bed\u8a00\u4e2d\u7684\u4e00\u4e2a\u6700\u4e3a\u4eba\u6d25\u6d25\u4e50\u9053\u7684\u6982\u5ff5.\u8bb8\u591aOO\u8bed\u8a00\u90fd\u652f\u6301\u4e24\u79cd\u7ee7\u627f\u65b9\u5f0f: \u63a5\u53e3\u7ee7\u627f \u548c \u5b9e\u73b0\u7ee7\u627f .\u63a5\u53e3\u7ee7\u627f\u53ea\u7ee7\u627f\u65b9\u6cd5\u7b7e\u540d,\u800c\u5b9e\u73b0\u7ee7\u627f\u5219\u7ee7\u627f\u5b9e\u9645\u7684\u65b9\u6cd5.\u7531\u4e8ejs\u4e2d\u65b9\u6cd5\u6ca1\u6709\u7b7e\u540d,\u5728ECMAScript\u4e2d\u65e0\u6cd5\u5b9e\u73b0\u63a5\u53e3\u7ee7\u627f.ECMAScript\u53ea\u652f\u6301\u5b9e\u73b0\u7ee7\u627f,\u800c\u4e14\u5176 \u5b9e\u73b0\u7ee7\u627f \u4e3b\u8981\u662f\u4f9d\u9760\u539f\u578b\u94fe\u6765\u5b9e\u73b0\u7684.\n\n### \u6784\u9020\u51fd\u6570,\u539f\u578b\u548c\u5b9e\u4f8b\u7684\u5173\u7cfb:\n\n\u6bcf\u4e2a\u6784\u9020\u51fd\u6570(constructor)\u90fd\u6709\u4e00\u4e2a\u539f\u578b\u5bf9\u8c61(prototype),\u539f\u578b\u5bf9\u8c61\u90fd\u5305\u542b\u4e00\u4e2a\u6307\u5411\u6784\u9020\u51fd\u6570\u7684\u6307\u9488,\u800c\u5b9e\u4f8b(instance)\u90fd\u5305\u542b\u4e00\u4e2a\u6307\u5411\u539f\u578b\u5bf9\u8c61\u7684\u5185\u90e8\u6307\u9488.\n\n\u770b\u4e2a\u539f\u578b\u94fe\u7ee7\u627f\u7684\u4f8b\u5b50\n\n```js\nfunction Father(){\n\tthis.property = true;\n}\nFather.prototype.getFatherValue = function(){\n\treturn this.property;\n}\nfunction Son(){\n\tthis.sonProperty = false;\n}\n//\u7ee7\u627f Father\nSon.prototype = new Father();//Son.prototype\u88ab\u91cd\u5199,\u5bfc\u81f4Son.prototype.constructor\u4e5f\u4e00\u540c\u88ab\u91cd\u5199\nSon.prototype.getSonVaule = function(){\n\treturn this.sonProperty;\n}\nvar instance = new Son();\nalert(instance.getFatherValue());//true\n```\n\n\u4f46\u662f\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u539f\u578b\u94fe\u5e76\u975e\u5341\u5206\u5b8c\u7f8e\uff0c\u4f1a\u5b58\u5728\u4ee5\u4e0b\u95ee\u9898\uff1a\n\n- \u95ee\u9898\u4e00: \u5f53\u539f\u578b\u94fe\u4e2d\u5305\u542b\u5f15\u7528\u7c7b\u578b\u503c\u7684\u539f\u578b\u65f6,\u8be5\u5f15\u7528\u7c7b\u578b\u503c\u4f1a\u88ab\u6240\u6709\u5b9e\u4f8b\u5171\u4eab;\n- \u95ee\u9898\u4e8c: \u5728\u521b\u5efa\u5b50\u7c7b\u578b(\u4f8b\u5982\u521b\u5efaSon\u7684\u5b9e\u4f8b)\u65f6,\u4e0d\u80fd\u5411\u8d85\u7c7b\u578b(\u4f8b\u5982Father)\u7684\u6784\u9020\u51fd\u6570\u4e2d\u4f20\u9012\u53c2\u6570.\n\n\u6240\u4ee5\u5728\u5b9e\u8df5\u4e2d\uff0c\u5f88\u5c11\u4f1a\u5355\u72ec\u4f7f\u7528\u539f\u578b\u94fe\u3002\u4ece\u800c\u51fa\u73b0\u4e86\u4e00\u4e9b\u5f25\u8865\u65b9\u6848\n\n- \u501f\u7528\u6784\u9020\u51fd\u6570\u7ee7\u627f\n- \u7ec4\u5408\u7ee7\u627f\n- \u539f\u578b\u7ee7\u627f\n- \u5bc4\u751f\u5f0f\u7ee7\u627f\n- \u5bc4\u751f\u7ec4\u5408\u5f0f\u7ee7\u627f", "answer": "", "biz_type": 1, "qid": 310, "subject": "", "title": "\u4f7f\u7528\u539f\u578b\u94fe\u5982\u4f55\u5b9e\u73b0\u7ee7\u627f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 319, "qtype": "short_answer", "short_answer": {"analysis": "### \u7b54\u6848\n\n```js\n1 2\n```\n\n### \u89e3\u6790\n\ncatch() \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2aPromise\uff0c\u5e76\u4e14\u5904\u7406\u62d2\u7edd\u7684\u60c5\u51b5\u3002\u5b83\u7684\u884c\u4e3a\u4e0e\u8c03\u7528Promise.prototype.then(undefined, onRejected) \u76f8\u540c\u3002 (\u4e8b\u5b9e\u4e0a, calling obj.catch(onRejected) \u5185\u90e8calls obj.then(undefined, onRejected))\u3002\n\n\u6240\u4ee5\u8f93\u51fa1\uff0c2", "answer": "", "biz_type": 1, "qid": 319, "subject": "```js\nnew Promise((resolve, reject) => {\n reject(\"1\");\n})\n .catch((e) => {\n console.log(1);\n })\n .then((res) => {\n console.log(2);\n });\n```\n", "title": "\u5199\u51fa\u4ee3\u7801\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 318, "qtype": "short_answer", "short_answer": {"analysis": "### \u6709\u4e09\u79cd\u53ef\u80fd\n\nPromise.resolve(value)\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u4ee5\u7ed9\u5b9a\u503c\u89e3\u6790\u540e\u7684 Promise \u5bf9\u8c61\u3002\n\n- \u5982\u679c\u8fd9\u4e2a\u503c\u662f\u4e00\u4e2a promise \uff0c\u90a3\u4e48\u5c06\u8fd4\u56de\u8fd9\u4e2a promise \uff1b\n- \u5982\u679c\u8fd9\u4e2a\u503c\u662f thenable\uff08\u5373\u5e26\u6709\"then\" \u65b9\u6cd5\uff09\uff0c\u8fd4\u56de\u7684 promise \u4f1a\u201c\u8ddf\u968f\u201d\u8fd9\u4e2a thenable \u7684\u5bf9\u8c61\uff0c\u91c7\u7528\u5b83\u7684\u6700\u7ec8\u72b6\u6001\uff1b\n- \u5426\u5219\u8fd4\u56de\u7684 promise \u5c06\u4ee5\u6b64\u503c\u5b8c\u6210\u3002\u6b64\u51fd\u6570\u5c06\u7c7b promise \u5bf9\u8c61\u7684\u591a\u5c42\u5d4c\u5957\u5c55\u5e73\u3002\n\n\u6545obj\u6709\u4e09\u79cd\u53ef\u80fd\uff0cobj\u53ef\u4ee5\u662f\u6b63\u5e38\u503c\uff0c\u53ef\u4ee5\u662fpromise\u5bf9\u8c61\uff0c\u53ef\u4ee5\u662fthenable\u3002", "answer": "", "biz_type": 1, "qid": 318, "subject": "", "title": "Promise.resolve(obj)\uff0cobj \u6709\u51e0\u79cd\u53ef\u80fd"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 364, "qtype": "short_answer", "short_answer": {"analysis": "### \u641c\u7d22\u8bf7\u6c42\u5904\u7406\n\n\u641c\u7d22\u8fc7\u7a0b\u4e2d, \u5feb\u901f\u7684\u5b57\u7b26\u8f93\u5165/\u66f4\u53d8, \u4f1a\u5bfc\u81f4\u8fc7\u4e8e\u9891\u7e41\u7684\u8bf7\u6c42, \u6d6a\u8d39\u8bf7\u6c42\u652f\u63f4, \u4e00\u822c\u8003\u8651\u9632\u6296(debounce)\u8fdb\u884c\u4f18\u5316;\n\n**\u9632\u6296**: \u6301\u7eed\u89e6\u53d1\u4e8b\u4ef6\u65f6\uff0c\u4e00\u5b9a\u65f6\u95f4\u5185\u6ca1\u6709\u518d\u89e6\u53d1\u4e8b\u4ef6\uff0c\u51fd\u6570\u6267\u884c\u4e00\u6b21; \u5982\u679c\u8bbe\u5b9a\u7684\u65f6\u95f4\u4e4b\u524d\uff0c\u53c8\u89e6\u53d1\u4e86\u4e8b\u4ef6\uff0c\u5c31\u91cd\u65b0\u5f00\u59cb\u5ef6\u65f6\n\n**\u9632\u6296\u5b9e\u73b0**\n\n```js\nfunction debounce(fn, wait) { \n var timeout = null; \n return function() { \n if(timeout !== null) {\n clearTimeout(timeout)\n }; \n timeout = setTimeout(fn, wait); \n }\n}\n```\n\n### \u641c\u7d22\u8bf7\u6c42\u4e2d\u6587\u5982\u4f55\u5904\u7406\n\n\u4e00\u822c\u662f\u901a\u8fc7encodeURI\u6216encodeURIComponent\u65b9\u6cd5\u5c06\u5bf9\u5e94\u7684\u4e2d\u6587\u8fdb\u884c\u7f16\u7801;\n", "answer": "", "biz_type": 1, "qid": 364, "subject": "", "title": "\u641c\u7d22\u8bf7\u6c42\u5982\u4f55\u5904\u7406\uff1f\u641c\u7d22\u8bf7\u6c42\u4e2d\u6587\u5982\u4f55\u8bf7\u6c42\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 408, "qtype": "short_answer", "short_answer": {"analysis": "### \u539f\u578b\u94fe\n\n![prototype](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-828-prototype.jpg)\n\n#### 1.\u6982\u5ff5\n\n\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u6307\u5411\u5b83\u7684\u539f\u578b\uff08prototype\uff09\u5bf9\u8c61\u7684\u5185\u90e8\u94fe\u63a5\u3002\u8fd9\u4e2a\u539f\u578b\u5bf9\u8c61\u53c8\u6709\u81ea\u5df1\u7684\u539f\u578b\uff0c\u76f4\u5230\u67d0\u4e2a\u5bf9\u8c61\u7684\u539f\u578b\u4e3a null \u4e3a\u6b62\uff08\u4e5f\u5c31\u662f\u4e0d\u518d\u6709\u539f\u578b\u6307\u5411\uff09\uff0c\u7ec4\u6210\u8fd9\u6761\u94fe\u7684\u6700\u540e\u4e00\u73af\u3002\u8fd9\u79cd\u4e00\u7ea7\u4e00\u7ea7\u7684\u94fe\u7ed3\u6784\u5c31\u79f0\u4e3a\u539f\u578b\u94fe\n\n#### 2.\u539f\u578b\u94fe\u7279\u70b9\n\n1. \u6240\u6709\u7684\u5bf9\u8c61\u90fd\u6709\"[[prototype]]\"\u5c5e\u6027\uff08\u901a\u8fc7__proto__\u8bbf\u95ee\uff09\uff0c\u8be5\u5c5e\u6027\u5bf9\u5e94\u5bf9\u8c61\u7684\u539f\u578b\n2. \u6240\u6709\u7684\u51fd\u6570\u5bf9\u8c61\u90fd\u6709\"prototype\"\u5c5e\u6027\uff0c\u8be5\u5c5e\u6027\u7684\u503c\u4f1a\u88ab\u8d4b\u503c\u7ed9\u8be5\u51fd\u6570\u521b\u5efa\u7684\u5bf9\u8c61\u7684\"__proto__\"\u5c5e\u6027\n3. \u6240\u6709\u7684\u539f\u578b\u5bf9\u8c61\u90fd\u6709\"constructor\"\u5c5e\u6027\uff0c\u8be5\u5c5e\u6027\u5bf9\u5e94\u521b\u5efa\u6240\u6709\u6307\u5411\u8be5\u539f\u578b\u7684\u5b9e\u4f8b\u7684\u6784\u9020\u51fd\u6570\n4. \u51fd\u6570\u5bf9\u8c61\u548c\u539f\u578b\u5bf9\u8c61\u901a\u8fc7\"prototype\"\u548c\"constructor\"\u5c5e\u6027\u8fdb\u884c\u76f8\u4e92\u5173\u8054", "answer": "", "biz_type": 1, "qid": 408, "subject": "", "title": "\u4ecb\u7ecd\u4e0b\u539f\u578b\u94fe"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u559c\u9a6c\u62c9\u96c5"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 472, "qtype": "short_answer", "short_answer": {"analysis": "### \u524d\u540e\u7aef\u901a\u4fe1\u65b9\u6848\n\n1. Ajax - \u662f\u540c\u6e90\u4e0b\u7684\u901a\u4fe1\u65b9\u5f0f\n2. WebSocket - \u4e0d\u53d7\u540c\u6e90\u9650\u5236\n3. CORS - \u652f\u6301\u8de8\u57df\u4e5f\u652f\u6301\u540c\u6e90\n\n#### websocket\n\nHTTP \u534f\u8bae\u6709\u4e00\u4e2a\u7f3a\u9677\uff1a\u901a\u4fe1\u53ea\u80fd\u7531\u5ba2\u6237\u7aef\u53d1\u8d77\u3002\u6240\u4ee5\u51fa\u73b0\u4e86 WebSocket \u3002\u5b83\u7684\u6700\u5927\u7279\u70b9\u5c31\u662f\uff0c\u670d\u52a1\u5668\u53ef\u4ee5\u4e3b\u52a8\u5411\u5ba2\u6237\u7aef\u63a8\u9001\u4fe1\u606f\uff0c\u5ba2\u6237\u7aef\u4e5f\u53ef\u4ee5\u4e3b\u52a8\u5411\u670d\u52a1\u5668\u53d1\u9001\u4fe1\u606f\uff0c\u662f\u771f\u6b63\u7684\u53cc\u5411\u5e73\u7b49\u5bf9\u8bdd\uff0c\u5c5e\u4e8e\u670d\u52a1\u5668\u63a8\u9001\u6280\u672f\u7684\u4e00\u79cd\u3002\n\n\u5176\u4ed6\u7279\u70b9\u5305\u62ec\uff1a\n\n1. \u5efa\u7acb\u5728 TCP \u534f\u8bae\u4e4b\u4e0a\uff0c\u670d\u52a1\u5668\u7aef\u7684\u5b9e\u73b0\u6bd4\u8f83\u5bb9\u6613\u3002\n1. \u4e0e HTTP \u534f\u8bae\u6709\u7740\u826f\u597d\u7684\u517c\u5bb9\u6027\u3002\u9ed8\u8ba4\u7aef\u53e3\u4e5f\u662f 80 \u548c 443 \uff0c\u5e76\u4e14\u63e1\u624b\u9636\u6bb5\u91c7\u7528 HTTP \u534f\u8bae\uff0c\u56e0\u6b64\u63e1\u624b\u65f6\u4e0d\u5bb9\u6613\u5c4f\u853d\uff0c\u80fd\u901a\u8fc7\u5404\u79cd HTTP \u4ee3\u7406\u670d\u52a1\u5668\u3002\n3. \u6570\u636e\u683c\u5f0f\u6bd4\u8f83\u8f7b\u91cf\uff0c\u6027\u80fd\u5f00\u9500\u5c0f\uff0c\u901a\u4fe1\u9ad8\u6548\u3002\n4. \u53ef\u4ee5\u53d1\u9001\u6587\u672c\uff0c\u4e5f\u53ef\u4ee5\u53d1\u9001\u4e8c\u8fdb\u5236\u6570\u636e\u3002\n5. \u6ca1\u6709\u540c\u6e90\u9650\u5236\uff0c\u5ba2\u6237\u7aef\u53ef\u4ee5\u4e0e\u4efb\u610f\u670d\u52a1\u5668\u901a\u4fe1\u3002\n6. \u534f\u8bae\u6807\u8bc6\u7b26\u662fws\uff08\u5982\u679c\u52a0\u5bc6\uff0c\u5219\u4e3awss\uff09\uff0c\u670d\u52a1\u5668\u7f51\u5740\u5c31\u662f URL\u3002\n\n#### CORS\n\nCORS\u662f\u4e00\u4e2a W3C \u6807\u51c6\uff0c\u8de8\u57df\u8d44\u6e90\u5171\u4eab\uff08CORS \uff09\u662f\u4e00\u79cd\u7f51\u7edc\u6d4f\u89c8\u5668\u7684\u6280\u672f\u89c4\u8303\uff0c\u5b83\u4e3aWeb\u670d\u52a1\u5668\u5b9a\u4e49\u4e86\u4e00\u79cd\u65b9\u5f0f\uff0c\u5141\u8bb8\u7f51\u9875\u4ece\u4e0d\u540c\u7684\u57df\u8bbf\u95ee\u5176\u8d44\u6e90\u3002\u800c\u8fd9\u79cd\u8bbf\u95ee\u662f\u88ab\u540c\u6e90\u7b56\u7565\u6240\u7981\u6b62\u7684\u3002CORS \u7cfb\u7edf\u5b9a\u4e49\u4e86\u4e00\u79cd\u6d4f\u89c8\u5668\u548c\u670d\u52a1\u5668\u4ea4\u4e92\u7684\u65b9\u5f0f\u6765\u786e\u5b9a\u662f\u5426\u5141\u8bb8\u8de8\u57df\u8bf7\u6c42\u3002 \n\n\u5b83\u662f\u4e00\u4e2a\u59a5\u534f\uff0c\u6709\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u4f46\u6bd4\u8d77\u7b80\u5355\u5730\u5141\u8bb8\u6240\u6709\u8fd9\u4e9b\u7684\u8981\u6c42\u6765\u8bf4\u66f4\u52a0\u5b89\u5168\u3002\u7b80\u8a00\u4e4b\uff0c CORS \u5c31\u662f\u4e3a\u4e86\u8ba9 AJAX \u53ef\u4ee5\u5b9e\u73b0\u53ef\u63a7\u7684\u8de8\u57df\u8bbf\u95ee\u800c\u751f\u7684\u3002", "answer": "", "biz_type": 1, "qid": 472, "subject": "", "title": "\u524d\u540e\u7aef\u901a\u4fe1\u4f7f\u7528\u4ec0\u4e48\u65b9\u6848"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u997f\u4e86\u4e48"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 461, "qtype": "short_answer", "short_answer": {"analysis": "### \u5177\u4f53\u5b9e\u73b0\n\n\n#### 1.\u751f\u6210dom\u5e76\u7ed1\u5b9a\u4e8b\u4ef6\n\n```js\n for (var i = 0; i < 10000; i++) {\n var btn = document.createElement('button');\n btn.innerHtml = \"\u6309\u94ae\" + i;\n document.getElementsByClassName('btns')[0].appendChild(btn);\n }\n```\n\n\u6dfb\u52a0\u5927\u91cfdom\u5143\u7d20\u65f6\uff0c\u51fa\u4e8e\u6027\u80fd\u8003\u8651\uff0c\u53ef\u4ee5\u4f7f\u7528\u6587\u6863\u788e\u7247\u7684\u65b9\u5f0f\u6dfb\u52a0dom\uff0c\u4f18\u52bf\u5728\u4e8e\u53ea\u6dfb\u52a0\u4e00\u6b21\u771f\u5b9edom\u64cd\u4f5c\uff0c\u51cf\u5c11\u5927\u91cf\u91cd\u7ed8\u65f6\u95f4\n\n```js\nvar docFragment = document.createDocumentFragment();\n\nfor (var i = 0; i < 10000; i++) {\n var node = document.createElement('button');\n node.className = 'btn' + i\n node.innerHtml = '\u6309\u94ae' + i;\n docFragment.appendChild(node);\n}\n\ndocument.body.appendChild(docFragment);\n```\n\n#### 2.\u7ed1\u5b9a\u4e8b\u4ef6\n\n- \u5faa\u73af\u7ed1\u5b9a\n\n```javascript\nfor(var i=0;i < 10000;i++){\ndocument.getElementsByClassName('btn'+i)[0].addEventListener('click', function (e) {\n //TODO ...\n //\u5728\u5faa\u73af\u7ed1\u5b9a\u4e2d\uff0c\u9700\u8981\u7528\u5230i\u7684\u503c\uff0c\u8fd9\u79cd\u7ed1\u5b9a\u65b9\u5f0f\u5728\u89e6\u53d1\u70b9\u51fb\u65f6\u5df2\u53d8\u4e3a10000\n //\u89e3\u51b3\u529e\u6cd5\u6709\u4e09\u79cd:\n //1. \u5c06\u5faa\u73af\u4f53\u5185var i= 0;\u6539\u4e3a let i = 0;\n //2.\u4f7f\u7528\u95ed\u5305\u5305\u88f9\u5faa\u73af\u4f53\uff0c\u5c06i\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u95ed\u5305\n //3.\u5728\u751f\u6210button\u65f6\uff0c\u5c06i\u5199\u5165\u5230button\u81ea\u5b9a\u4e49\u5c5e\u6027\u4e0a\u8bb0\u5f55\u4e0b\u6765\uff0c\u5982: btn-index\uff0c\u9700\u8981\u65f6\u518d\u8bfb\u53d6\u5f53\u524d\u5143\u7d20\u7684btn-index\u5c5e\u6027\u503c\n}, false)\n}\n```\n\n\u4e0d\u63a8\u8350\uff0c\u4f1a\u4ea7\u751f\u5927\u91cf\u91cd\u590d\u7684dom\u4e8b\u4ef6\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u3002\n\n- \u4f7f\u7528\u4e8b\u4ef6\u59d4\u6258, \u5229\u7528\u4e86\u4e8b\u4ef6\u7684\u5192\u6ce1\u673a\u5236(\u5047\u5b9abutton\u7236\u8282class\u4e3a btns)\u3002\u63a8\u8350\u4f7f\u7528\u3002\n\n```javascript\ndocument.getElementsByClassName('btns')[0].addEventListener('click', function (e) {\n var e = e || window.event;\n var target = e.target || e.srcElement;\n if (target.tagName == 'BUTTON') {\n //TODO ...\n }\n}, false)\n```\n\n\u6ce8\u610f\u4e00\u70b9\u5c31\u662f\u4e0d\u8981\u963b\u6b62\u5192\u6ce1\u4e8b\u4ef6!", "answer": "", "biz_type": 1, "qid": 461, "subject": "", "title": "\u9875\u9762\u4e0a\u751f\u6210\u4e00\u4e07\u4e2a button\uff0c\u5e76\u4e14\u7ed1\u5b9a\u4e8b\u4ef6\uff0c\u5982\u4f55\u505a\uff08JS \u539f\u751f\u64cd\u4f5c DOM\uff09\uff1f\u5faa\u73af\u7ed1\u5b9a\u65f6\u7684 index \u662f\u591a\u5c11\uff0c\u4e3a\u4ec0\u4e48\uff0c\u600e\u4e48\u89e3\u51b3\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5151\u5427"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 488, "qtype": "short_answer", "short_answer": {"analysis": "### \u4f4e\u7248\u672c\u9002\u914d\u5904\u7406\n\n- \u5f53\u524d\u6d4f\u89c8\u5668\u4e0d\u652f\u6301 fetch \u65f6\uff0c\u4f7f\u7528 `fetch polyfill`\u3002\n- \u7531\u4e8e fetch \u7684\u5e95\u5c42\u5b9e\u73b0\u9700\u8981\u7528\u5230\u5168\u5c40\u4e0b\u7684 Promise\uff0c\u5bf9\u4e8e\u4e0d\u652f\u6301 Promise \u7684\u73af\u5883\u8fd8\u9700\u5728\u5168\u5c40\u6dfb\u52a0 `Promise polyfill`\u3002\n\n**fetch polyfill**\n\n- whatwg-fetch,\u7ed3\u5408 Promise \u4f7f\u7528 XMLHttpRequest \u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\n- isomorphic-fetch\n- fetch-polyfill2", "answer": "", "biz_type": 1, "qid": 488, "subject": "", "title": "\u5f02\u6b65\u8bf7\u6c42\uff0c\u4f4e\u7248\u672c fetch \u5982\u4f55\u4f4e\u7248\u672c\u9002\u914d"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 329, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n```js\nvar str = '<h1 id=\"text\" style=\"background: #ccc;\">\u6d4b\u8bd5\u5143\u7d20</h1>';\nvar reg1 = /\\s+style=\\\"[^\\\"]*\\\"/;\n\nstr= str.replace(reg1, '');\nconsole.log('del style', str);\n```", "answer": "", "biz_type": 1, "qid": 329, "subject": "", "title": "\u4f7f\u7528\u6b63\u5219\u53bb\u6389 Dom \u4e2d\u7684\u5185\u8054\u6837\u5f0f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5151\u5427"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 491, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001\u4ec0\u4e48\u662fImmutable Data\n\n- `Immutable Data`\u00a0\u5c31\u662f\u4e00\u65e6\u521b\u5efa\uff0c\u5c31\u4e0d\u80fd\u518d\u88ab\u66f4\u6539\u7684\u6570\u636e\n- \u5bf9 `Immutable`\u00a0\u5bf9\u8c61\u7684\u4efb\u4f55 `\u4fee\u6539`\u00a0\u3001 `\u6dfb\u52a0`\u00a0\u3001 `\u5220\u9664`\u00a0\u64cd\u4f5c\u90fd\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 `Immutable`\u00a0\u5bf9\u8c61\n- `Immutable`\u00a0\u5b9e\u73b0\u539f\u7406\u662f **`Persistent Data Structure`**\u00a0(**\u6301\u4e45\u5316\u6570\u636e\u7ed3\u6784**)\uff0c\u4e5f\u5c31\u662f\u4f7f\u7528\u65e7\u6570\u636e\u521b\u5efa\u65b0\u6570\u636e\u65f6\uff0c\u8981\u4fdd\u8bc1\u65e7\u6570\u636e\u540c\u65f6\u53ef\u7528\u4e14\u4e0d\u53d8\n- \u540c\u65f6\u4e3a\u4e86\u907f\u514d `deepCopy`\u00a0\u628a\u6240\u6709\u8282\u70b9\u90fd\u590d\u5236\u4e00\u904d\u5e26\u6765\u7684\u6027\u80fd\u635f\u8017\uff0c `Immutable`\u00a0\u4f7f\u7528\u4e86 **`Structural Sharing`**\u00a0(**\u7ed3\u6784\u5171\u4eab**)\uff0c\u5373\u5982\u679c\u5bf9\u8c61\u6811\u4e2d\u4e00\u4e2a\u8282\u70b9\u53d1\u751f\u53d8\u5316\uff0c\u53ea\u4fee\u6539\u8fd9\u4e2a\u8282\u70b9\u548c\u53d7\u5b83\u5f71\u54cd\u7684\u7236\u8282\u70b9\uff0c\u5176\u4ed6\u8282\u70b9\u5219\u8fdb\u884c\u5171\u4eab\n\n\u4e0b\u8fb9\u6765\u4e86\u89e3\u4e00\u4e2a\u5e93\uff0c `Immutable.js`\u00a0\u5e93\n\n### \u4e8c\u3001\u4e3a\u4ec0\u4e48\u9700\u8981immutable\n\n#### 1.\u5f15\u7528\u5e26\u6765\u7684\u526f\u4f5c\u7528\n\n`javascript`\u00a0\u4e2d\u5b58\u5728\u4e24\u7c7b\u6570\u636e\u7c7b\u578b\uff1a `\u57fa\u672c\u6570\u636e\u7c7b\u578b`\u00a0\u548c `\u5f15\u7528\u6570\u636e\u7c7b\u578b`\u00a0\u3002js\u4e2d\u7684\u5bf9\u8c61\u975e\u5e38\u7075\u6d3b\u3001\u591a\u53d8\uff0c\u5bf9\u8c61\u8d4b\u503c\u4ec5\u4ec5\u662f\u4f20\u9012\u5f15\u7528\u5730\u5740\uff0c\u6240\u4ee5\u4f1a\u5f15\u8d77\u5f88\u591a\u95ee\u9898\u3002\n\n#### 2.\u6df1\u62f7\u8d1d\u5e26\u6765\u7684\u95ee\u9898\n\n\u9488\u5bf9\u5f15\u7528\u5e26\u6765\u7684\u526f\u4f5c\u7528\uff0c\u53ef\u4ee5\u91c7\u7528\u6df1\u62f7\u8d1d\uff08deep clone\uff09\u7684\u65b9\u5f0f\u3002\u6df1\u62f7\u8d1d\u4e5f\u6709\u5176\u6027\u80fd\u95ee\u9898\uff0c\u5982\u679c\u53ea\u62f7\u8d1d\u90e8\u5206\u5c5e\u6027\uff0c\u6df1\u62f7\u8d1d\u4f1a\u5c06\u5176\u4ed6\u90e8\u5206\u4e5f\u62f7\u8d1d\uff0c\u6570\u636e\u91cf\u5927\u7684\u65f6\u5019\uff0c\u4f1a\u6709\u5f88\u9ad8\u7684\u6027\u80fd\u635f\u8017\u3002\n\n#### 3.javascript\u8bed\u8a00\u672c\u8eab\u4e0d\u8db3\n\n\u5728js\u4e2d\u5b9e\u73b0\u6570\u636e\u4e0d\u53ef\u53d8\uff0c\u6709\u4e24\u4e2a\u65b9\u6cd5\uff1a `const`\u00a0\u3001 `Object.freeze()`\u00a0\u3002\u4f46\u662f\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u662f\u6d45\u62f7\u8d1d\uff0c\u9047\u5230\u5d4c\u5957\u8fc7\u6df1\u7684\u7ed3\u6784\u5c31\u9700\u8981\u9012\u5f52\u5904\u7406\uff0c\u53c8\u4f1a\u5b58\u5728\u6027\u80fd\u4e0a\u7684\u95ee\u9898\u3002\n\n### \u4e09\u3001Immutable\u7279\u70b9\n\n#### 1.\u6301\u7eed\u5316\u6570\u636e\u7ed3\u6784\uff08Persistent data structure\uff09\n\n`\u6301\u4e45\u5316\u6570\u636e\u7ed3\u6784`\u00a0\uff1a\u6bcf\u6b21\u4fee\u6539\u6570\u636e\u4e4b\u540e\u6211\u4eec\u90fd\u4f1a\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u7248\u672c\uff0c\u4e14\u65e7\u7248\u672c\u53ef\u4ee5\u5b8c\u597d\u4fdd\u7559\u3002\n\nImmutable.js\u63d0\u4f9b\u4e867\u79cd\u4e0d\u53ef\u53d8\u7684\u6570\u636e\u7c7b\u578b\uff1a `List`\u00a0\u3001 `Map`\u00a0\u3001 `Stack`\u00a0\u3001 `OrderdMap`\u00a0\u3001 `Set`\u00a0\u3001 `OrderedSet`\u00a0\u3001 `Record`\u00a0\u3002\u5bf9Immutable\u5bf9\u8c61\u64cd\u4f5c\u5747\u4f1a\u8fd4\u56de\u65b0\u5bf9\u8c61\u3002\n\n```js\nvar obj = { count:1 };\nvar map = Immutable.fromJs(obj);//\u5c06javascript\u5bf9\u8c61\u8f6c\u6362\u4e3aImmutable\u5bf9\u8c61\nvar map2 = map.set('count',2);\n\nconsole.log(map.get('count'));\nconsole.log(map2.get('count'));\n```\n\nImmutable.js\u7528\u6811\u5b9e\u73b0\u4e86 `\u6301\u4e45\u5316\u7ed3\u6784\u6570\u636e`\u00a0\uff0c\u5148\u770b\u4e0b\u56fe\u8fd9\u68f5\u6811\n\n![tree1.png](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-491-tree1.png)\n\n\u5982\u679c\u6211\u4eec\u8981\u5728 g \u4e0b\u9762\u63d2\u5165\u4e00\u4e2a\u8282\u70b9 h ,\u5982\u4f55\u63d2\u5165\u540e\u8ba9\u539f\u6709\u7684\u6811\u4fdd\u6301\u4e0d\u53d8\uff1f\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u5c31\u662f\u91cd\u65b0\u751f\u6210\u4e00\u68f5\u6811\uff1a\n\n![tree2.png](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-491-tree2.png)\n\n\u8fd9\u6837\u505a\u663e\u7136\u5f88\u4f4e\u6548\uff0c\u6bcf\u6b21\u64cd\u4f5c\u90fd\u9700\u8981\u751f\u6210\u4e00\u9897\u5168\u65b0\u7684\u6811\uff0c\u65e2\u8d39\u65f6\u53c8\u8d39\u7a7a\u95f4\u3002\n\n#### 2.\u7ed3\u6784\u5171\u4eab\uff08structural sharing\uff09\n\n`\u7ed3\u6784\u5171\u4eab`\u00a0\u5c31\u662f\u9488\u5bf9\u4e0a\u9762\u91cd\u65b0\u751f\u6210\u4e00\u9897\u6811\u505a\u7684\u4f18\u5316\u65b9\u6848\u3002\n\nImmutableJS\u57fa\u4e8e `\u54c8\u5e0c\u6620\u5c04\u6811\uff08Hash Array Mapped Trie\uff09`\u00a0\u548c `\u77e2\u91cf\u6620\u5c04\u6811(Vector Trie)`\u00a0\uff0c\u53ea\u514b\u9686\u8be5\u526f\u672c\u4ee5\u53ca\u5b83\u7684\u7956\u5148\u7ee7\u627f\uff0c\u5176\u4ed6\u4fdd\u6301\u4e0d\u53d8\u3002\u8fd9\u6837\u53ef\u4ee5\u5171\u4eab\u76f8\u540c\u7684\u90e8\u5206\uff0c\u5927\u5927\u63d0\u9ad8\u6027\u80fd\u3002\n\n```js\nvar obj = {\n\tcount : 1,\n list : [1,2,3,4,5]\n}\nvar map1 = Immutable.fromJS(obj);\nvar map2 = map1.set('count',2);\nconsole.log(map1.list === map2.list);//true\n```\n\n\u7ed3\u6784\u5171\u4eab\u8fc7\u7a0b\uff1a\n\n![immutable.gif](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-tree3.gif)\n\n#### 3.\u652f\u6301\u60f0\u6027\u64cd\u4f5c\n\n\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u8bed\u8a00\u4e2d\uff0c\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u7ed3\u6784Seq(\u5168\u79f0Sequence)\uff0c\u5176\u4ed6Immutable\u5bf9\u8c61\u53ef\u4ee5\u901a\u8fc7 `toSeq()`\u00a0\u8fdb\u884c\u8f6c\u6362\u3002Seq\u6709\u4ee5\u4e0b\u7279\u6027\n\n- **Seq\u662f\u4e0d\u53ef\u53d8\u7684** -- \u4e00\u65e6\u521b\u5efaSeq\uff0c\u5b83\u5c31\u4e0d\u80fd\u88ab\u66f4\u6539\u3001\u9644\u52a0\u5230\u3001\u91cd\u65b0\u6392\u5217\u6216\u4ee5\u5176\u4ed6\u65b9\u5f0f\u4fee\u6539\u3002\n- **Seq\u662f\u61d2\u60f0\u7684** -- Seq\u53ea\u9700\u8981\u505a\u5f88\u5c11\u7684\u5de5\u4f5c\u6765\u54cd\u5e94\u4efb\u4f55\u65b9\u6cd5\u8c03\u7528\u3002\u503c\u5728\u8fed\u4ee3\u8fc7\u7a0b\u4e2d\u521b\u5efa\uff0c\u5305\u62ec\u5728\u51cf\u5c11\u6216\u8f6c\u6362\u4e3a\u5177\u4f53\u6570\u636e\u7ed3\u6784\u65f6\u7684\u9690\u6c0f\u8fed\u4ee3\u3002\n\n\u4ee5\u4e0b\u5185\u5bb9\u4e0d\u4f1a\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\uff0c\u56e0\u4e3a\u751f\u6210Seq\u503c\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u8fed\u4ee3\n\n```js\nvar oddSquares = Immutable.Seq.of(1, 2, 3, 4, 5, 6, 7, 8)\n .filter(x => {\n log(x % 2);\n return x % 2;\n }).map(x => {\n log(x * x);\n return x * x;\n });\n```\n\n\u4e00\u65e6Seq\u88ab\u4f7f\u7528\uff0c\u5b83\u6267\u884c\u5fc5\u8981\u7684\u5de5\u4f5c\u3002\u4ee5\u4e0b\u4ee3\u7801\uff0c\u53ef\u4ee5\u901a\u8fc7console.log\u53ef\u4ee5\u770b\u51fa\uff0c\u6ca1\u6709\u521b\u5efa\u4efb\u4f55\u6570\u636e\u7ed3\u6784\uff0c\u8fc7\u6ee4\u5668\u53ea\u88ab\u8c03\u7528\u4e863\u6b21\uff0cmap\u503c\u88ab\u8c03\u7528\u4e86\u4e00\u6b21\n\n```javascript\nconsole.log(oddSquares.get(1));\n/*\n1\n0\n1\n9\n9\n*/\n```\n\nSeq\u901a\u5e38\u7528\u4e8e\u4e3aJavaScript Object\u63d0\u4f9b\u4e30\u5bcc\u7684\u96c6\u5408API\u3002\u5177\u4f53\u89c1Seq\u6587\u6863\u3002\n\n#### 4.\u5f3a\u5927\u7684API\u673a\u5236\n\nImmutableJS\uff0c\u63d0\u4f9b\u66f4\u9ad8\u7684\u65b9\u6cd5\uff0c\u6709\u4e9b\u65b9\u6cd5\u6cbf\u7528\u539f\u751fjs\u7684\u7c7b\u4f3c\uff0c\u964d\u4f4e\u5b66\u4e60\u6210\u672c\uff0c\u6709\u4e9b\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4fbf\u6377\u64cd\u4f5c\uff0c\u4f8b\u5982`setIn`\uff0c`UpdateIn`\u53ef\u4ee5\u8fdb\u884c\u6df1\u5ea6\u64cd\u4f5c\u3002\n\n```js\nvar obj = {\n a: {\n b: {\n list: [1, 2, 3]\n }\n }\n};\nvar map = Immutable.fromJS(obj);\nvar map2 = Immutable.updateIn(['a', 'b', 'list'], (list) => {\n return list.push(4);\n});\n\nconsole.log(map2.getIn(['a', 'b', 'list']))\n// List [ 1, 2, 3, 4 ]\n\n```\n\n### \u56db\u3001Immutable\u6838\u5fc3API\n\n#### 1.\u539f\u751fjs\u8f6c\u6362\u4e3aimmutableData\n\n```js\nImmutable.fromJS([1,2]) // immutable\u7684 list\nImmutable.fromJS({a: 1}) // immutable\u7684 map\n```\n\n#### 2.\u4eceimmutableData \u56de\u5230 JavaScript \u5bf9\u8c61\n\n```js\nimmutableData.toJS()\n```\n\n#### 3.\u5224\u65ad\u4e24\u4e2aimmutable\u6570\u636e\u662f\u5426\u4e00\u81f4\n\n```js\nImmutable.is(immutableA, immutableB)\n```\n\n#### 4.\u5224\u65ad\u662f\u4e0d\u662fmap\u6216List\n\n```js\nImmutable.Map.isMap(x)\nImmutable.Map.isList(x)\n\n```\n\n#### 5.\u5bf9\u8c61\u5408\u5e76(\u6ce8\u610f\u662f\u540c\u4e2a\u7c7b\u578b)\n\n```js\nimmutableMaB = immutableMapA.merge(immutableMaC)\n```\n\n#### 6.Map\u7684\u589e\u5220\u67e5\u6539\u67e5\n\n```js\nimmutableData.get('a') // {a:1} \u5f97\u52301\u3002\nimmutableData.getIn(['a', 'b']) // {a:{b:2}} \u5f97\u52302\u3002\u8bbf\u95ee\u6df1\u5c42\u6b21\u7684key\n\n// \u589e\u548c\u6539(\u6ce8\u610f\u4e0d\u4f1a\u6539\u53d8\u539f\u6765\u7684\u503c\uff0c\u8fd4\u56de\u65b0\u7684\u503c)\nimmutableData.set('a', 2) // {a:1} \u5f97\u52301\u3002\nimmutableData.setIn(['a', 'b'], 3)\nimmutableData.update('a',function(x){return x+1})\nimmutableData.updateIn(['a', 'b'],function(x){return x+1})\n\n// \u5220\nimmutableData.delete('a')\nimmutableData.deleteIn(['a', 'b'])\n```\n\n#### 7.List\u7684\u589e\u5220\u67e5\u6539\n\n\u5982\u540cMap\uff0c\u4e0d\u8fc7\u53c2\u6570\u53d8\u4e3a\u6570\u5b57\u7d22\u5f15\u3002\n\u6bd4\u5982immutableList.set(1, 2)\n\n#### 8.\u5176\u5b83\u4fbf\u6377\u51fd\u6570\n\n\u5982\u540cunderscore\u7684\u65b9\u6cd5\uff0c\u5177\u4f53\u8bf7\u53c2\u7167\u6587\u6863\u3002\n\n### \u4e94\u3001Immutable\u4f18\u70b9\n\n#### 1.Immutable\u964d\u4f4e\u4e86Mutable\u5e26\u6765\u7684\u590d\u6742\u5ea6\n\n\u53ef\u53d8\u6570\u636e\u8026\u5408\u4e86Time\u548cValue\u7684\u6982\u5ff5\uff0c\u9020\u6210\u4e86\u6570\u636e\u5f88\u96be\u88ab\u56de\u6eaf\u3002\n\n```js\nfunction touchAndLog(touchFn) {\n let data = { key: 'value' };\n touchFn(data);\n console.log(data.key); // \u731c\u731c\u4f1a\u6253\u5370\u4ec0\u4e48\uff1f\n}\n```\n\n\u5728\u4e0d\u67e5\u770b `touchFn` \u7684\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u56e0\u4e3a\u4e0d\u786e\u5b9a\u5b83\u5bf9 `data` \u505a\u4e86\u4ec0\u4e48\uff0c\u4f60\u662f\u4e0d\u53ef\u80fd\u77e5\u9053\u4f1a\u6253\u5370\u4ec0\u4e48\uff08\u8fd9\u4e0d\u662f\u5e9f\u8bdd\u5417\uff09\u3002\u4f46\u5982\u679c `data` \u662f Immutable \u7684\u5462\uff0c\u4f60\u53ef\u4ee5\u5f88\u80af\u5b9a\u7684\u77e5\u9053\u6253\u5370\u7684\u662f `value`\u3002\n\n#### 2.\u8282\u7701\u5185\u5b58\n\n`ImmutableJS`\u00a0\u4f7f\u7528 `Structure Sharing`\u00a0\u4f1a\u5c3d\u91cf\u590d\u7528\u5185\u5b58\u3002\u6ca1\u6709\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u4f1a\u88ab\u5783\u573e\u56de\u6536\u3002\n\n```js\nimport { Map} from 'immutable';\nlet a = Map({\n select: 'users',\n filter: Map({ name: 'Cam' })\n})\nlet b = a.set('select', 'people');\na === b; // false\na.get('filter') === b.get('filter'); // true\n\n```\n\n\u4e0a\u9762a\u548cb\u5171\u4eab\u4e86\u6ca1\u6709\u53d8\u5316\u7684 `filter`\u00a0\u8282\u70b9\u3002\n\n#### 3.Undo/Redo\uff0cCopy/Paste\uff0c\u751a\u81f3\u65f6\u95f4\u65c5\u884c\u8fd9\u4e9b\u529f\u80fd\u505a\u8d77\u6765\u4e5f\u662f\u5c0f\u83dc\u4e00\u789f\n\n\u56e0\u4e3a\u6bcf\u6b21\u6570\u636e\u90fd\u662f\u4e0d\u4e00\u6837\u7684\uff0c \u53ea\u8981\u628a\u8fd9\u4e9b\u6570\u636e\u653e\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u5b58\u50a8\u8d77\u6765\uff0c\u60f3\u56de\u9000\u5230\u54ea\u91cc\u5c31\u62ff\u51fa\u5bf9\u5e94\u7684\u6570\u636e\u5373\u53ef\uff0c\u5f88\u5bb9\u6613\u5f00\u53d1\u51fa\u64a4\u9500\u91cd\u505a\u7684\u8fd9\u79cd\u529f\u80fd\u3002\n\n#### 4.\u5e76\u53d1\u5b89\u5168\n\n\u4f20\u7edf\u7684\u5e76\u53d1\u975e\u5e38\u96be\u505a\uff0c\u56e0\u4e3a\u8981\u5904\u7406\u5404\u79cd\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\uff0c\u53d1\u660e\u4e86\u5404\u79cd\u9501\u6765\u89e3\u51b3\u95ee\u9898\u3002\u4f46\u662f\u4f7f\u7528\u4e86Immutable\u4e4b\u540e\uff0c\u6570\u636e\u5929\u751f\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u5e76\u53d1\u9501\u4e5f\u4e0d\u9700\u8981\u4e86\u3002\n\u5bf9\u4e8ejavascript\u6ca1\u6709\u4ec0\u4e48\u7279\u6b8a\u610f\u4e49\uff0c\u56e0\u4e3ajavascript\u662f\u5355\u7ebf\u7a0b\u8fd0\u884c\uff0c\u672a\u6765\u53ef\u80fd\u4f1a\u52a0\u5165\uff0c\u63d0\u524d\u89e3\u51b3\u95ee\u9898\u4e86\u3002\n\n#### 5.\u62e5\u62b1\u51fd\u6570\u5f0f\u7f16\u7a0b\n\nImmutable\u672c\u8eab\u5c31\u662f\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u6982\u5ff5\uff0c\u7eaf\u51fd\u6570\u7f16\u7a0b\u6bd4\u9762\u5411\u5bf9\u8c61\u66f4\u4f7f\u7528\u4e8e\u524d\u7aef\u5f00\u53d1\u3002\u56e0\u4e3a\u53ea\u8981\u8f93\u5165\u4e00\u81f4\uff0c\u8f93\u51fa\u5fc5\u7136\u4e00\u81f4\uff0c\u8fd9\u6837\u5f00\u53d1\u7684\u7ec4\u4ef6\u66f4\u6613\u4e8e\u8c03\u8bd5\u548c\u7ec4\u88c5\u3002\n\u50cf ClojureScript\uff0cElm \u7b49\u51fd\u6570\u5f0f\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u5929\u751f\u90fd\u662f Immutable \u7684\uff0c\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48 ClojureScript \u57fa\u4e8e React \u7684\u6846\u67b6 --- Om \u6027\u80fd\u6bd4 React \u8fd8\u8981\u597d\u7684\u539f\u56e0\u3002\n\n### \u516d\u3001Immutable\u7684\u7f3a\u70b9\n\n#### 1.\u9700\u8981\u5b66\u4e60\u65b0\u7684API\n\n#### 2.\u589e\u52a0\u4e86\u8d44\u6e90\u6587\u4ef6\u7684\u5927\u5c0f\n\n#### 3.\u5bb9\u6613\u548c\u539f\u751f\u5bf9\u8c61\u6df7\u6dc6\n\n\u8fd9\u70b9\u662f\u6211\u4eec\u4f7f\u7528 `Immutable.js`\u00a0\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u6700\u5927\u95ee\u9898\u3002\u5199\u4ee3\u7801\u8981\u505a\u601d\u7ef4\u4e0a\u7684\u8f6c\u53d8\u3002\n\n\u867d\u7136 `Immutable.js`\u00a0\u5c3d\u91cf\u5c1d\u8bd5\u628aAPI\u8bbe\u8ba1\u7684\u539f\u751f\u5bf9\u8c61\u7c7b\u4f3c\uff0c\u6709\u7684\u65f6\u5019\u8fd8\u662f\u5f88\u96be\u533a\u522b\u5230\u5e95\u662f `Immutable`\u00a0\u5bf9\u8c61\u8fd8\u662f\u539f\u751f\u5bf9\u8c61\uff0c\u5f88\u5bb9\u6613\u6df7\u6dc6\u3002\n\n`Immutable`\u00a0\u4e2d\u7684 `Map`\u00a0\u548c `List`\u00a0\u867d\u5bf9\u5e94\u539f\u751f `Object`\u00a0\u548c `Array`\u00a0\uff0c\u4f46\u64cd\u4f5c\u975e\u5e38\u4e0d\u540c\uff0c\u6bd4\u5982\u4f60\u8981\u7528 `map.get('key')`\u00a0\u800c\u4e0d\u662f `map.key`\u00a0\uff0c `array.get(0)`\u00a0\u800c\u4e0d\u662f `array[0]`\u00a0\u3002\u53e6\u5916 `Immutable`\u00a0\u6bcf\u6b21\u4fee\u6539\u4e22\u4f1a\u8fd4\u56de\u65b0\u5bf9\u8c61\uff0c\u5f88\u5bb9\u6613\u5c31\u5fd8\u8bb0\u8d4b\u503c\u3002\n\n\u4f7f\u7528\u5916\u90e8\u5e93\u7684\u65f6\u5019\uff0c\u4e00\u822c\u4e5f\u9700\u8981\u4f7f\u7528\u539f\u751f\u7684\u5bf9\u8c61\uff0c\u4e5f\u5f88\u5bb9\u6613\u5fd8\u8bb0\u8f6c\u6362\u3002\n\u4e00\u822c\u4f7f\u7528\u4ee5\u4e0b\u529e\u6cd5\u6765\u907f\u514d\u7c7b\u4f3c\u7684\u95ee\u9898\u53d1\u751f\uff1a\n\n- \u4f7f\u7528Flow\u6216TypeScript\u8fd9\u7c7b\u6709\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u7684\u5de5\u5177\n- \u7ea6\u5b9a\u53d8\u91cf\u547d\u540d\u89c4\u5219\uff1a\u5982\u6240\u6709 `Immutable`\u00a0\u7c7b\u578b\u5bf9\u8c61\u90fd\u4ee5 `$$`\u00a0\u5f00\u5934\n- \u4f7f\u7528 `Immutable.fromJS`\u00a0\u800c\u4e0d\u662f `Immutable.Map`\u00a0\u6216 `Immutable.List`\u00a0\u6765\u521b\u5efa\u5bf9\u8c61\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d `Immutable`\u00a0\u548c\u539f\u751f\u7684\u5bf9\u8c61\u4e4b\u95f4\u7684\u6df7\u7528\u3002\n\n### \u4e03\u3001\u5b9e\u8df5\n\n#### 1.\u5728React\u4e2d\u5e94\u7528\n\nReact\u662f\u4e00\u4e2a `UI = f(state)`\u00a0\u5e93\uff0c\u4e3a\u4e86\u89e3\u51b3\u6027\u80fd\u95ee\u9898\uff0c\u865a\u62dfdom\u901a\u8fc7diff\u7b97\u6cd5\u4fee\u6539DOM\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684DOM\u66f4\u65b0\u3002\n\n\u9047\u5230\u4e2a\u95ee\u9898\uff1a\u5f53\u6267\u884csetState\u65f6\uff0c\u5373\u4f7f\u72b6\u6001\u6570\u636e\u6ca1\u6709\u53d1\u751f\u53d8\u5316\uff0c\u4e5f\u4f1a\u53bb\u505avirtual dom\u7684diff\uff0c\u56e0\u4e3a\u5728React\u7684\u58f0\u660e\u5468\u671f\u4e2d\uff0c\u591a\u6570\u60c5\u51b5\u4e0b `shouldComponentUpdate`\u00a0\u603b\u662f\u8fd4\u56detrue\u3002\u5728 `shouldComponentUpdate`\u00a0\u8fdb\u884c\u72b6\u6001\u6bd4\u8f83\uff1f\n\nReact\u7684\u89e3\u51b3\u529e\u6cd5\uff1a\u63d0\u4f9b\u4e86\u4e00\u4e2a `PureRenderMixin`\u00a0\u3001 `PureRenderMixin`\u00a0\u5bf9 `shouldComponentUpdate`\u00a0\u65b9\u6cd5\u8fdb\u884c\u4e86\u8986\u76d6\uff0c\u4f46\u662f `PureRenderMixin`\u00a0\u91cc\u9762\u662f\u94b1\u6bd4\u8f83\uff1a\n\n```js\nvar ReactComponentWithPureRenderMixin = {\n shouldComponentUpdate: function(nextProps, nextState) {\n return shallowCompare(this, nextProps, nextState);\n },\n};\n\nfunction shallowCompare(instance, nextProps, nextState) {\n return (\n !shallowEqual(instance.props, nextProps) ||\n !shallowEqual(instance.state, nextState)\n );\n}\n```\n\n\u6d45\u6bd4\u8f83\u53ea\u80fd\u8fdb\u884c\u7b80\u5355\u6bd4\u8f83\uff0c\u5982\u679c\u6570\u636e\u7ed3\u6784\u590d\u6742\u7684\u8bdd\uff0c\u4f9d\u7136\u4f1a\u5b58\u5728\u591a\u4f59\u7684diff\u8fc7\u7a0b\uff0c\u8bf4\u660e `PureRenderMixin`\u00a0\u4f9d\u7136\u4e0d\u662f\u7406\u60f3\u7684\u89e3\u51b3\u65b9\u6848\u3002\n\nImmutable\u6765\u89e3\u51b3\uff1a\u56e0\u4e3aimmutable\u7684 `\u4e0d\u53ef\u53d8\u6027`\u00a0&& `\u7ed3\u6784\u5171\u4eab`\u00a0\uff0c\u80fd\u591f\u5feb\u901f\u8fdb\u884c\u6570\u636e\u6bd4\u8f83\uff1a\n\n```js\nshouldComponentUpdate: function(nextProps, nextState) {\n return deepCompare(this, nextProps, nextState);\n},\n \nfunction deepCompare(instance, nextProps, nextState) {\n\treturn !Immutable.is(instance.props, nextProps) || \n\t\t!Immutable.is(instance.state, nextState);\n}\n```\n\n**setState\u6280\u5de7**\n\nReact\u5efa\u8bae\u628a `this.state`\u00a0\u5f53\u4f5c `Immutbale`\u00a0\u7684\uff0c\u56e0\u4e3a\u4fee\u6539\u524d\u9700\u8981\u505a\u4e00\u4e2adeepCopy\uff0c\u663e\u5f97\u5f88\u9ebb\u70e6\n\n```js\nimport '_' from 'lodash';\n\nconst Component = React.createClass({\n getInitialState() {\n return {\n data: { times: 0 }\n }\n },\n handleAdd() {\n let data = _.cloneDeep(this.state.data);\n data.times = data.times + 1;\n this.setState({ data: data });\n // \u5982\u679c\u4e0a\u9762\u4e0d\u505a cloneDeep\uff0c\u4e0b\u9762\u6253\u5370\u7684\u7ed3\u679c\u4f1a\u662f\u5df2\u7ecf\u52a0 1 \u540e\u7684\u503c\u3002\n console.log(this.state.data.times);\n }\n}\n```\n\n\u4f7f\u7528Immutable\u540e\uff1a\n\n```javascript\ngetInitialState() {\n return {\n data: Map({ times: 0 })\n }\n },\n handleAdd() {\n this.setState(({data}) => ({\n data: data.update('times', v => v + 1) })\n });\n }\n```\n\n#### 2.\u4e0eFlux\u642d\u914d\u4f7f\u7528\n\n\u7531\u4e8eFlux\u6ca1\u6709\u9650\u5b9aStore\u4e2d\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f7f\u7528 `Immutable`\u00a0\u975e\u5e38\u7b80\u5355\u3002\n\u73b0\u5728\u5b9e\u73b0\u4e00\u4e2a\u7c7b\u4f3c\u5e26\u6709\u6dfb\u52a0\u548c\u64a4\u9500\u529f\u80fd\u7684Store\uff1a\n\n```javascript\nimport { Map, OrderedMap } from 'immutable';\nlet todos = OrderedMap();\nlet history = []; // \u666e\u901a\u6570\u7ec4\uff0c\u5b58\u653e\u6bcf\u6b21\u64cd\u4f5c\u540e\u4ea7\u751f\u7684\u6570\u636e\n\nlet TodoStore = createStore({\n getAll() { return todos; }\n});\n\nDispatcher.register(action => {\n if (action.actionType === 'create') {\n let id = createGUID();\n history.push(todos); // \u8bb0\u5f55\u5f53\u524d\u64cd\u4f5c\u524d\u7684\u6570\u636e\uff0c\u4fbf\u4e8e\u64a4\u9500\n todos = todos.set(id, Map({\n id: id,\n complete: false,\n text: action.text.trim()\n }));\n TodoStore.emitChange();\n } else if (action.actionType === 'undo') {\n // \u8fd9\u91cc\u662f\u64a4\u9500\u529f\u80fd\u5b9e\u73b0\uff0c\n // \u53ea\u9700\u4ece history \u6570\u7ec4\u4e2d\u53d6\u524d\u4e00\u6b21 todos \u5373\u53ef\n if (history.length > 0) {\n todos = history.pop();\n }\n TodoStore.emitChange();\n }\n});\n```\n\n#### 3.\u4e0eRedux\u642d\u914d\u4f7f\u7528\n\n\u7531\u4e8eRedux\u4e2d\u5185\u7f6e\u7684 `combineReducers`\u00a0\u548creducer\u79cd\u7684 `initialState`\u00a0\u90fd\u4e3a\u539f\u751f\u7684Object\u5bf9\u8c61\uff0c\u6240\u4ee5\u4e0d\u80fd\u548cImmutable\u539f\u751f\u642d\u914d\u4f7f\u7528\u3002\n\n\u5e78\u8fd0\u7684\u662f\uff0cRedux\u4e0d\u6392\u65a5\u4f7f\u7528 `Immutable`\u00a0\uff0c\u53ef\u4ee5\u81ea\u5df1\u91cd\u5199 `combineReducers`\u00a0\u6216\u4f7f\u7528 `Redux-immutablejs`\u00a0\u6765\u63d0\u4f9b\u652f\u6301\n\n### \u516b\u3001\u603b\u7ed3\n\nImmutable\u53ef\u4ee5\u7ed9\u5e94\u7528\u5e26\u6765\u6781\u5927\u7684\u6027\u80fd\u63d0\u5347\uff0c\u4f46\u662f\u5426\u4f7f\u7528\u8fd8\u662f\u8981\u770b\u9879\u76ee\u60c5\u51b5\u3002\u7531\u4e8e\u4fb5\u5165\u6027\u8f83\u5f3a\uff0c\u65b0\u9879\u76ee\u5f15\u5165\u6bd4\u8f83\u5bb9\u6613\uff0c\u8001\u9879\u76ee\u8fc1\u79fb\u9700\u8981\u8bc4\u4f30\u8fc1\u79fb\u3002\u5bf9\u4e8e\u4e00\u4e9b\u63d0\u4f9b\u7ed9\u5916\u90e8\u4f7f\u7528\u7684\u516c\u5171\u7ec4\u4ef6\uff0c\u6700\u597d\u4e0d\u8981\u628aImmutable\u5bf9\u8c61\u4e4b\u95f4\u66b4\u9732\u5728\u5bf9\u5916\u63a5\u53e3\u4e2d\u3002\n\n### \u4e5d\u3001Immutable\u5b9e\u73b0\u673a\u5236\n\n#### 1.\u7b80\u5355\u4f8b\u5b50\n\n\u7814\u7a76\u524d\u5148\u770b\u4e2a\u4f8b\u5b50\uff1a\n\n```js\nlet map1 = Immutable.Map({});\n\nfor (let i = 0; i < 800; i++) {\n map1 = map1.set(Math.random(), Math.random());\n}\n\nconsole.log(map1);\n```\n\n\u8fd9\u6bb5\u4ee3\u7801\u5f80map\u91cc\u5199\u5165\u4e86800\u4e2a\u968f\u673a\u751f\u6210\u7684key\u548cvalue\u3002\u770b\u63a7\u5236\u53f0\u8f93\u51fa\n![immutable_console.png](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-491-out1.png)\n\n- \u8fd9\u662f\u4e00\u4e2a\u6811\u7ed3\u6784\uff0c\u5b50\u8282\u70b9\u4ee5\u6570\u7ec4\u7684\u5f62\u5f0f\u653e\u5728nodes\u5c5e\u6027\u4e2d\uff0cnodes\u7684\u6700\u5927\u957f\u5ea6\u662f32\u4e2a\n- bitmap\u6d89\u53ca\u5230\u5bf9\u6811\u5bbd\u7684\u538b\u7f29\uff08\u540e\u9762\u8ba8\u8bba\uff09\n\n\u5176\u4e2d\u4e00\u4e2a\u8282\u70b9\u5c55\u5f00\u540e\n\n![immutable_console_2.png](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-491-out2.png)\n\n\u8fd9\u4e2aValueNode\u5b58\u7684\u5c31\u662f\u4e00\u7ec4\u503c\uff0centry[0]\u662fkey\uff0centry[1]\u662fvalue\n\n#### 2.\u539f\u7406\n\n`Immutable`\u00a0\u7684\u57fa\u672c\u539f\u7406\u4e3b\u8981\u662f\u6301\u4e45\u5316\u6570\u636e\u7ed3\u6784\u548c\u7ed3\u6784\u5171\u4eab,\u8fd9\u91cc\u4e3b\u8981\u8ba8\u8bba\u8fd9\u4e24\u90e8\u5206\u7684\u539f\u7406\u5b9e\u73b0\u3002\n\n\n**1\uff09Vector Trie**\n\n**1.1 Vector Trie\u6570\u636e\u7ed3\u6784**\n\n\u5728\u5b9e\u73b0\u6301\u7eed\u5316\u6570\u636e\u7ed3\u6784\u65f6\uff0cImmutable.js\u53c2\u8003\u4e86 Vector Trie\u8fd9\u79cd\u6570\u636e\u7ed3\u6784(\u8fd9\u662fClojure\u91cc\u4f7f\u7528\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0cImmutable.js\u91cc\u7684\u76f8\u5173\u5b9e\u73b0\u4e0e\u5176\u975e\u5e38\u76f8\u4f3c)\u3002\u5148\u4e86\u89e3\u5176\u57fa\u672c\u7ed3\u6784\n\n\u4f8b\u5982\u6211\u4eec\u6709\u4e00\u4e2amap\uff0ckey\u5168\u90e8\u662f\u6570\u5b57\n\n```js\n{\n 0:'banana',\n 1:'prape',\n 2:'lemon',\n 3:'orange',\n 4:'apple'\n}\n```\n\n\u4e3a\u4e86\u9020\u4e00\u68f5Vector Trie\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u628akey\u8f6c\u4e3a\u4e3a\u4e8c\u8fdb\u5236\u5f62\u5f0f\uff1a\n\n```js\n{\n '000':'banana',\n '001':'prape',\n '010':'lemon',\n '011':'orange',\n '100':'apple'\n}\n```\n\n\u5efa\u56fe\u5982\u4e0b:\n\n![vectorTrie1.png](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-491-3.png)\n\n\u53ef\u4ee5\u770b\u5230\uff0cVector Trie\u7684\u6bcf\u4e2a\u8282\u70b9\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u67090\u548c1\u4e24\u4e2a\u6570\uff0c\u8868\u793a\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6570\uff0c\u6240\u6709\u503c\u90fd\u5b58\u5728\u5728\u53f6\u5b50\u8282\u70b9\u4e0a\u3002\u6bd4\u5982\u6211\u4eec\u8981\u627e001\u7684\u503c\u65f6\uff0c\u53ea\u9700\u8981\u987a\u7740 0 0 1\u627e\u4e0b\u6765\u5373\u53ef\u5f97\u5230prape\u3002\n\n\u60f3\u8981\u5b9e\u73b0\u6301\u4e45\u5316\u6570\u636e\u7ed3\u6784\u4e5f\u4e0d\u96be\uff0c\u6dfb\u52a0\u4e00\u4e2a5\uff1a'waterlemon'\n\n![vectorTrie2.png](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-491-4.png)\n\n\n- \u53ef\u89c1\u5bf9\u4e8e\u4e00\u4e2akey\u5168\u662f\u6570\u5b57\u7684map\uff0c\u6211\u4eec\u5b8c\u5168\u53ef\u4ee5\u901a\u8fc7\u4e00\u9897Vector Trie\u6765\u5b9e\u73b0\u5b83\uff0c\u540c\u65f6\u5b9e\u73b0\u6301\u7eed\u5316\u6570\u636e\u7ed3\u6784\u3002\n- \u5982\u679ckey\u4e0d\u662f\u6570\u5b57\u7684\u8bdd\uff0c\u7528\u4e00\u5957\u6620\u5c04\u673a\u5236\u628a\u5b83\u8f6c\u6210\u6570\u5b57\u5c31\u884c\u3002Immutable.js\u5b9e\u73b0\u4e00\u4e2ahash\u51fd\u6570\uff0c\u53ef\u4ee5\u5427\u4e00\u4e2a\u503c\u8f6c\u6362\u4e3a\u4e00\u4e2a\u76f8\u5e94\u7684\u6570\u5b57\u3002\n- \u8fd9\u91cc\u4e3a\u4e86\u7b80\u5316\u6bcf\u4e2a\u8282\u70b9\u6570\u7ec4\u957f\u5ea6\u4ec5\u4e3a2\uff0c\u8fd9\u6837\u6570\u636e\u91cf\u5927\u7684\u65f6\u5019\uff0c\u6811\u4f1a\u53d8\u5f97\u5f88\u6df1\uff0c\u67e5\u8be2\u5f88\u8017\u65f6\uff0c\u6240\u4ee5\u53ef\u4ee5\u6269\u5927\u6570\u7ec4\u957f\u5ea6\uff0cImmutable\u9009\u62e9\u4e8632.\n### 2.2\u6570\u5b57\u5206\u533a\uff08Digit partitioning\uff09\n\u6570\u5b57\u5206\u533a \u6307\u6211\u4eec\u628a\u4e00\u4e2akey\u4f5c\u4e3a\u6570\u5b57\u5bf9\u5e94\u5230\u4e00\u9897\u524d\u7f00\u6811\u4e0a\uff0c\u6b63\u5982\u4e0a\u8282\u6240\u8bb2\u3002\n\u6709\u4e2akey\u4e3a 9128 \uff0c\u4ee57\u4f4d\u57fa\u6570\uff0c\u5373\u6570\u7ec4\u957f\u5ea6\u4e3a7\uff0c\u5b83\u5728Vector Trie\u91cc\u8fd9\u4e48\u8868\u793a\uff1a\n\n![base7.png](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-491-5.png)\n\n\u672c\u8d28\u5c31\u662f\u5c069128\u8f6c\u5316\u4e3a7\u8fdb\u5236\u768435420\uff0c\u65e0\u987b\u9884\u5148\u8f6c\u6362\u597d\uff0c\u7c7b\u4f3c\u7684\u64cd\u4f5c\u53ef\u4ee5\u6bcf\u4e00\u5c42\u4e0a\u4f9d\u6b21\u6267\u884c\u3002\n\n\u8fdb\u5236\u8f6c\u6362\u516c\u5f0f\n> key / radix\u00a0% radix\n\n- \u4e3a\u4e86\u7b80\u4fbf\uff0c/\u8868\u793a\u5411\u4e0b\u53d6\u6574\n\n- radix\uff1a\u6bcf\u5c42\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u5373\u8f6c\u6362\u4e3a\u51e0\u8fdb\u5236\n- level\uff1a\u5f53\u524d\u5c42\u6570\uff0c\u7b2c\u51e0\u4f4d\u6570\n\n\u4ee3\u7801\u5b9e\u73b0\u5982\u4e0b\n\n```js\nconst RADIX = 7;\n\nfunction find(key) {\n let node = root; // root\u662f\u6839\u8282\u70b9\uff0c\u5728\u522b\u7684\u5730\u65b9\u5b9a\u4e49\u4e86\n\n // depth\u662f\u5f53\u524d\u6811\u7684\u6df1\u5ea6\u3002\u8fd9\u79cd\u8ba1\u7b97\u65b9\u5f0f\u8ddf\u4e0a\u9762\u5217\u51fa\u7684\u5f0f\u5b50\u662f\u7b49\u4ef7\u7684\uff0c\u4f46\u53ef\u4ee5\u907f\u514d\u591a\u6b21\u6307\u6570\u8ba1\u7b97\u3002\u8fd9\u4e2asize\u5c31\u662f\u4e0a\u9762\u7684radix^level - 1\n for (let size = Math.pow(RADIX, (depth - 1)); size > 1; size /= RADIX) {\n node = node[Math.floor(key / size) % RADIX];\n }\n\n return node[key % RADIX];\n}\n```\n\n**2.3\u4f4d\u5206\u533a\uff08Bit Partitioning\uff09**\n\n\u4f4d\u5206\u533a\u662f\u5bf9\u6570\u5b57\u5206\u533a\u7684\u4f18\u5316\uff0c\u6570\u5b57\u5206\u533a\u65b9\u6cd5\u6709\u4e9b\u8017\u65f6\uff0c\u6bcf\u4e00\u5c42\u90fd\u8981\u8fdb\u884c\u4e24\u6b21\u9664\u6cd5\u4e00\u6b21\u53d6\u6a21\uff0c\u5e76\u4e0d\u9ad8\u6548\u3002\n\n\u4f4d\u5206\u533a\u662f\u5efa\u7acb\u5728\u6570\u5b57\u5206\u533a\u7684\u57fa\u7840\u4e0a\u7684\uff0c\u6240\u6709\u4ee52\u7684\u6574\u6570\u6b21\u5e42\u4e3a\u57fa\u6570\u7684\u6570\u5b57\u5206\u533a\u524d\u7f00\u6811\uff0c\u90fd\u53ef\u4ee5\u8f6c\u5316\u4e3a\u4f4d\u5206\u533a\u3002\u57fa\u4e8e\u4e00\u4e9b\u4f4d\u8fd0\u7b97\uff0c\u907f\u514d\u4e00\u4e9b\u8017\u65f6\u7684\u8ba1\u7b97\u3002\n\n\u4f4d\u5206\u533a\u7684\u672c\u8d28\u5c31\u662f\uff1a\u5c06Vector Trie\u6bcf\u5c42\u7684\u6570\u5b57\u8f6c\u5316\u4e3abit\uff08\u4f4d\u8868\u793a\uff09\uff0c\u8282\u7701\u8ba1\u7b97\u65f6\u95f4\u3002\n\n\u6570\u5b57\u5206\u533a\u5c06key\u62c6\u5206\u4e3a\u4e00\u4e2a\u4e2a\u6570\u5b57\uff0c\u800c\u4f4d\u5206\u533a\u628akey\u5206\u6210\u4e00\u7ec4\u7ec4bit\u3002\n\n**\u4e3e\u4e2a\u4f8b\u5b50\uff1a**\n\n\u4ee532\u8def\u7684\u524d\u7f00\u6811\u4e3a\u4f8b\uff0c\u6570\u5b57\u5206\u533a\u7684\u65b9\u6cd5\u662f\u628akey\u4e3a32\u7684\u57fa\u6570\u62c6\u5206\uff0c\u800c\u4f4d\u5206\u533a\u662f\u628a\u5b83\u4ee55\u4e2abits\u62c6\u5206\uff0c\u56e0\u4e3a32 = 2\n\n\u90a3\u6211\u4eec\u53ef\u4ee5\u628a32\u8fdb\u5236\u6570\u7684\u6bcf\u4e00\u4f4d\u770b\u505a5\u4e2a\u4e8c\u8fdb\u5236\u4f4d\u3002\n\n\u524d\u9762\u7684\u516c\u5f0f\n\n> key / 2\u00a0% 2\n\n\u6839\u636e\u76f8\u5173\u4f4d\u8fd0\u7b97\u76f8\u5173\u7684\u77e5\u8bc6\uff1a\n\n`a / 2 === a >>> n\u00a0`\u3001`a % 2 === a & (2 - 1)\u00a0`\n\n\u8fd9\u6837\u5c31\u53ef\u4ee5\u901a\u8fc7\u4f4d\u8fd0\u7b97\u5f97\u51fa\u8be5\u5f0f\u5b50\u7684\u503c\n\n\u4ee3\u7801\u5b9e\u73b0\u5982\u4e0b\n\n```js\nconst BITS = 5;\nconst WIDTH = 1 << BITS, // 25 = 32\nconst MASK = WIDTH - 1; // 31\uff0c\u537311111\n\nfunction find(key) {\n let node = root; \n\n for (let bits = (depth - 1) * BITS; bits > 0; bits -= BITS) {\n node = node[(key >>> bits) & MASK];\n }\n\n return node[key & MASK];\n}\n```\n\n**2.4\u67e5\u627e\u90e8\u5206\u6e90\u7801**\n\nVector Trie\u67e5\u627e\u90e8\u5206\u6e90\u7801\uff0c\u8fd9\u4e5f\u662f\u6301\u4e45\u5316\u6570\u636e\u7ed3\u6784\u6838\u5fc3\n\n```js\nget(shift, keyHash, key, notSetValue) {\n if (keyHash === undefined) {\n keyHash = hash(key);\n }\n const idx = (shift === 0 ? keyHash : keyHash >>> shift) & MASK;\n const node = this.nodes[idx];\n return node\n ? node.get(shift + SHIFT, keyHash, key, notSetValue)\n : notSetValue;\n}\n```\n\n\u53ef\u4ee5\u770b\u5230\uff0c Immutable.js \u4e5f\u6b63\u662f\u91c7\u7528\u4e86\u4f4d\u5206\u533a\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u4f4d\u8fd0\u7b97\u5f97\u5230\u5f53\u524d\u6570\u7ec4\u7684 index \u9009\u62e9\u76f8\u5e94\u5206\u652f\u3002\n\n\u4e0d\u8fc7\u5b83\u7684\u5b9e\u73b0\u65b9\u5f0f\u548c\u4e0a\u6587\u6240\u8bb2\u7684\u6709\u4e00\u70b9\u4e0d\u540c\uff0c\u4e0a\u6587\u4e2d\u5bf9\u4e00\u4e2akey\uff0c\u90fd\u662f\u201c\u6b63\u5e8f\u201d\u5b58\u50a8\u7684\u3002\n\n\u800cImmutable.js\u91cc\u5219\u662f\u201c\u5012\u5e8f\u201d\uff0c\u5148\u627e\u5230key \u672b\u5c3e\u7684 SHIFT \u4e2a bit \uff0c\u7136\u540e\u518d\u5f97\u5230\u5b83\u4eec\u4e4b\u524d\u7684 SHIFT \u4e2a bit \uff0c\u4f9d\u6b21\u5f80\u524d\u4e0b\u53bb\uff0c\u800c\u524d\u9762\u6211\u4eec\u7684\u4ee3\u7801\u662f\u5148\u5f97\u5230 key \u5f00\u5934\u7684 SHIFT \u4e2a bit\uff0c\u4f9d\u6b21\u5f80\u540e\u3002\n\n\u7528\u8fd9\u79cd\u65b9\u5f0f\u7684\u613f\u610f\u4e4b\u4e00\u4e5f\u662fkey\u7684\u5927\u5c0f\uff08\u4e8c\u8fdb\u5236\u957f\u5ea6\uff09\u4e0d\u56fa\u5b9a\u3002\n\n**2.5\u65f6\u95f4\u590d\u6742\u5ea6**\n\n\u56e0\u4e3a\u91c7\u7528\u4e86\u7ed3\u6784\u5171\u4eab\uff0c\u5728\u6dfb\u52a0\u3001\u5220\u9664\u3001\u4fee\u6539\u64cd\u4f5c\u540e\uff0c\u6211\u4eec\u907f\u514d\u4e86\u5c06map\u6240\u6709\u503c\u62f7\u8d1d\u4e00\u904d\uff0c\u6240\u6709\u7279\u522b\u5728\u6570\u636e\u91cf\u5927\u7684\u65f6\u5019\uff0cObject.assgin\u6709\u660e\u663e\u63d0\u5347\u3002\n\n\u7136\u800c\u67e5\u8be2\u901f\u5ea6\u4f3c\u4e4e\u53d8\u6162\u4e86\uff0cmap\u91cc\u6839\u636ekey\u67e5\u627e\u7684\u901f\u5ea6\u662fO(1)\uff1b\u8fd9\u91cc\u7531\u4e8e\u53d8\u6210\u4e86\u4e00\u68f5\u6811\uff0c\u67e5\u8be2\u65f6\u95f4\u590d\u6742\u5ea6\u53d8\u6210\u4e86O(log N)\uff0c\u56e0\u4e3a\u662f32\u53c9\u6811\uff0c\u51c6\u786e\u7684\u8bf4\u662fO(log\u00a0N)\n32\u53c9\u6811\u5360\u7528\u7a7a\u95f4\u8fc7\u5927\uff0cImmutable\u5bf9\u6811\u7684\u9ad8\u5ea6\u548c\u5bbd\u5ea6\u90fd\u8fdb\u884c\u4e86\u201c\u538b\u7f29\u201d\u3002\n\n**3\uff09HAMT**\nHAMT\uff1ahash array mapped trie\uff0c\u5176\u539f\u7406\u548cVector Trie\u975e\u5e38\u76f8\u4f3c\uff0c\u4e0d\u8fc7\u5b83\u4f1a\u5bf9\u6811\u8fdb\u884c\u538b\u7f29\uff0c\u4ee5\u8282\u7ea6\u7a7a\u95f4\u3002\n\nImmutable.js\u53c2\u8003\u4e86HAMT\u5bf9\u6570\u7684\u9ad8\u5ea6\u548c\u8282\u70b9\u5185\u90e8\u538b\u7f29\u3002\n\n**3.1\u6811\u9ad8\u538b\u7f29**\n\n\u6811\u9ad8\u538b\u7f29\u7684\u539f\u7406\uff1a\u901a\u8fc7\u5c06\u548ckey\u503c\u65e0\u5173\u7684\u8282\u70b9\u53bb\u6389\u6765\u8fbe\u5230\u538b\u7f29\u6811\u9ad8\u7684\u76ee\u7684\u3002\u5728\u9700\u8981\u7684\u65f6\u5019\u589e\u52a0\u548c\u51cf\u5c11\u68c0\u70b9\u5373\u53ef\u3002\n\n\u4e3e\u4e2a\u4f8b\u5b50\uff1a\n\n\u6709\u4e2a2\u53c9Vector Trie\uff0c\u73b0\u5728\u5b58\u4e86\u4e00\u4e2a\u503c\uff0ckey\u4e3a110\uff0c\u88ab\u5b58\u5230\u4e860 1 1\u8def\u52b2\u4e0b\uff08\u6e90\u7801\u4e2d\u8bfb\u53d6\u8def\u5f84\u548c\u6811\u7684\u8def\u5f84\u76f8\u53cd\uff0c\u89c1\u4e0a\u6587\uff09\n![HAMT1.png](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-491-6.png)\n\n\u8fd9\u91cc\u5df2\u7ecf\u5c55\u793a\u7684\u7ed3\u6784\u5df2\u7ecf\u6700\u7b80\u5355\u7684\u4f18\u5316\uff0c\u56e0\u4e3a\u73b0\u5728\u53ea\u5b58\u4e86\u4e00\u4e2a\u503c\uff0c\u6240\u4ee5\u628a\u4e0e110\u65e0\u5173\u7684\u8282\u70b9\u53bb\u6389\u3002\u4e2d\u95f4\u4e24\u4e2a\u8282\u70b9\u4e5f\u53ef\u4ee5\u53bb\u6389\u3002\u5982\u4e0b\u56fe\uff1a\n\n![hamt2.png](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-491-7.png)\n\n\u83b7\u53d6\u8be5\u503c\u76f4\u63a5\u4ece0\u627e\u4e0b\u6765\uff0c\u53d1\u73b0\u76f4\u63a5\u662f\u6839\u8282\u70b9\u3002\n\n\u5982\u679c\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a\u503c\uff0c\u5b83\u7684key\u7ed3\u5c3e\u4e5f\u662f0\uff0c\u600e\u4e48\u529e\uff0c\u5f88\u7b80\u5355\uff0c\u5982\u4e0b\u56fe\uff1a\n\n![HAMT3.png](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-491-8.png)\n\n\u6211\u4eec\u53ea\u8981\u5728\u9700\u8981\u7684\u65f6\u5019\u589e\u52a0\u6216\u51cf\u5c11\u8282\u70b9\u5373\u53ef\u3002\n\n**3.2\u8282\u70b9\u5185\u90e8\u538b\u7f29-Bitmap**\n\n\u00a0Immutable.js \u7684 Trie \u91cc\uff0c\u6bcf\u4e2a\u8282\u70b9\u6570\u7ec4\u7684\u957f\u5ea6\u662f 32 \uff0c\u7136\u800c\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u8fd9 32 \u4e2a\u4f4d\u7f6e\u5927\u90e8\u5206\u662f\u7528\u4e0d\u5230\u7684\uff0c\u8fd9\u4e48\u5927\u7684\u6570\u7ec4\u663e\u7136\u4e5f\u5360\u7528\u4e86\u5f88\u5927\u7a7a\u95f4\u3002\u4f7f\u7528`Bitmap`\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u538b\u7f29\n\n**\u8282\u70b9\u5185\u90e8\u538b\u7f29\u539f\u7406**\uff1a\u7528\u4e00\u4e2a\u4e8c\u8fdb\u5236\u5047\u503c\u6765\u8868\u793a\u8282\u70b9\u6570\u7ec4\u7684\u4e0a\u6bcf\u4e2a\u4f4d\u7f6e\u662f\u5426\u6709\u503c\u7684\u60c5\u51b5\u3002\u7136\u540e\u901a\u8fc7popcount\u7b97\u6cd5\u6765\u4ece\u4e8c\u8fdb\u5236\u5047\u503c\u4e0a\u6765\u83b7\u53d6\u6570\u7ec4\u6709\u503c\u7684\u6570\u91cf\u548c\u4e0b\u6807\u3002\n\n\u4e3e\u4e2a\u4f8b\u5b50\uff1a\n\n\u5148\u62ff\u957f\u5ea6\u4e3a 8 \u7684\u6570\u7ec4\n\n![bitmap1.jpg](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-491-9.jpeg)\n\n\u6211\u4eec\u5b9e\u9645\u4e0a\u53ea\u662f\u7528\u4e86\u6570\u7ec4\u7684\u4e0b\u6807\u5bf9 key \u8fdb\u884c\u7d22\u5f15\uff0c\u8fd9\u6837\u60f3\u6570\u7ec4\u7b2c 5\u30016\u30017 \u4f4d\u663e\u7136\u76ee\u524d\u662f\u6beb\u65e0\u4f5c\u7528\u7684\uff0c\u90a3 0\u30012\u30013 \u5462\uff1f\u6211\u4eec\u6709\u5fc5\u8981\u4e3a\u4e86\u4e00\u4e2a\u4e0b\u6807 4 \u53bb\u7ef4\u6301\u4e00\u4e2a\u957f\u5ea6\u4e3a5\u7684\u6570\u7ec4\u5417\uff1f\u6211\u4eec\u53ea\u9700\u8981\u6307\u660e\u201c\u5047\u60f3\u6570\u7ec4\u201d\u4e2d\u4e0b\u6807\u4e3a 1 \u548c\u4e3a 4 \u7684\u4f4d\u7f6e\u6709\u6570\u5c31\u53ef\u4ee5\u4e86\u3002\u8fd9\u91cc\u5c31\u53ef\u4ee5\u7528\u5230bitmap\uff0c\u5982\u4e0b\uff1a\n\n![bitmap2.png](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-491-10.png)\n\n- \u4ee5\u5176\u4e8c\u8fdb\u5236\u5f62\u5f0f\u8868\u8fbe\u201c\u5047\u60f3\u7684\u957f\u5ea6\u4e3a8\u7684\u6570\u7ec4\u201d\u4e2d\u7684\u5360\u4f4d\u60c5\u51b5\uff0c1 \u8868\u793a\u6570\u7ec4\u91cc\u76f8\u5e94\u4e0b\u6807\u4f4d\u7f6e\u6709\u503c\uff0c0 \u5219\u8868\u793a\u76f8\u5e94\u4f4d\u7f6e\u4e3a\u7a7a\u3002\n- \u6bd4\u5982\u8fd9\u4e2a\u4e8c\u8fdb\u5236\u6570\u7b2c 4 \u4f4d\uff08\u4ece\u53f3\u5f80\u5de6\uff0c\u4ece 0 \u5f00\u59cb\u6570\uff09\u73b0\u5728\u662f 1 \uff0c\u5c31\u8868\u793a\u6570\u7ec4\u4e0b\u6807\u4e3a 4 \u7684\u4f4d\u7f6e\u6709\u503c\u3002\u8fd9\u6837\u539f\u672c\u7684\u957f\u5ea6\u4e3a 8 \u7684\u6570\u7ec4\u5c31\u53ef\u4ee5\u538b\u7f29\u5230 2 \u3002\n- \u6ce8\u610f\u8fd9\u4e2a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fd8\u662f\u6309\u7167\u201c\u5047\u60f3\u6570\u7ec4\u201d\u4e2d\u7684\u987a\u5e8f\u6392\u5217\u7684\uff0c\u8fd9\u6837\u6211\u4eec\u82e5\u8981\u53d6\u201c\u5047\u60f3\u6570\u7ec4\u201d\u4e2d\u4e0b\u6807\u4e3a i \u7684\u5143\u7d20\u65f6\uff0c\u9996\u5148\u662f\u5224\u65ad\u8be5\u4f4d\u7f6e\u6709\u6ca1\u6709\u503c\uff0c\u82e5\u6709\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u5f97\u5230\u5728\u5b83\u4e4b\u524d\u6709\u51e0\u4e2a\u5143\u7d20\uff0c\u5373\u5728\u4e8c\u8fdb\u5236\u6570\u91cc\u7b2c i \u4f4d\u4e4b\u524d\u6709\u591a\u5c11\u4f4d\u4e3a 1 \uff0c\u5047\u8bbe\u6570\u91cf\u4e3a a \uff0c\u90a3\u4e48\u8be5\u5143\u7d20\u5728\u5f53\u524d\u538b\u7f29\u540e\u7684\u6570\u7ec4\u91cc\u4e0b\u6807\u5c31\u662f a \u3002\n\n\u5177\u4f53\u64cd\u4f5c\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7`bitmap & (1 << i - 1)`\uff0c\u5f97\u5230\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6570\uff0c\u8be5\u4e8c\u8fdb\u5236\u6570\u4e2d\u53ea\u6709\u7b2c i \u4f4d\u4e4b\u524d\u6709\u503c\u7684\u5730\u65b9\u4e3a 1 \uff0c\u5176\u4f59\u5168\u4e3a 0 \uff0c\u4e0b\u9762\u6211\u4eec\u53ea\u9700\u7edf\u8ba1\u8be5\u4e8c\u8fdb\u5236\u6570\u91cc 1 \u7684\u6570\u91cf\u5373\u53ef\u5f97\u5230\u4e0b\u6807\u3002\u8ba1\u7b97\u4e8c\u8fdb\u5236\u6570\u4e2d 1 \u6570\u91cf\u7684\u8fc7\u7a0b\u88ab\u79f0\u4f5c`[popcount](https://en.wikipedia.org/wiki/Hamming_weight)`\uff0c\n\n```js\nget(shift, keyHash, key, notSetValue) {\n if (keyHash === undefined) {\n keyHash = hash(key);\n }\n const bit = 1 << ((shift === 0 ? keyHash : keyHash >>> shift) & MASK);\n const bitmap = this.bitmap;\n return (bitmap & bit) === 0\n ? notSetValue\n : this.nodes[popCount(bitmap & (bit - 1))].get(\n shift + SHIFT,\n keyHash,\n key,\n notSetValue\n );\n}\n```\n\n\u53ef\u89c1\u5b83\u4e0e\u6211\u4eec\u4e0a\u4e00\u7bc7\u770b\u5230\u7684\u6e90\u7801\u5e76\u6ca1\u6709\u592a\u5927\u4e0d\u540c\uff08Immutable.js \u91cc\u5982\u679c\u4e00\u4e2a\u6570\u7ec4\u5360\u7528\u4e0d\u8d85\u8fc7\u4e00\u534a\uff08 16 \u4e2a\uff09\uff0c\u5c31\u4f1a\u5bf9\u5176\u8fdb\u884c\u538b\u7f29\uff0c\u4e0a\u4e00\u7bc7\u7684\u6e90\u7801\u5c31\u662f\u6ca1\u6709\u538b\u7f29\u4e0b\u7684\u60c5\u51b5\uff09\uff0c\u5c31\u662f\u591a\u4e86\u4e00\u4e2a\u7528 bitmap \u8ba1\u7b97\u6570\u7ec4\u4e0b\u6807\u7684\u8fc7\u7a0b\uff0c\u65b9\u5f0f\u4e5f\u8ddf\u4e0a\u6587\u6240\u8bb2\u7684\u4e00\u6837\uff0c\u5bf9\u4e8e\u8fd9\u4e2a`popCount`\u65b9\u6cd5\uff0c\u6211\u628a\u6e90\u7801\u4e5f\u8d34\u51fa\u6765\uff1a\n\n```js\nfunction popCount(x) {\n x -= (x >> 1) & 0x55555555;\n x = (x & 0x33333333) + ((x >> 2) & 0x33333333);\n x = (x + (x >> 4)) & 0x0f0f0f0f;\n x += x >> 8;\n x += x >> 16;\n return x & 0x7f;\n}\n```\n**4\uff09Transient**\n\n- Immutable.js \u4e2d\u7684\u6570\u636e\u7ed3\u6784\u6709\u4e24\u79cd\u5f62\u6001\uff0c\u201c**\u4e0d\u53ef\u53d8**\u201d\u548c\u201c**\u53ef\u53d8**\u201d\u3002\n- \u867d\u7136\u201c\u4e0d\u53ef\u53d8\u201d\u662f Immutable.js \u7684\u4e3b\u8981\u4f18\u52bf\uff0c\u4f46\u201c\u53ef\u53d8\u201d\u5f62\u6001\u4e0b\u7684\u64cd\u4f5c\u5f53\u7136\u6548\u7387\u66f4\u9ad8\u3002\u6709\u65f6\u5bf9\u4e8e\u67d0\u4e00\u7cfb\u5217\u64cd\u4f5c\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5f97\u5230**\u8fd9\u7ec4\u64cd\u4f5c\u7ed3\u675f\u540e\u7684\u72b6\u6001**\uff0c\u82e5\u4e2d\u95f4\u7684\u6bcf\u4e00\u4e2a\u64cd\u4f5c\u90fd\u7528\u4e0d\u53ef\u53d8\u6570\u636e\u7ed3\u6784\u53bb\u5b9e\u73b0\u663e\u7136\u6709\u4e9b\u591a\u4f59\u3002\n- \u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528`[withMutations](http://facebook.github.io/immutable-js#batching-mutations)`\u65b9\u6cd5\u5bf9\u76f8\u5e94\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u4e34\u65f6\u7684\u201c\u53ef\u53d8\u201d\u64cd\u4f5c\uff0c\u6700\u540e\u518d\u8fd4\u56de\u4e00\u4e2a\u4e0d\u53ef\u53d8\u7684\u7ed3\u6784\uff0c\u8fd9\u5c31\u662f`Transient`\uff0c\u6bd4\u5982\u8fd9\u6837\uff1a\n\n```js\nlet map = new Immutable.Map({});\nmap = map.withMutations((m) => {\n // \u5f00\u542fTransient\n m.set('a', 1); // \u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5728m\u4e0a\u8fdb\u884c\u4fee\u6539\uff0c\u4e0d\u9700\u8981 m = m.set('a', 1)\n m.set('b', 2);\n m.set('c', 3);\n});\n// Transient\u7ed3\u675f\n```\n\n\u5b9e\u9645\u4e0a\uff0c Immutable.js \u91cc\u5f88\u591a\u65b9\u6cd5\u90fd\u4f7f\u7528\u4e86`withMutations`\u6784\u9020\u4e34\u65f6\u7684\u53ef\u53d8\u6570\u636e\u7ed3\u6784\u6765\u63d0\u9ad8\u6548\u7387\uff0c\u6bd4\u5982 Map \u4e2d\u7684`map`\u3001`deleteAll`\u65b9\u6cd5\u4ee5\u53ca Map \u7684\u6784\u9020\u51fd\u6570\u3002\n\n**5\uff09hash\u51b2\u7a81**\n\n**hash\u51b2\u7a81**\uff1a\u00a0Immutable.js \u4f1a\u5148\u5bf9 key \u8fdb\u884c hash \uff0c\u6839\u636e hash \u540e\u7684\u503c\u5b58\u5230\u6811\u7684\u76f8\u5e94\u4f4d\u7f6e\u91cc\u3002\u4e0d\u540c\u7684 key \u88ab hash \u540e\u7684\u7ed3\u679c\u662f\u53ef\u80fd\u76f8\u540c\u7684\uff0c\u5373\u4fbf\u6982\u7387\u5e94\u5f53\u5f88\u5c0f\u3002\n\n**\u89e3\u51b3\u65b9\u6cd5**\uff1a\u5c06\u51b2\u7a81\u7684\u8282\u70b9\u6269\u5c55\u6210\u4e00\u4e2a\u7ebf\u6027\u7ed3\u6784\uff0c\u5373\u6570\u7ec4\uff0c\u6570\u7ec4\u91cc\u76f4\u63a5\u5b58\u4e00\u7ec4\u7ec4[key,value]\uff0c\u67e5\u627e\u5230\u6b64\u5904\u65f6\u904d\u5386\u8be5\u6570\u7ec4\u627e\u5230\u5339\u914d\u7684key\u3002\n\n\u867d\u7136\u8fd9\u91cc\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u7ebf\u6027\u7684\uff0c\u8003\u8651\u5230\u53d1\u751fhash\u51b2\u7a81\u7684\u6982\u7387\u5f88\u4f4e\uff0c\u6240\u4ee5\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u589e\u52a0\u53ef\u4ee5\u5ffd\u7565\u4e0d\u8ba1\u3002", "answer": "", "biz_type": 1, "qid": 491, "subject": "", "title": "\u4ecb\u7ecd Immuable"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5151\u5427"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 490, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001jsonp\u539f\u7406\n\n#### 1.1 \u8df3\u8fc7\u540c\u6e90\u7b56\u7565\u7684\u6807\u7b7e\n\n\u6d4f\u89c8\u5668\u7684 \u540c\u6e90\u7b56\u7565 \u628a\u8de8\u57df\u8bf7\u6c42\u90fd\u7981\u6b62\u4e86\uff0c\u4f46\u662f\u9875\u9762\u4e2d `link`\u00a0 `script`\u00a0`img`\u00a0`iframe`\u00a0`a`\u00a0\u6807\u7b7e\u662f\u4e2a\u4f8b\u5916\uff0c\u8fd9\u4e9b\u6807\u7b7e\u7684\u5916\u94fe\u662f\u4e0d\u53d7\u540c\u6e90\u7b56\u7565\u9650\u5236\u7684\u3002\n\n`jsonp`\u00a0\u5c31\u662f\u5229\u7528\u4e86\u4e0a\u9762 `script`\u00a0\u6807\u7b7e\u7279\u6027\u6765\u8fdb\u884c\u8de8\u57df\u6570\u636e\u8bbf\u95ee\u3002\n\n#### 1.2 jsonp \u7684\u5b9e\u73b0\u673a\u5236\n\n1. \u4e0e\u670d\u52a1\u7aef\u7ea6\u5b9a\u597d\u4e00\u4e2a \u56de\u8c03\u51fd\u6570\u540d \uff0c\u5728\u5ba2\u6237\u7aef\u5b9a\u4e49\u597d\u8fd9\u4e2a\u51fd\u6570\uff0c\u5728\u8bf7\u6c42url\u4e2d\u6dfb\u52a0 `callback= \u51fd\u6570\u540d`\u00a0\u7684\u67e5\u8be2\u5b57\u7b26\u3002\n1. \u670d\u52a1\u7aef\u63a5\u6536\u5230\u8bf7\u6c42\u4e4b\u540e\uff0c\u5c06\u51fd\u6570\u540d\u548c\u9700\u8981\u8fd4\u56de\u7684\u6570\u636e\uff0c\u62fc\u63a5\u6210 `\"\u51fd\u6570\u540d(data)\"`\u00a0\u51fd\u6570\u6267\u884c\u7684\u5f62\u5f0f\u8fd4\u56de\n1. \u9875\u9762\u63a5\u6536\u5230\u6570\u636e\u540e\uff0c\u89e3\u6790\u5b8c\u76f4\u63a5\u6267\u884c\u4e86\u8fd9\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u8fd9\u65f6\u6570\u636e\u5c31\u6210\u529f\u4f20\u8f93\u5230\u4e86\u5ba2\u6237\u7aef\u3002\n\n- \u5ba2\u6237\u7aef\u4ee3\u7801\uff1a\n\n```js\n var flightHandler = function (data) {\n alert('\u4f60\u67e5\u8be2\u7684\u822a\u73ed\u7ed3\u679c\u662f\uff1a\u7968\u4ef7 ' + data.price + ' \u5143\uff0c' + '\u4f59\u7968 ' + data.tickets + ' \u5f20\u3002');\n };\n \n var url = \"http://localhost:8080/jsonp?callback=flightHandler\";\n var script = document.createElement('script');\n script.setAttribute('src', url);\n document.getElementsByTagName('head')[0].appendChild(script);\n```\n\n- \u670d\u52a1\u7aef\u4ee3\u7801\uff1akoa2\u5b9e\u73b0\u4e00\u4e2a\u670d\u52a1\n\n```js\napp.use(async (ctx, next) => {\n if (ctx.path == '/jsonp' && ctx.querystring) {\n //querystring\u5904\u7406\n let queryArr = ctx.querystring.split(\"&\");\n let queryObj = {};\n queryArr.forEach((item) => {\n let tmp = item.split(\"=\"); \n queryObj[tmp[0]] = tmp[1];\n })\n const callback = queryObj['callback'];\n const obj = {\n price: '18',\n tickets: 33\n }\n const args = JSON.stringify(obj);\n ctx.body = `${callback}(${args})`;\n }\n await next();\n})\n```\n\n### \u4e8c\u3001\u4e3a\u4ec0\u4e48\u4e0d\u652f\u6301 POST\n\n- script \u7684 src \u4e0d\u80fd\u53d1\u9001 POST \u8bf7\u6c42\n- POST\u8bf7\u6c42\u4f1a\u5f15\u53d1 \u8de8\u57df\u68c0\u67e5\n\n\u89c4\u8303\u8981\u6c42\uff0c\u5bf9\u4e8e\u4f1a\u7ed9\u670d\u52a1\u5668\u4ea7\u751f\u526f\u4f5c\u7528\u7684\u8bf7\u6c42\uff08\u9664GET\u4ee5\u5916\u7684\u522b\u7684HTT\u8bf7\u6c42\u3001\u6216\u8005\u642d\u914d\u67d0\u79cdMIME\u7c7b\u578b\u7684POST\uff09\uff0c\u670d\u52a1\u5668\u90fd\u4f1a\u4f7f\u7528 OPTIONS \u53d1\u9001\u4e00\u4e2a \u9884\u68c0\u8bf7\u6c42\uff0c\u6765\u83b7\u77e5\u670d\u52a1\u5668\u662f\u5426\u5141\u8bb8\u8be5\u8de8\u57df\u8bf7\u6c42\u3002", "answer": "", "biz_type": 1, "qid": 490, "subject": "", "title": "jsonp \u4e3a\u4ec0\u4e48\u4e0d\u652f\u6301 post \u65b9\u6cd5"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5151\u5427", "\u6d77\u98ce\u6559\u80b2"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 487, "qtype": "short_answer", "short_answer": {"analysis": "### Koa2\n\nKoa\u662f\u7ee7Express\u4e4b\u540e\uff0cNode\u7684\u53c8\u4e00\u4e3b\u6d41Web\u5f00\u53d1\u6846\u67b6\u3002\u76f8\u6bd4\u4e8eExpress\uff0cKoa\u53ea\u4fdd\u7559\u4e86\u6838\u5fc3\u7684\u4e2d\u95f4\u4ef6\u5904\u7406\u903b\u8f91\uff0c\u53bb\u6389\u4e86\u8def\u7531\uff0c\u6a21\u677f\uff0c\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u529f\u80fd\uff0c\u662f\u4e00\u4e2a\u57fa\u4e8eNode\u5b9e\u73b0\u7684Web\u6846\u67b6\uff0c\u7279\u70b9\u662f\u4f18\u96c5\u3001\u7b80\u6d01\u3001\u5065\u58ee\u3001\u4f53\u79ef\u5c0f\u3001\u8868\u73b0\u529b\u5f3a\u3002\u5b83\u6240\u6709\u7684\u529f\u80fd\u901a\u8fc7\u63d2\u4ef6\u7684\u5f62\u5f0f\u6765\u5b9e\u73b0\u3002\n\n#### 1.\u539f\u7406\n\nKoa2\u662f\u4e00\u4e2a\u57fa\u4e8eNode\u5b9e\u73b0\u7684Web\u6846\u67b6\uff0c\u7279\u70b9\u662f\u4f18\u96c5\u3001\u7b80\u6d01\u3001\u5065\u58ee\u3001\u4f53\u79ef\u5c0f\u3001\u8868\u73b0\u529b\u5f3a\u3002\u5b83\u6240\u6709\u7684\u529f\u80fd\u901a\u8fc7\u63d2\u4ef6\u7684\u5f62\u5f0f\u6765\u5b9e\u73b0\u3002\n\nkoa2 \u662f\u901a\u8fc7\u5c01\u88c5\u539f\u751f\u7684node http\u6a21\u5757\u3002koa\u7684 Context \u628a Node \u7684 Request \u5bf9\u8c61\u548c Response \u5bf9\u8c61\u5c01\u88c5\u5230\u5355\u4e2a\u5bf9\u8c61\u4e2d\uff0c\u5e76\u4e14\u66b4\u9732\u7ed9\u4e2d\u95f4\u4ef6\u7b49\u56de\u8c03\u51fd\u6570.\n\n\u6700\u4e3b\u8981\u7684\u6838\u5fc3\u662f **\u4e2d\u95f4\u4ef6\u673a\u5236\u6d0b\u8471\u6a21\u578b**\n\n![\u6d0b\u8471\u6a21\u578b](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-487-middleware.png)\n\n\u901a\u8fc7use()\u6ce8\u518c\u591a\u4e2a\u4e2d\u95f4\u4ef6\u653e\u5165\u6570\u7ec4\u4e2d\uff0c\u7136\u540e\u4ece\u5916\u5c42\u5f00\u59cb\u5f80\u5185\u6267\u884c\uff0c\u9047\u5230next()\u540e\u8fdb\u5165\u4e0b\u4e00\u4e2a\u4e2d\u95f4\u4ef6\uff0c\u5f53\u6240\u6709\u4e2d\u95f4\u4ef6\u6267\u884c\u5b8c\u540e\uff0c\u5f00\u59cb\u8fd4\u56de\uff0c\u4f9d\u6b21\u6267\u884c\u4e2d\u95f4\u4ef6\u4e2d\u672a\u6267\u884c\u7684\u90e8\u5206.\u6574\u4f53\u6d41\u7a0b\u5c31\u662f\u9012\u5f52\u5904\u7406\u3002\n\n```js\nfunction compose(middleware) {\n // console.log(middleware)\n // [ [AsyncFunction: fn1], [AsyncFunction: fn2], [AsyncFunction: fn3] ]\n return () => {\n // \u5148\u6267\u884c\u7b2c\u4e00\u4e2a\u51fd\u6570\n return dispatch(0)\n \n function dispatch(i) {\n let fn = middleware[i]\n // \u5982\u4f55\u4e0d\u5b58\u5728\u76f4\u63a5\u8fd4\u56de Promise\n if (!fn) {\n return Promise.resolve()\n }\n // step1: \u8fd4\u56de\u4e00\u4e2a Promise\uff0c\u56e0\u6b64\u5355\u7eaf\u53d8\u6210\u4e00\u4e2a Promise \u4e14 \u7acb\u5373\u6267\u884c\n // step2: \u5f80\u5f53\u524d\u4e2d\u95f4\u4ef6\u4f20\u5165\u4e00\u4e2anext()\u65b9\u6cd5\uff0c\u5f53\u8fd9\u4e2a\u4e2d\u95f4\u4ef6\u6709\u6267\u884c next \u7684\u65f6\u5019\u624d\u6267\u884c\u4e0b\u4e00\u4e2a\u4e2d\u95f4\u4ef6\n return Promise.resolve(fn(function next() {\n // \u6267\u884c\u4e0b\u4e00\u4e2a\u4e2d\u95f4\u4ef6\n return dispatch(i + 1)\n }))\n }\n }\n }\n```\n\n\u6838\u5fc3\u4ee3\u7801\u662f `return Promise.resolve(fn(context, dispatch.bind(null, i + 1)));` \u9012\u5f52\u904d\u5386\uff0c\u76f4\u5230\u904d\u5386\u5b8c\u6240\u6709\u7684\u4e2d\u95f4\u4ef6next\uff0c\u751f\u6210\u4e00\u4e2a\u591a\u5c42\u5d4c\u5957\u7684promise\u51fd\u6570\u3002\n\nkoa\u7684\u4e2d\u95f4\u4ef6\u5904\u7406\u53ef\u4ee5\u5f53\u505a\u662f\u6d0b\u8471\u6a21\u578b\u3002\u4e2d\u95f4\u4ef6\u6570\u7ec4\u4e2d\u4e2d\u95f4\u4ef6\u7684\u6267\u884c\u662f\u901a\u8fc7\u9012\u5f52\u7684\u65b9\u5f0f\u6765\u6267\u884c\uff0c\u8c03\u7528dispatch\u51fd\u6570\uff0c\u4ece\u7b2c\u4e00\u4e2a\u5f00\u59cb\u6267\u884c\uff0c\u5f53\u6709next\u65b9\u6cd5\u65f6\u521b\u5efa\u4e00\u4e2apromise\uff0c\u7b49\u5230\u4e0b\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u6267\u884c\u7ed3\u679c\u540e\u518d\u6267\u884cnext\u540e\u7aef\u4ee3\u7801\u3002\u5f53\u7b2c\u4e8c\u4e2a\u4e2d\u95f4\u4ef6\u4e5f\u6709next\u65b9\u6cd5\u65f6\uff0c\u4f9d\u7136\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684promise\u7b49\u5f85\u4e0b\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u7684\u6267\u884c\u7ed3\u679c\uff0c\u8fd9\u4e5f\u5c31\u662f\u4e2d\u95f4\u4ef6next\u7684\u6267\u884c\u539f\u7406\n\n`app.use()` \u5c06\u4e2d\u95f4\u4ef6push\u5230\u4e2d\u95f4\u4ef6\u6570\u7ec4\u4e2d\uff0c\u7136\u540e\u5728listen\u65b9\u6cd5\u4e2d\u901a\u8fc7\u8c03\u7528compose\u65b9\u6cd5\u8fdb\u884c\u96c6\u4e2d\u5904\u7406\u3002\n\n#### 2.Koa\u57fa\u672c\u7ec4\u6210\n\n- `application.js`\uff1aApplication(\u6216Koa)\u8d1f\u8d23\u7ba1\u7406\u4e2d\u95f4\u4ef6\uff0c\u4ee5\u53ca\u5904\u7406\u8bf7\u6c42\n- `context.js`\uff1aContext\u7ef4\u62a4\u4e86\u4e00\u4e2a\u8bf7\u6c42\u7684\u4e0a\u4e0b\u6587\u73af\u5883\n- `request.js`\uff1aRequest\u5bf9`req`\u505a\u4e86\u62bd\u8c61\u548c\u5c01\u88c5\n- `response.js`\uff1aResponse\u5bf9`res`\u505a\u4e86\u62bd\u8c61\u548c\u5c01\u88c5\n\n**1\uff09Application**\n\n\u4e3b\u8981\u7ef4\u62a4\u4e2d\u95f4\u4ef6\u4ee5\u53ca\u5176\u5b83\u4e00\u4e9b\u73af\u5883\n\n```js\n// application.js\nmodule.exports = class Application extends Emitter {\n constructor() {\n super();\n this.proxy = false;\n this.middleware = [];\n this.subdomainOffset = 2;\n this.env = process.env.NODE_ENV || 'development';\n this.context = Object.create(context);\n this.request = Object.create(request);\n this.response = Object.create(response);\n }\n // ...\n```\n\n\u901a\u8fc7 app.use(fn) \u53ef\u4ee5\u5c06 fn \u6dfb\u52a0\u5230\u4e2d\u95f4\u4ef6\u5217\u8868 this.middleware \u4e2d\u3002\n\napp.listen \u65b9\u6cd5\u6e90\u7801\u5982\u4e0b\uff1a\n\n```js\n// application.js\nlisten() {\n debug('listen');\n const server = http.createServer(this.callback());\n return server.listen.apply(server, arguments);\n}\n```\n\n\u9996\u5148\u4f1a\u901a\u8fc7 this.callback \u65b9\u6cd5\u6765\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a http.createServer \u7684\u56de\u8c03\u51fd\u6570\uff0c\u7136\u540e\u8fdb\u884c\u76d1\u542c\u3002\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\uff0c http.createServer \u7684\u56de\u8c03\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570: req \u548c res\uff0c\u4e0b\u9762\u6765\u770bthis.callback \u7684\u5b9e\u73b0\uff1a\n\n```js\n// application.js\ncallback() {\n const fn = compose(this.middleware);\n if (!this.listeners('error').length) this.on('error', this.onerror);\n return (req, res) => {\n res.statusCode = 404;\n const ctx = this.createContext(req, res);\n onFinished(res, ctx.onerror);\n fn(ctx).then(() => respond(ctx)).catch(ctx.onerror);\n };\n}\n```\n\n\u9996\u5148\u662f\u5c06\u6240\u6709\u7684\u4e2d\u95f4\u4ef6\u901a\u8fc7 compose \u7ec4\u5408\u6210\u4e00\u4e2a\u51fd\u6570 fn\uff0c\u7136\u540e\u8fd4\u56de http.createServer \u6240\u9700\u8981\u7684\u56de\u8c03\u51fd\u6570\u3002\u4e8e\u662f\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5f53\u670d\u52a1\u5668\u6536\u5230\u4e00\u4e2a\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u4f1a\u4f7f\u7528 req \u548c res \u901a\u8fc7 this.createContext \u65b9\u6cd5\u6765\u521b\u5efa\u4e00\u4e2a\u4e0a\u4e0b\u6587\u73af\u5883 ctx\uff0c\u7136\u540e\u4f7f\u7528 fn \u6765\u8fdb\u884c\u4e2d\u95f4\u4ef6\u7684\u903b\u8f91\u5904\u7406\u3002\n\n**2\uff09Context**\n\n\u901a\u8fc7\u4e0a\u9762\u7684\u5206\u6790\uff0c\u6211\u4eec\u5df2\u7ecf\u53ef\u4ee5\u5927\u6982\u5f97\u77e5Koa\u5904\u7406\u8bf7\u6c42\u7684\u8fc7\u7a0b\uff1a\u5f53\u8bf7\u6c42\u5230\u6765\u7684\u65f6\u5019\uff0c\u4f1a\u901a\u8fc7 req \u548c res \u6765\u521b\u5efa\u4e00\u4e2a context (ctx)\uff0c\u7136\u540e\u6267\u884c\u4e2d\u95f4\u4ef6\u3002\n\n\u4e8b\u5b9e\u4e0a\uff0c\u5728\u521b\u5efa context \u7684\u65f6\u5019\uff0c\u8fd8\u4f1a\u540c\u65f6\u521b\u5efa request \u548c response\uff0c\u901a\u8fc7\u4e0b\u56fe\u53ef\u4ee5\u6bd4\u8f83\u76f4\u89c2\u5730\u770b\u5230\u6240\u6709\u8fd9\u4e9b\u5bf9\u8c61\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\n\n![context](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-487-koa2.png)\n\n- \u6700\u5de6\u8fb9\u4e00\u5217\u8868\u793a\u6bcf\u4e2a\u6587\u4ef6\u7684\u5bfc\u51fa\u5bf9\u8c61\n- \u4e2d\u95f4\u4e00\u5217\u8868\u793a\u6bcf\u4e2aKoa\u5e94\u7528\u53ca\u5176\u7ef4\u62a4\u7684\u5c5e\u6027\n- \u53f3\u8fb9\u4e24\u5217\u8868\u793a\u5bf9\u5e94\u6bcf\u4e2a\u8bf7\u6c42\u6240\u7ef4\u62a4\u7684\u4e00\u4e9b\u5bf9\u8c61\n- \u9ed1\u8272\u7684\u7ebf\u8868\u793a\u5b9e\u4f8b\u5316\n- \u7ea2\u8272\u7684\u7ebf\u8868\u793a\u539f\u578b\u94fe\n- \u84dd\u8272\u7684\u7ebf\u8868\u793a\u5c5e\u6027\n\n\u5b9e\u9645\u4e0a\uff0c ctx \u4e3b\u8981\u7684\u529f\u80fd\u662f\u4ee3\u7406 request \u548c response \u7684\u529f\u80fd\uff0c\u63d0\u4f9b\u4e86\u5bf9 request \u548c response \u5bf9\u8c61\u7684\u4fbf\u6377\u8bbf\u95ee\u80fd\u529b\u3002\u5728\u6e90\u7801\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff1a\n\n```js\n// context.js\ndelegate(proto, 'response')\n .method('attachment')\n // ...\n .access('status')\n // ...\n .getter('writable');\n\ndelegate(proto, 'request')\n .method('acceptsLanguages')\n // ...\n .access('querystring')\n // ...\n .getter('ip');\n```\n\n\u8fd9\u91cc\u4f7f\u7528\u4e86 delegates \u6a21\u5757\u6765\u5b9e\u73b0\u5c5e\u6027\u8bbf\u95ee\u7684\u4ee3\u7406\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u901a\u8fc7 delegate(proto, 'response') \uff0c\u5f53\u8bbf\u95ee proto \u7684\u4ee3\u7406\u5c5e\u6027\u7684\u65f6\u5019\uff0c\u5b9e\u9645\u4e0a\u662f\u5728\u8bbf\u95ee proto.response \u7684\u5bf9\u5e94\u5c5e\u6027\u3002\n\n**3\uff09\u4e2d\u95f4\u4ef6\u7684\u6267\u884c**\n\n\u5728\u4e0a\u9762\u5df2\u7ecf\u63d0\u5230\uff0c\u6240\u6709\u7684\u4e2d\u95f4\u4ef6\u4f1a\u7ecf\u8fc7 compose \u5904\u7406\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u3002\u8be5\u6a21\u5757\u6e90\u7801\u5982\u4e0b\uff1a\n\n```js\nfunction compose(middleware) {\n // \u9519\u8bef\u5904\u7406\n if (!Array.isArray(middleware)) throw new TypeError('Middleware stack must be an array!')\n for (const fn of middleware) {\n if (typeof fn !== 'function') throw new TypeError('Middleware must be composed of functions!')\n }\n\n return function(context, next) {\n // last called middleware #\n let index = -1\n return dispatch(0)\n\n function dispatch(i) {\n if (i <= index) return Promise.reject(new Error('next() called multiple times'))\n // \u5f53\u524d\u6267\u884c\u7b2c i \u4e2a\u4e2d\u95f4\u4ef6\n index = i\n let fn = middleware[i]\n // \u6240\u6709\u7684\u4e2d\u95f4\u4ef6\u6267\u884c\u5b8c\u6bd5\n if (i === middleware.length) fn = next\n if (!fn) return Promise.resolve()\n\n try {\n // \u6267\u884c\u5f53\u524d\u7684\u4e2d\u95f4\u4ef6\n // \u8fd9\u91cc\u7684fn\u4e5f\u5c31\u662fapp.use(fn)\u4e2d\u7684fn\n return Promise.resolve(fn(context, function next() {\n return dispatch(i + 1)\n }))\n } catch (err) {\n return Promise.reject(err)\n }\n }\n }\n}\n```\n\nKoa\u7684\u4e2d\u95f4\u4ef6\u652f\u6301\u666e\u901a\u51fd\u6570\uff0c\u8fd4\u56de\u4e00\u4e2aPromise\u7684\u51fd\u6570\uff0c\u4ee5\u53caasync\u51fd\u6570\u3002\u7531\u4e8egenerator\u51fd\u6570\u4e2d\u95f4\u4ef6\u5728\u65b0\u7684\u7248\u672c\u4e2d\u5c06\u4e0d\u518d\u652f\u6301\uff0c\u56e0\u6b64\u4e0d\u5efa\u8bae\u4f7f\u7528\u3002\n", "answer": "", "biz_type": 1, "qid": 487, "subject": "", "title": "\u4ecb\u7ecd koa2\uff0c\u539f\u7406\u662f\u4ec0\u4e48\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6caa\u6c5f", "\u5bfa\u5e93"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 449, "qtype": "short_answer", "short_answer": {"analysis": "### JavaScript\u4e2d\u7684\u5783\u573e\u56de\u6536\n\nV8\uff08javascript\u5f15\u64ce\uff09\u7684\u65b0\u8001\u7a7a\u95f4\u5185\u5b58\u5206\u914d\u4e0e\u5927\u5c0f\u9650\u5236\n\n#### \u4e00\u3001\u65b0\u8001\u7a7a\u95f4\n\n\u51e1\u4e8b\u90fd\u6709\u4e00\u628a\u53cc\u5203\u5251\uff0c\u5728\u5783\u573e\u56de\u6536\u7684\u6f14\u53d8\u8fc7\u7a0b\u4e2d\u4eba\u4eec\u53d1\u73b0\uff0c\u6ca1\u6709\u4e00\u79cd\u7279\u5b9a\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u662f\u53ef\u4ee5\u5b8c\u7f8e\u7684\u89e3\u51b3\u95ee\u9898\uff0c\u56e0\u6b64V8\u91c7\u7528\u4e86\u65b0\u751f\u4ee3\u4e0e\u8001\u751f\u4ee3\u7ed3\u5408\u7684\u5783\u573e\u56de\u6536\u65b9\u5f0f\uff0c\u5c06\u5185\u5b58\u5206\u4e3a\u65b0\u751f\u4ee3\u548c\u8001\u751f\u4ee3\u3002 \u65b0\u751f\u4ee3\u9891\u7e41\u8fdb\u884cGC\uff0c\u7a7a\u95f4\u5c0f\uff0c\u91c7\u7528\u7684\u662f\u7a7a\u95f4\u6362\u65f6\u95f4\u7684scavenge\u7b97\u6cd5\uff0c\u6240\u4ee5\u53c8\u5212\u5206\u4e3a\u4e24\u5757semispace\uff0cFrom\u548cTo\u3002 \u8001\u751f\u4ee3\u5927\u90e8\u5206\u4fdd\u5b58\u7684\u662f\u5b58\u6d3b\u65f6\u95f4\u8f83\u957f\u7684\u6216\u8005\u8f83\u5927\u7684\u5bf9\u8c61\u3002\u91c7\u7528\u7684\u662fmark-sweep\uff08\u4e3b\uff09&mark-compact\uff08\u8f85\uff09\u7b97\u6cd5\u3002\n\nV8\u9650\u5236\u4e86js\u5bf9\u8c61\u53ef\u4ee5\u4f7f\u7528\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u4e0d\u6b62\u662f\u56e0\u4e3a\u6700\u521dV8\u662f\u4f5c\u4e3a\u6d4f\u89c8\u5668\u5f15\u64ce\u800c\u8bbe\u8ba1\u7684\u3002\u8fd8\u6709\u5176\u5783\u573e\u56de\u6536\u673a\u5236\u7684\u5f71\u54cd\u56e0\u7d20\u3002V8\u4f7f\u7528stop-the-world\uff08\u5168\u505c\u987f\uff09, generational, accurate\u7684\u5783\u573e\u56de\u6536\u5668\u3002\u5728\u6267\u884c\u56de\u6536\u4e4b\u65f6\u4f1a\u6682\u65f6\u4e2d\u65ad\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u800c\u4e14\u53ea\u5904\u7406\u5bf9\u8c61\u5806\u6808\u3002\u5f53\u5185\u5b58\u8fbe\u5230\u4e00\u5b9a\u7684\u4f53\u79ef\u65f6\uff0c\u8fdb\u884c\u4e00\u6b21\u5783\u573e\u56de\u6536\u7684\u65f6\u95f4\u5c06\u4f1a\u5f88\u957f\uff0c\u4ece\u800c\u5f71\u54cd\u5176\u76f8\u5e94\u800c\u9020\u6210\u6d4f\u89c8\u5668\u5047\u6b7b\u7684\u72b6\u51b5\u3002\u56e0\u6b64\uff0c\u5728V8\u4e2d\u9650\u5236\u8001\u751f\u4ee364\u4f4d\u4e3a1.4GB\uff0c32\u4f4d\u4e3a0.7GB\uff0c\u65b0\u751f\u4ee364\u4f4d\u4e3a32M\uff0c32\u4f4d\u4e3a16M\u3002 \u5f53\u7136\uff0c\u5982\u679c\u9700\u8981\u66f4\u5927\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u5728node\u4e2d\u53ef\u4ee5\u8fdb\u884c\u66f4\u6539\u3002\n\n#### \u4e8c\u3001\u5bf9\u8c61\u664b\u5347\n\n\u65b0\u751f\u6210\u7684\u5bf9\u8c61\u653e\u5165\u65b0\u751f\u4ee3\u5185\u5b58\u4e2d\uff0c\u90a3\u54ea\u4e9b\u5bf9\u8c61\u4f1a\u88ab\u653e\u5165\u8001\u751f\u4ee3\u4e2d\u5462\uff1f\u5927\u90e8\u5206\u653e\u5165\u8001\u751f\u4ee3\u7684\u5bf9\u8c61\u662f\u7531\u65b0\u751f\u4ee3\u664b\u5347\u800c\u6765\u3002\u5bf9\u8c61\u7684\u664b\u5347\u7684\u65b9\u5f0f\uff1a\n\n\u5f53\u65b0\u751f\u4ee3\u7684To semispace\u5185\u5b58\u5360\u6ee125%\u65f6\uff0c\u6b64\u65f6\u518d\u4eceFrom semispace\u62f7\u8d1d\u5bf9\u8c61\u5c06\u4e0d\u4f1a\u518d\u653e\u5165To\u7a7a\u95f4\u4e2d\u4ee5\u9632\u5f71\u54cd\u540e\u7eed\u7684\u65b0\u5bf9\u8c61\u5206\u914d\uff0c\u800c\u5c06\u5176\u76f4\u63a5\u590d\u5236\u5230\u8001\u751f\u4ee3\u7a7a\u95f4\u4e2d\u3002\n\n\u5728\u8fdb\u884c\u4e00\u6b21\u5783\u573e\u56de\u6536\u540e\uff0c\u7b2c\u4e8c\u6b21GC\u65f6\uff0c\u53d1\u73b0\u5df2\u7ecf\u7ecf\u5386\u8fc7\u4e00\u6b21GC\u7684\u5bf9\u8c61\u5728\u4eceFrom\u7a7a\u95f4\u590d\u5236\u65f6\u76f4\u63a5\u590d\u5236\u5230\u8001\u751f\u4ee3\u3002\n\n\u5728\u65b0\u5bf9\u8c61\u5206\u914d\u65f6\u5927\u90e8\u5206\u5bf9\u8c61\u88ab\u5206\u914d\u5230\u65b0\u751f\u4ee3\u7684From semispace\uff0c\u4f46\u5f53\u8fd9\u4e2a\u5bf9\u8c61\u7684\u4f53\u79ef\u8fc7\u5927\uff0c\u8d85\u8fc71MB\u7684\u5185\u5b58\u9875\u65f6\uff0c\u76f4\u63a5\u5206\u914d\u5230\u8001\u751f\u4ee3\u4e2d\u7684large Object Space\u3002\n\n#### \u4e09\u3001\u65b0\u751f\u4ee3\u7684GC\u673a\u5236\u4e0e\u4f18\u7f3a\u70b9\n\n**\u56de\u6536\u673a\u5236**\n\n\u65b0\u751f\u4ee3\u91c7\u7528Scavenge\u7b97\u6cd5\uff0c\u5728scavenge\u7b97\u6cd5\u7684\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\uff0c\u5219\u4e3b\u8981\u91c7\u7528\u4e86cheney\u7b97\u6cd5\u3002\u5373\u4f7f\u7528\u590d\u5236\u65b9\u5f0f\u6765\u5b9e\u73b0\u5783\u573e\u56de\u6536\u3002\u5b83\u5c06\u5185\u5b58\u4e00\u5206\u4e3a\u4e8c\uff0c\u6bcf\u4e00\u4e2a\u7a7a\u95f4\u90fd\u662f\u4e00\u4e2asemispace\u3002\n\n\u5904\u4e8e\u4f7f\u7528\u72b6\u6001\u7684\u662fFrom\u7a7a\u95f4\uff0c\u95f2\u7f6e\u7684\u662fTo\u7a7a\u95f4\u3002\u5f53\u5206\u914d\u5bf9\u8c61\u65f6\uff0c\u5148\u662f\u5206\u914d\u5230From\u7a7a\u95f4\uff0c\u5783\u573e\u56de\u6536\u65f6\u4f1a\u68c0\u67e5From\u7a7a\u95f4\u4e2d\u5b58\u6d3b\u7684\u5bf9\u8c61\uff0c\u5c06\u5176\u590d\u5236\u5230To\u7a7a\u95f4\uff0c\u56de\u6536\u5176\u4ed6\u7684\u5bf9\u8c61\u3002\u5b8c\u6210\u590d\u5236\u540e\u4f1a\u8fdb\u884c\u7d27\u7f29\uff0cFrom\u548cTo\u7a7a\u95f4\u7684\u8c03\u6362\u3002\u5982\u6b64\u5faa\u73af\u5f80\u590d\u3002\n\n**\u4f18\u52bf**\n\n\u7531\u5176\u6267\u884c\u7684\u7b97\u6cd5\u53ca\u8fc7\u7a0b\u6211\u4eec\u53ef\u4ee5\u4e86\u89e3\u5230\uff0c\u5728\u65b0\u751f\u4ee3\u7684\u5783\u573e\u56de\u6536\u8fc7\u7a0b\u4e2d\uff0c\u603b\u662f\u7531\u4e00\u534a\u7684semispace\u662f\u7a7a\u4f59\u7684\u3002scavenge\u53ea\u590d\u5236\u5b58\u6d3b\u7684\u5bf9\u8c61\uff0c\u5728\u65b0\u751f\u4ee3\u7684\u5185\u5b58\u4e2d\uff0c\u5b58\u6d3b\u7684\u5bf9\u8c61\u76f8\u5bf9\u8f83\u5c11\uff0c\u6240\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u7b97\u6cd5\u6070\u5230\u597d\u5904\u3002\n\n#### \u56db\u3001\u8001\u751f\u4ee3\u7684GC\u673a\u5236\u4e0e\u4f18\u7f3a\u70b9\n\n**\u56de\u6536\u673a\u5236**\n\n\u7531\u4e8e\u7684scavenge\u7b97\u6cd5\u53ea\u590d\u5236\u5b58\u6d3b\u7684\u5bf9\u8c61\uff0c\u5982\u679c\u5728\u8001\u751f\u4ee3\u4e2d\u4e5f\u4f7f\u7528\u6b64\u7b97\u6cd5\u7684\u8bdd\u5c31\u4f1a\u9020\u6210\u590d\u5236\u5f88\u591a\u5bf9\u8c61\uff0c\u6548\u7387\u4f4e\uff0c\u5e76\u4e14\u9020\u6210\u5f88\u5927\u7684\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u3002 \u8001\u751f\u4ee3\u4e2d\u91c7\u7528\u7684\u5219\u662fmark-sweep\uff08\u6807\u8bb0\u6e05\u9664\uff09\u548cmark-compact\uff08\u6807\u8bb0\u6574\u7406\uff09\u7ed3\u5408\u7684\u65b9\u5f0f\u3002\u800c\u4e3a\u4ec0\u4e48\u4f7f\u7528\u4e24\u8005\u7ed3\u5408\u5462\uff1f\u8fd9\u5c31\u8981\u8bb2\u5230\u4e24\u8005\u7684\u4f18\u70b9\u4e0e\u7f3a\u70b9\u3002\n\n**mark-sweep\uff08\u6807\u8bb0\u6e05\u9664\uff09**\n\n1\uff09\u4f18\u70b9\n\n- \u6807\u8bb0\u6e05\u9664\u9700\u8981\u6807\u8bb0\u5806\u5185\u5b58\u4e2d\u7684\u6240\u6709\u5bf9\u8c61\uff0c\u6807\u8bb0\u51fa\u5728\u4f7f\u7528\u7684\u5bf9\u8c61\uff0c\u6e05\u9664\u90a3\u4e9b\u6ca1\u6709\u88ab\u6807\u8bb0\u7684\u5bf9\u8c61\u3002\u5728\u8001\u751f\u4ee3\u5185\u5b58\u4e2d\u4e0e\u65b0\u751f\u4ee3\u76f8\u53cd\uff0c\u4e0d\u4f7f\u7528\u7684\u5bf9\u8c61\u53ea\u5360\u5f88\u5c0f\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u6e05\u9664\u4e0d\u7528\u7684\u5bf9\u8c61\u6548\u7387\u9ad8\u3002\n- mark-sweep\u4e0d\u4f1a\u5c06\u5185\u5b58\u7a7a\u95f4\u5206\u4e3a\u4e24\u534a\uff0c\u6240\u4ee5\uff0c\u4e0d\u4f1a\u6d6a\u8d39\u4e00\u534a\u7a7a\u95f4\u3002\n\n2\uff09\u7f3a\u70b9\n\n\u4f46\u6807\u8bb0\u6e05\u9664\u4f1a\u9020\u6210\u4e00\u4e2a\u95ee\u9898\uff0c\u5c31\u662f\u5728\u6e05\u9664\u8fc7\u540e\u4f1a\u5bfc\u81f4\u5185\u5b58\u4e0d\u8fde\u7eed\uff0c\u9020\u6210\u5185\u5b58\u788e\u7247\uff0c\u5982\u679c\u6b64\u65f6\u9700\u8981\u50a8\u5b58\u4e00\u4e2a\u5f88\u5927\u7684\u5185\u5b58\u800c\u7a7a\u95f4\u53c8\u4e0d\u591f\u7684\u65f6\u5019\u5c31\u4f1a\u9020\u6210\u6ca1\u6709\u5fc5\u8981\u7684\u53cd\u590d\u5783\u573e\u56de\u6536\u3002\n\n**mark-compact\uff08\u6807\u8bb0\u6574\u7406\uff09**\n\n1\uff09\u4f18\u70b9\n\n\u6b64\u65f6\u6807\u8bb0\u6574\u7406\u5c31\u53ef\u4ee5\u51fa\u573a\u4e86\uff0c\u5728\u6807\u8bb0\u6e05\u9664\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6807\u8bb0\u6574\u7406\u4f1a\u5c06\u5b58\u6d3b\u7684\u5bf9\u8c61\u548c\u9700\u8981\u6e05\u9664\u7684\u5bf9\u8c61\u79fb\u52a8\u5230\u4e24\u7aef\u3002\u7136\u540e\u5c06\u5176\u4e2d\u4e00\u6bb5\u9700\u8981\u6e05\u9664\u7684\u6d88\u706d\u6389\uff0c\u53ef\u4ee5\u89e3\u51b3\u6807\u8bb0\u6e05\u9664\u9020\u6210\u7684\u5185\u5b58\u788e\u7247\u95ee\u9898\u3002\n\n2\uff09\u7f3a\u70b9\n\n\u4f46\u662f\u5728\u7d27\u7f29\u5185\u5b58\u7684\u8fc7\u7a0b\u4e2d\u9700\u8981\u79fb\u52a8\u5bf9\u8c61\uff0c\u6548\u7387\u6bd4\u8f83\u4f4e\u3002\u6240\u4ee5V8\u5728\u6e05\u7406\u65f6\u4e3b\u8981\u4f1a\u4f7f\u7528Mark-sweep,\u5728\u7a7a\u95f4\u4e0d\u8db3\u4ee5\u5bf9\u65b0\u751f\u4ee3\u4e2d\u664b\u5347\u8fc7\u6765\u7684\u5bf9\u8c61\u8fdb\u884c\u5206\u914d\u65f6\u624d\u4f1a\u4f7f\u7528Mark-compact\u3002\n\n#### \u4e94\u3001\u5783\u573e\u56de\u6536\u673a\u5236\u7684\u4f18\u5316\n\n\u589e\u91cf\u6807\u8bb0(\u5728\u8001\u7a7a\u95f4\u91cc\u5f15\u5165\u4e86\u6b64\u65b9\u5f0f)\n\nscavenge\u7b97\u6cd5,mark-sweep\u53camark-compact\u90fd\u4f1a\u5bfc\u81f4stop-the-world\uff08\u5168\u505c\u987f\uff09\u3002\u800c\u5168\u505c\u987f\u5f88\u5bb9\u6613\u5e26\u6765\u660e\u663e\u7684\u7a0b\u5e8f\u8fdf\u6ede\uff0c\u6807\u8bb0\u9636\u6bb5\u5f88\u5bb9\u6613\u5c31\u4f1a\u8d85\u8fc7100ms\uff0c\u56e0\u6b64V8\u5f15\u5165\u4e86\u589e\u91cf\u6807\u8bb0\uff0c\u5c06\u6807\u8bb0\u9636\u6bb5\u5206\u4e3a\u82e5\u5e72\u5c0f\u6b65\u9aa4\uff0c\u6bcf\u4e2a\u6b65\u9aa4\u63a7\u5236\u57285ms\u5185\uff0c\u6bcf\u8fd0\u884c\u4e00\u6bb5\u65f6\u95f4\u6807\u8bb0\u52a8\u4f5c\uff0c\u5c31\u8ba9JavaScript\u7a0b\u5e8f\u6267\u884c\u4e00\u4f1a\u513f\uff0c\u5982\u6b64\u4ea4\u66ff\uff0c\u660e\u663e\u5730\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u6d41\u7545\u6027\uff0c\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u907f\u514d\u4e86\u957f\u65f6\u95f4\u5361\u987f\u3002\n\n\n\n\n", "answer": "", "biz_type": 1, "qid": 449, "subject": "", "title": "JavaScript \u91cc\u5783\u573e\u56de\u6536\u673a\u5236\u662f\u4ec0\u4e48\uff0c\u5e38\u7528\u7684\u662f\u54ea\u79cd\uff0c\u600e\u4e48\u5904\u7406\u7684"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 307, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 307, "subject": "", "title": "addEventListener \u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u7684\u4f5c\u7528"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 309, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 309, "subject": "", "title": "\u83b7\u53d6 id \u4e3a netease \u8282\u70b9\u4e0b\u6240\u6709\u7684 checkbox \u5b50\u5143\u7d20(\u4e0d\u7528\u6846\u67b6\uff0c\u6ce8\u610f\u517c\u5bb9)"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 311, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 311, "subject": "", "title": "\u5982\u4f55\u83b7\u53d6\u4e00\u4e2a\u5bf9\u8c61\u7684\u6df1\u5ea6"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 313, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 313, "subject": "", "title": "\u8bf4\u4e00\u4e0b splice \u548c slice \u7684\u529f\u80fd\u7528\u6cd5"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 315, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 315, "subject": "", "title": "\u51fd\u6570\u4e2d\u7684 this \u6709\u51e0\u79cd"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 316, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 316, "subject": "", "title": "\u5982\u4f55\u540c\u65f6\u83b7\u53d6 html \u4e2d\u7684 h1,h2,h3,h4,h5,h6 \u4e2d\u7684\u5185\u5bb9"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 317, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 317, "subject": "", "title": "JavaScript \u7684\u6267\u884c\u6d41\u7a0b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 336, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 336, "subject": "", "title": "IOC \u662f\u5565\uff0c\u5e94\u7528\u573a\u666f\u662f\u5565\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 338, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 338, "subject": "```js\nfunction a(obj) {\n obj.a = 2;\n obj = { a: 3 };\n return obj;\n}\nconst obj = { a: 1 };\na(obj);\nconsole.log(obj);\n```\n", "title": "\u5199\u51fa\u4ee3\u7801\u6267\u884c\u7684\u6253\u5370\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 339, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 339, "subject": "```js\nd1,,,\nd2,,,\nd3,,,\n\n\u628a\u4e0a\u8fb9\u7684\u5b57\u7b26\u4e32\u8f93\u51fa1\uff0c2\uff0c3\u7684\u548c //6\n```\n", "title": "\u5b9e\u73b0\u51fd\u6570"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u963f\u91cc"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 361, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 361, "subject": "", "title": "\u600e\u4e48\u5b9e\u73b0 this \u5bf9\u8c61\u7684\u6df1\u62f7\u8d1d"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5b9d\u5b9d\u6811"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 362, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 362, "subject": "", "title": "\u4f7f\u7528 canvas \u7ed8\u56fe\u65f6\u5982\u4f55\u7ec4\u7ec7\u6210\u901a\u7528\u7ec4\u4ef6"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 366, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 366, "subject": "", "title": "\u4ecb\u7ecd\u4e2d\u4ecb\u8005\u6a21\u5f0f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 370, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 370, "subject": "", "title": "\u4ecb\u7ecd service worker"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613", "\u6caa\u6c5f"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 374, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 374, "subject": "", "title": "\u4ecb\u7ecd\u4e8b\u4ef6\u4ee3\u7406\u4ee5\u53ca\u4f18\u7f3a\u70b9\uff0c\u4e3b\u8981\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613", "\u8611\u83c7\u8857"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 376, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 376, "subject": "", "title": "\u4ecb\u7ecd\u4e0b this \u7684\u5404\u79cd\u60c5\u51b5"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 378, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 378, "subject": "", "title": "\u4f7f\u7528\u8def\u7531\u65f6\u51fa\u73b0\u95ee\u9898\u5982\u4f55\u89e3\u51b3"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 6, "qid": 399, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 399, "subject": "", "title": "\u4ecb\u7ecd AST\uff08Abstract Syntax Tree\uff09\u62bd\u8c61\u8bed\u6cd5\u6811"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761", "\u5b89\u5c45\u5ba2"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 406, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 406, "subject": "", "title": "== \u548c ===\u7684\u533a\u522b\uff0c\u4ec0\u4e48\u60c5\u51b5\u4e0b\u7528\u76f8\u7b49=="}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761", "\u6316\u8d22", "\u997f\u4e86\u4e48", "\u5fc3\u5a31"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 407, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 407, "subject": "", "title": "bind\u3001call\u3001apply \u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6709\u8d5e"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 414, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 414, "subject": "", "title": "\u4ecb\u7ecd\u6682\u65f6\u6027\u6b7b\u533a"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6709\u8d5e"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 415, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 415, "subject": "", "title": "ES6 \u4e2d\u7684 map \u548c\u539f\u751f\u7684\u5bf9\u8c61\u6709\u4ec0\u4e48\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6709\u8d5e"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 419, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 419, "subject": "", "title": "\u5bf9\u7eaf\u51fd\u6570\u7684\u7406\u89e3"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6709\u8d5e"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 420, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 420, "subject": "", "title": "\u4ecb\u7ecd JSX"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6709\u8d5e"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 425, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 425, "subject": "", "title": "\u5982\u4f55\u8bbe\u8ba1\u4e00\u4e2a localStorage\uff0c\u4fdd\u8bc1\u6570\u636e\u7684\u5b9e\u6548\u6027"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6709\u8d5e", "\u7f51\u6613", "\u4e58\u6cd5\u4e91"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 6, "qid": 427, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 427, "subject": "", "title": "\u5b9e\u73b0 sum \u65b9\u6cd5\uff0c\u4f7f sum(x)(y),sum(x,y)\u8fd4\u56de\u7684\u7ed3\u679c\u76f8\u540c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6709\u8d5e"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 428, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 428, "subject": "", "title": "\u4e24\u4e2a\u5bf9\u8c61\u5982\u4f55\u6bd4\u8f83"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6316\u8d22"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 430, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 430, "subject": "", "title": "\u4ecb\u7ecd dom \u6811\u5bf9\u6bd4"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6316\u8d22"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 431, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 431, "subject": "", "title": "\u5982\u4f55\u8bbe\u8ba1\u72b6\u6001\u6811"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6caa\u6c5f"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 441, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 441, "subject": "", "title": "Ajax \u53d1\u751f\u8de8\u57df\u8981\u8bbe\u7f6e\u4ec0\u4e48\uff08\u524d\u7aef\uff09"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6caa\u6c5f"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 442, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 442, "subject": "", "title": "\u52a0\u4e0a CORS \u4e4b\u540e\u4ece\u53d1\u8d77\u5230\u8bf7\u6c42\u6b63\u5f0f\u6210\u529f\u7684\u8fc7\u7a0b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6caa\u6c5f", "\u9177\u72d7"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 448, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 448, "subject": "", "title": "JavaScript \u53d8\u91cf\u7c7b\u578b\u5206\u4e3a\u51e0\u79cd\uff0c\u533a\u522b\u662f\u4ec0\u4e48"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u997f\u4e86\u4e48"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 456, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 456, "subject": "", "title": "ES5 \u548c ES6 \u6709\u4ec0\u4e48\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u997f\u4e86\u4e48"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 457, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 457, "subject": "", "title": "\u53d6\u6570\u7ec4\u7684\u6700\u5927\u503c\uff08ES5\u3001ES6\uff09"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u997f\u4e86\u4e48"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 458, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 458, "subject": "", "title": "some\u3001every\u3001find\u3001filter\u3001map\u3001forEach \u6709\u4ec0\u4e48\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u997f\u4e86\u4e48"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 462, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 462, "subject": "", "title": "\u9875\u9762\u4e0a\u6709\u4e00\u4e2a input\uff0c\u8fd8\u6709\u4e00\u4e2a p \u6807\u7b7e\uff0c\u6539\u53d8 input \u540e p \u6807\u7b7e\u5c31\u8ddf\u7740\u53d8\u5316\uff0c\u5982\u4f55\u5904\u7406\uff1f\u76d1\u542c input \u7684\u54ea\u4e2a\u4e8b\u4ef6\uff0c\u5728\u4ec0\u4e48\u65f6\u5019\u89e6\u53d1\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u643a\u7a0b", "\u6d77\u98ce\u6559\u80b2"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 6, "qid": 464, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 464, "subject": "", "title": "Promise \u548c async/await\uff0c\u548c Callback \u6709\u4ec0\u4e48\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u559c\u9a6c\u62c9\u96c5"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 470, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 470, "subject": "", "title": "\u9879\u76ee\u4e2d\u5bf9\u4e8e\u7528\u6237\u4f53\u9a8c\u505a\u8fc7\u4ec0\u4e48\u4f18\u5316"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u559c\u9a6c\u62c9\u96c5"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 473, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 473, "subject": "", "title": "RESTful \u5e38\u7528\u7684 Method"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5151\u5427", "\u7f51\u6613", "\u5fc3\u5a31"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 479, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 479, "subject": "", "title": "\u5982\u4f55\u5b9e\u73b0 H5 \u624b\u673a\u7aef\u7684\u9002\u914d"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5151\u5427"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 483, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 483, "subject": "", "title": "\u5982\u4f55\u53bb\u9664 url \u4e2d\u7684#\u53f7"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5151\u5427"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 486, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 486, "subject": "", "title": "\u4ecb\u7ecd webp \u8fd9\u4e2a\u56fe\u7247\u6587\u4ef6\u683c\u5f0f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5151\u5427"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 489, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 489, "subject": "", "title": "ajax \u5982\u4f55\u5904\u7406\u8de8\u57df\uff1fCORSr \u5982\u4f55\u8bbe\u7f6e\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6caa\u6c5f"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 444, "qtype": "short_answer", "short_answer": {"analysis": "\n#### 1.\u53ef\u4ee5\u4f7f\u4fe9\u4e2a\u8bf7\u6c42\u5e76\u884c\u5904\u7406\n\n```js\n//\u4e32\u884c\u8bf7\u6c42\nconst showColumnInfo = async (id) => {\n console.time('showColumnInfo')\n const Promise1 = await getColumn('awaitPromise1')\n const Promise2 = await getColumn('awaitPromise2')\n console.log(`name:${Promise1.name}`)\n console.log(`description:${Promise1.description}`)\n console.log(`name:${Promise2.name}`)\n console.log(`description:${Promise2.description}`)\n console.timeEnd('showColumnInfo') // 4349.122ms\n}\nshowColumnInfo()\n\n//\u5e76\u884c\u8bf7\u6c42\nconst showColumnInfo = async (id) => {\n console.time('showColumnInfo')\n const Promise1 = getColumn('awaitPromise1')\n const Promise2 = getColumn('awaitPromise2')\n\n const awaitPromise1 = await Promise1\n const awaitPromise2 = await Promise2\n console.log(`name:${awaitPromise1.name}`)\n console.log(`description:${awaitPromise1.description}`)\n console.log(`name:${awaitPromise2.name}`)\n console.log(`description:${awaitPromise2.description}`)\n console.timeEnd('showColumnInfo') // 2526.615ms\n}\nshowColumnInfo()\n```\n\n#### 2.\u4f7f\u7528Promise.all()\u8ba9\u591a\u4e2aawait\u64cd\u4f5c\u5e76\u884c\n\n```js\nconst showColumnInfo = async (id) => {\n console.time('showColumnInfo')\n const [Promise1, Promise2] = await Promise.all([\n getZhihuColumn('Promise1'),\n getZhihuColumn('Promise2')\n ])\n\n console.log(`name:${qianduanzPromise1hidian.name}`)\n console.log(`description:${Promise1.description}`)\n\n console.log(`name:${Promise2.name}`)\n console.log(`description:${Promise2.description}`)\n\n console.timeEnd('showColumnInfo')\n}\nshowColumnInfo() // 2630.869ms\n```", "answer": "", "biz_type": 1, "qid": 444, "subject": "", "title": "Async \u91cc\u9762\u6709\u591a\u4e2a await \u8bf7\u6c42\uff0c\u53ef\u4ee5\u600e\u4e48\u4f18\u5316"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613", "\u6d77\u98ce\u6559\u80b2"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 365, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u89c2\u5bdf\u8005\u6a21\u5f0f\n\n#### 1.\u5b9a\u4e49\n\n\u89c2\u5bdf\u8005\u6a21\u5f0f\u5b9a\u4e49\u4e86\u5bf9\u8c61\u4e4b\u95f4\u7684\u4e00\u5bf9\u591a\u4f9d\u8d56\uff0c\u8fd9\u6837\u4e00\u6765\uff0c\u5f53\u4e00\u4e2a\u5bf9\u8c61\u6539\u53d8\u72b6\u6001\u65f6\uff0c\u5b83\u7684\u6240\u6709\u4f9d\u8d56\u8005\u90fd\u4f1a\u6536\u5230\u901a\u77e5\u5e76\u81ea\u52a8\u66f4\u65b0\n\n#### 2.\u4e3b\u8981\u89e3\u51b3\u7684\u95ee\u9898\n\n\u4e00\u4e2a\u5bf9\u8c61\u72b6\u6001\u6539\u53d8\u7ed9\u5176\u4ed6\u5bf9\u8c61\u901a\u77e5\u7684\u95ee\u9898\uff0c\u800c\u4e14\u8981\u8003\u8651\u5230\u6613\u7528\u548c\u4f4e\u8026\u5408\uff0c\u4fdd\u8bc1\u9ad8\u5ea6\u7684\u534f\u4f5c\u3002\n\n#### 3.\u4f18\u70b9\n\n1. \u89c2\u5bdf\u8005\u548c\u88ab\u89c2\u5bdf\u8005\u662f\u62bd\u8c61\u8026\u5408\u7684\u3002 \n2. \u5efa\u7acb\u4e00\u5957\u89e6\u53d1\u673a\u5236\u3002\n\n#### 4.\u7f3a\u70b9\n\n1. \u5982\u679c\u4e00\u4e2a\u88ab\u89c2\u5bdf\u8005\u5bf9\u8c61\u6709\u5f88\u591a\u7684\u76f4\u63a5\u548c\u95f4\u63a5\u7684\u89c2\u5bdf\u8005\u7684\u8bdd\uff0c\u5c06\u6240\u6709\u7684\u89c2\u5bdf\u8005\u90fd\u901a\u77e5\u5230\u4f1a\u82b1\u8d39\u5f88\u591a\u65f6\u95f4\u3002 \n2. \u5982\u679c\u5728\u89c2\u5bdf\u8005\u548c\u89c2\u5bdf\u76ee\u6807\u4e4b\u95f4\u6709\u5faa\u73af\u4f9d\u8d56\u7684\u8bdd\uff0c\u89c2\u5bdf\u76ee\u6807\u4f1a\u89e6\u53d1\u5b83\u4eec\u4e4b\u95f4\u8fdb\u884c\u5faa\u73af\u8c03\u7528\uff0c\u53ef\u80fd\u5bfc\u81f4\u7cfb\u7edf\u5d29\u6e83\u3002 \n3. \u89c2\u5bdf\u8005\u6a21\u5f0f\u6ca1\u6709\u76f8\u5e94\u7684\u673a\u5236\u8ba9\u89c2\u5bdf\u8005\u77e5\u9053\u6240\u89c2\u5bdf\u7684\u76ee\u6807\u5bf9\u8c61\u662f\u600e\u4e48\u53d1\u751f\u53d8\u5316\u7684\uff0c\u800c\u4ec5\u4ec5\u53ea\u662f\u77e5\u9053\u89c2\u5bdf\u76ee\u6807\u53d1\u751f\u4e86\u53d8\u5316\u3002", "answer": "", "biz_type": 1, "qid": 365, "subject": "", "title": "\u4ecb\u7ecd\u89c2\u5bdf\u8005\u6a21\u5f0f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 363, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u7b54\u6848\n\n\u8868\u5355\u53ef\u4ee5\u76f4\u63a5\u8de8\u57df\u8fdb\u884c\u63d0\u4ea4, \u4f46\u66f4\u65b0\u9875\u9762\u65f6, \u4e0d\u4f1a\u83b7\u53d6\u5230\u4efb\u4f55\u8de8\u57df\u9875\u9762\u7684\u4fe1\u606f;\n\najax\u5b9e\u8d28\u662f\u811a\u672c\u8fd0\u884c, \u53d7\u5230\u540c\u6e90\u7b56\u7565\u7684\u89c4\u8303;", "answer": "", "biz_type": 1, "qid": 363, "subject": "", "title": "\u8868\u5355\u53ef\u4ee5\u8de8\u57df\u5417"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 320, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u7b54\u6848\n\nnextTick \u5728\u672c\u6b21\u5faa\u73af\u6267\u884c, \u4e14\u5168\u90e8\u6267\u884c, setTimeout \u5728\u4e0b\u6b21\u5faa\u73af\u6267\u884c\n\n### \u89e3\u6790\n\n\u7531\u4e8ejs\u662f\u5355\u7ebf\u7a0b\uff0cjs\u8bbe\u8ba1\u8005\u628a\u4efb\u52a1\u5206\u4e3a\u540c\u6b65\u4efb\u52a1\u548c\u5f02\u6b65\u4efb\u52a1\uff0c\u540c\u6b65\u4efb\u52a1\u90fd\u5728\u4e3b\u7ebf\u7a0b\u4e0a\u6392\u961f\u6267\u884c\uff0c\u524d\u9762\u4efb\u52a1\u6ca1\u6709\u6267\u884c\u5b8c\u6210\uff0c\u540e\u9762\u7684\u4efb\u52a1\u4f1a\u4e00\u76f4\u7b49\u5f85\uff1b\u5f02\u6b65\u4efb\u52a1\u5219\u662f\u6302\u5728\u5728\u4e00\u4e2a\u4efb\u52a1\u961f\u5217\u91cc\uff0c\u7b49\u5f85\u4e3b\u7ebf\u7a0b\u6240\u6709\u4efb\u52a1\u6267\u884c\u5b8c\u6210\u540e\uff0c\u901a\u77e5\u4efb\u52a1\u961f\u5217\u53ef\u4ee5\u628a\u53ef\u6267\u884c\u7684\u4efb\u52a1\u653e\u5230\u4e3b\u7ebf\u7a0b\u6267\u884c\u3002\u5f02\u6b65\u4efb\u52a1\u653e\u5230\u4e3b\u7ebf\u7a0b\u6267\u884c\u5b8c\u540e\uff0c\u53c8\u901a\u77e5\u4efb\u52a1\u961f\u5217\u628a\u4e0b\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\u653e\u5230\u4e3b\u7ebf\u7a0b\u4e2d\u6267\u884c\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u4e00\u76f4\u6301\u7eed\uff0c\u76f4\u5230\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u5b8c\u6210\uff0c\u8fd9\u4e2a\u6301\u7eed\u91cd\u590d\u7684\u8fc7\u7a0b\u5c31\u53ebEvent loop\u3002\u800c\u4e00\u6b21\u5faa\u73af\u5c31\u662f\u4e00\u6b21tick \u3002\n\n\u5728\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u5f02\u6b65\u4efb\u52a1\u53c8\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cdmicrotast\uff08\u5fae\u4efb\u52a1\uff09 \u548c macrotask\uff08\u5b8f\u4efb\u52a1\uff09\n\n- microtast\uff08\u5fae\u4efb\u52a1\uff09\uff1aPromise\uff0c process.nextTick\uff0c Object.observe\uff0c MutationObserver\n- macrotask\uff08\u5b8f\u4efb\u52a1\uff09\uff1ascript\u6574\u4f53\u4ee3\u7801\u3001setTimeout\u3001 setInterval\u7b49\n\n\u6267\u884c\u4f18\u5148\u7ea7\u4e0a\uff0c\u5148\u6267\u884c\u5b8f\u4efb\u52a1macrotask\uff0c\u518d\u6267\u884c\u5fae\u4efb\u52a1mincrotask\u3002\n\n\u6267\u884c\u8fc7\u7a0b\u4e2d\u9700\u8981\u6ce8\u610f\u7684\u51e0\u70b9\u662f\uff1a\n\n- \u5728\u4e00\u6b21event loop\u4e2d\uff0cmicrotask\u5728\u8fd9\u4e00\u6b21\u5faa\u73af\u4e2d\u662f\u4e00\u76f4\u53d6\u4e00\u76f4\u53d6\uff0c\u76f4\u5230\u6e05\u7a7amicrotask\u961f\u5217\uff0c\u800cmacrotask\u5219\u662f\u4e00\u6b21\u5faa\u73af\u53d6\u4e00\u6b21\u3002\n- \u5982\u679c\u6267\u884c\u4e8b\u4ef6\u5faa\u73af\u7684\u8fc7\u7a0b\u4e2d\u53c8\u52a0\u5165\u4e86\u5f02\u6b65\u4efb\u52a1\uff0c\u5982\u679c\u662fmacrotask\uff0c\u5219\u653e\u5230macrotask\u672b\u5c3e\uff0c\u7b49\u5f85\u4e0b\u4e00\u8f6e\u5faa\u73af\u518d\u6267\u884c\u3002\u5982\u679c\u662fmicrotask\uff0c\u5219\u653e\u5230\u672c\u6b21event loop\u4e2d\u7684microtask\u4efb\u52a1\u672b\u5c3e\u7ee7\u7eed\u6267\u884c\u3002\u76f4\u5230microtask\u961f\u5217\u6e05\u7a7a\u3002", "answer": "", "biz_type": 1, "qid": 320, "subject": "", "title": "nextTick \u662f\u5728\u672c\u6b21\u5faa\u73af\u6267\u884c\uff0c\u8fd8\u662f\u5728\u4e0b\u6b21\uff0csetTimeout(() => {}, 0)\u5462\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 368, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u5b9e\u73b0\u65b9\u5f0f\n\n\u5728\u4f20\u7edfweb\u670d\u52a1\u6a21\u578b\u4e2d\uff0c\u5927\u591a\u90fd\u4f7f\u7528\u591a\u7ebf\u7a0b\u6765\u89e3\u51b3\u5e76\u53d1\u7684\u95ee\u9898\uff0c\u56e0\u4e3aI/O \u662f\u963b\u585e\u7684\uff0c\u5355\u7ebf\u7a0b\u5c31\u610f\u5473\u7740\u7528\u6237\u8981\u7b49\u5f85\uff0c\u663e\u7136\u8fd9\u662f\u4e0d\u5408\u7406\u7684\uff0c\u6240\u4ee5\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\u6765\u54cd\u5e94\u7528\u6237\u7684\u8bf7\u6c42\u3002\n\n![](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-368-req1.png)\n\nJavaScript\u662f\u89e3\u6790\u6027\u8bed\u8a00\uff0c\u4ee3\u7801\u6309\u7167\u7f16\u7801\u987a\u5e8f\u4e00\u884c\u4e00\u884c\u88ab\u538b\u8fdbstack\u91cc\u9762\u6267\u884c\uff0c\u6267\u884c\u5b8c\u6210\u540e\u79fb\u9664\u7136\u540e\u7ee7\u7eed\u538b\u4e0b\u4e00\u884c\u4ee3\u7801\u5757\u8fdb\u53bb\u6267\u884c\u3002\u4e0a\u9762\u4ee3\u7801\u5757\u7684\u5806\u6808\u56fe\uff0c\u5f53\u4e3b\u7ebf\u7a0b\u63a5\u53d7\u4e86request\u540e\uff0c\u7a0b\u5e8f\u88ab\u538b\u8fdb\u540c\u6b65\u6267\u884c\u7684sleep\u6267\u884c\u5757\uff08\u6211\u4eec\u5047\u8bbe\u8fd9\u91cc\u5c31\u662f\u7a0b\u5e8f\u7684\u4e1a\u52a1\u5904\u7406\uff09\uff0c\u5982\u679c\u5728\u8fd910s\u5185\u6709\u7b2c\u4e8c\u4e2arequest\u8fdb\u6765\u5c31\u4f1a\u88ab\u538b\u8fdbstack\u91cc\u9762\u7b49\u5f8510s\u6267\u884c\u5b8c\u6210\u540e\u518d\u8fdb\u4e00\u6b65\u5904\u7406\u4e0b\u4e00\u4e2a\u8bf7\u6c42\uff0c\u540e\u9762\u7684\u8bf7\u6c42\u90fd\u4f1a\u88ab\u6302\u8d77\u7b49\u5f85\u524d\u9762\u7684\u540c\u6b65\u6267\u884c\u5b8c\u6210\u540e\u518d\u6267\u884c\u3002\n\n\u4e8b\u4ef6\u9a71\u52a8\u53ef\u4ee5\u4f7f\u5355\u7ebf\u7a0b\u7684\u6548\u7387\u9ad8\uff0c\u540c\u65f6\u5904\u7406\u6570\u4e07\u7ea7\u7684\u5e76\u53d1\u800c\u4e0d\u4f1a\u9020\u6210\u963b\u585e\u3002\n\n![](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-368-req2.png)\n\n1\u3001\u6bcf\u4e2a\u8fdb\u7a0b\u53ea\u6709\u4e00\u4e2a\u4e3b\u7ebf\u7a0b\u5728\u6267\u884c\u7a0b\u5e8f\u4ee3\u7801\uff0c\u5f62\u6210\u4e00\u4e2a\u6267\u884c\u6808\uff08execution context stack)\u3002\n\n2\u3001\u4e3b\u7ebf\u7a0b\u4e4b\u5916\uff0c\u8fd8\u7ef4\u62a4\u4e86\u4e00\u4e2a\"\u4e8b\u4ef6\u961f\u5217\"\uff08Event queue\uff09\u3002\u5f53\u7528\u6237\u7684\u7f51\u7edc\u8bf7\u6c42\u6216\u8005\u5176\u5b83\u7684\u5f02\u6b65\u64cd\u4f5c\u5230\u6765\u65f6\uff0c\u90fd\u4f1a\u628a\u5b83\u653e\u5230Event Queue\u4e4b\u4e2d\uff0c\u6b64\u65f6\u5e76\u4e0d\u4f1a\u7acb\u5373\u6267\u884c\u5b83\uff0c\u4ee3\u7801\u4e5f\u4e0d\u4f1a\u88ab\u963b\u585e\uff0c\u7ee7\u7eed\u5f80\u4e0b\u8d70\uff0c\u76f4\u5230\u4e3b\u7ebf\u7a0b\u4ee3\u7801\u6267\u884c\u5b8c\u6bd5\u3002\n\n3\u3001\u4e3b\u7ebf\u7a0b\u4ee3\u7801\u6267\u884c\u5b8c\u6bd5\u5b8c\u6210\u540e\uff0c\u7136\u540e\u901a\u8fc7Event Loop\uff0c\u4e5f\u5c31\u662f\u4e8b\u4ef6\u5faa\u73af\u673a\u5236\uff0c\u5f00\u59cb\u5230Event Queue\u7684\u5f00\u5934\u53d6\u51fa\u7b2c\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u4ece\u7ebf\u7a0b\u6c60\u4e2d\u5206\u914d\u4e00\u4e2a\u7ebf\u7a0b\u53bb\u6267\u884c\u8fd9\u4e2a\u4e8b\u4ef6\uff0c\u63a5\u4e0b\u6765\u7ee7\u7eed\u53d6\u51fa\u7b2c\u4e8c\u4e2a\u4e8b\u4ef6\uff0c\u518d\u4ece\u7ebf\u7a0b\u6c60\u4e2d\u5206\u914d\u4e00\u4e2a\u7ebf\u7a0b\u53bb\u6267\u884c\uff0c\u7136\u540e\u7b2c\u4e09\u4e2a\uff0c\u7b2c\u56db\u4e2a\u3002\u4e3b\u7ebf\u7a0b\u4e0d\u65ad\u7684\u68c0\u67e5\u4e8b\u4ef6\u961f\u5217\u4e2d\u662f\u5426\u6709\u672a\u6267\u884c\u7684\u4e8b\u4ef6\uff0c\u76f4\u5230\u4e8b\u4ef6\u961f\u5217\u4e2d\u6240\u6709\u4e8b\u4ef6\u90fd\u6267\u884c\u5b8c\u4e86\uff0c\u6b64\u540e\u6bcf\u5f53\u6709\u65b0\u7684\u4e8b\u4ef6\u52a0\u5165\u5230\u4e8b\u4ef6\u961f\u5217\u4e2d\uff0c\u90fd\u4f1a\u901a\u77e5\u4e3b\u7ebf\u7a0b\u6309\u987a\u5e8f\u53d6\u51fa\u4ea4EventLoop\u5904\u7406\u3002\u5f53\u6709\u4e8b\u4ef6\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u4f1a\u901a\u77e5\u4e3b\u7ebf\u7a0b\uff0c\u4e3b\u7ebf\u7a0b\u6267\u884c\u56de\u8c03\uff0c\u7ebf\u7a0b\u5f52\u8fd8\u7ed9\u7ebf\u7a0b\u6c60\u3002\n\n4\u3001\u4e3b\u7ebf\u7a0b\u4e0d\u65ad\u91cd\u590d\u4e0a\u9762\u7684\u7b2c\u4e09\u6b65\u3002", "answer": "", "biz_type": 1, "qid": 368, "subject": "", "title": "\u901a\u8fc7\u4ec0\u4e48\u505a\u5230\u5e76\u53d1\u8bf7\u6c42"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6316\u8d22"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 429, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u53d8\u91cf\u7684\u4f5c\u7528\u57df\u94fe\n\n\u5df2\u77e5 JavaScript \u4ee3\u7801\u6267\u884c\u4e00\u6bb5\u53ef\u6267\u884c\u4ee3\u7801\u65f6\uff0c\u4f1a\u521b\u5efa\u5bf9\u5e94\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u3002\u5728\u521b\u5efa\u6267\u884c\u4e0a\u4e0b\u6587\u65f6\uff0c\u4f1a\u521b\u5efa\u4f5c\u7528\u57df\u94fe\uff08\u56e0\u4e3a\u5f15\u7528\u4e86\u5916\u90e8\u73af\u5883\uff09\u3002\n\n\u67e5\u627e\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u4f1a\u5148\u4ece\u5f53\u524d\u4e0a\u4e0b\u6587\u7684\u73af\u5883\u8bb0\u5f55\u4e2d\u67e5\u627e\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0c\u5c31\u4f1a\u4ece\u4e0a\u7ea7\u6267\u884c\u4e0a\u4e0b\u6587\u7684\u73af\u5883\u8bb0\u5f55\u4e2d\u67e5\u627e\uff0c\u4e00\u76f4\u627e\u5230\u5168\u5c40\u4e0a\u4e0b\u6587\u7684\u73af\u5883\u8bb0\u5f55\u3002\u8fd9\u6837\u6709\u591a\u4e2a\u6267\u884c\u4e0a\u4e0b\u6587\u7684\u73af\u5883\u8bb0\u5f55\u6784\u6210\u7684\u94fe\u8868\uff0c\u5c31\u53eb\u505a\u4f5c\u7528\u57df\u94fe\u3002\n\n\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u533a\u522b\u4e8ethis\u6307\u9488\uff0c\u53d8\u91cf\u4f5c\u7528\u57df\u662f\u9759\u6001\u7684\uff0c\u5728\u53d8\u91cf\u58f0\u660e\u540e\u5c31\u786e\u5b9a\u7684\uff0c\u800cthis\u5219\u662f\u52a8\u6001\u7684\uff0c\u6839\u636e\u6700\u540e\u7684\u8c03\u7528\u60c5\u51b5\u5224\u65ad\uff1b\n\n\u5728js\u4e2d\uff0c\u672f\u8bed\u201c\u5168\u5c40\u53d8\u91cf\u201d\u6307\u7684\u662f\u5b9a\u4e49\u5728\u6240\u6709\u51fd\u6570\u4e4b\u5916\u7684\u53d8\u91cf\uff08\u4e5f\u5c31\u662f\u5b9a\u4e49\u5728\u5168\u5c40\u4ee3\u7801\u4e2d\u7684\u53d8\u91cf\uff09\uff0c\u4e0e\u4e4b\u76f8\u5bf9\u7684\u662f\u201c\u5c40\u90e8\u53d8\u91cf\u201d\uff0c\u6240\u6307\u7684\u662f\u5728\u67d0\u4e2a\u51fd\u6570\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u3002\u5176\u4e2d\uff0c\u51fd\u6570\u5185\u7684\u4ee3\u7801\u53ef\u4ee5\u8bbf\u95ee\u81ea\u5df1\u4e0a\u5c42\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u4e5f\u53ef\u4ee5\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\uff0c\u8fd9\u6837\u5c31\u6784\u6210\u4e86\u4f5c\u7528\u57df\u94fe\u3002\u53e6\u5916\uff0c\u9690\u5f0f\u58f0\u660e\uff08\u6ca1\u6709\u4f7f\u7528var\u7b49\u8bed\u53e5\uff09\u4e00\u4e2a\u53d8\u91cf\uff0c\u8be5\u53d8\u91cf\u5c31\u4f1a\u88ab\u9ed8\u8ba4\u4e3a\u5168\u5c40\u53d8\u91cf\u3002\n\n### \u6df1\u5165\u4e00\u4e9b\n\n\u4f5c\u7528\u57df\u94fe\u5305\u542b\u4e86\u6267\u884c\u73af\u5883\u6709\u6743\u8bbf\u95ee\u7684\u53d8\u91cf\u3001\u51fd\u6570\u7684\u6709\u5e8f\u8bbf\u95ee\u3002\u5b83\u662f\u4e00\u4e2a\u7531\u53d8\u91cf\u5bf9\u8c61(VO/AO)\u7ec4\u6210\u7684\u5355\u5411\u94fe\u8868\uff0c\u4e3b\u8981\u7528\u6765\u8fdb\u884c\u53d8\u91cf\u67e5\u627e\u3002\n\nJS\u5185\u90e8\u6709\u4e00\u4e2a[[scope]]\u5c5e\u6027\uff0c\u8fd9\u4e2a\u5c5e\u6027\u5c31\u662f\u6307\u5411\u4f5c\u7528\u57df\u94fe\u7684\u9876\u7aef\u3002\n\n```js\nvar val='\u5168\u5c40\u53d8\u91cf'\nfunction handle(y){\n var val='\u5c40\u90e8\u53d8\u91cf';\n function s(){\n var z=0;\n alert(val);\n }\n s();\n}\nhandle(5);\n```\n\n\u5206\u6790\u4e0a\u9762\u7684\u4ee3\u7801\u7684 \u4f5c\u7528\u57df\u94fe\uff1a\n\n- \u5168\u5c40\u6267\u884c\u73af\u5883\uff1a`[[scope]]----->VO[handle,val]` \u53ea\u6709\u5168\u5c40VO\uff0c`[[scope]]`\u76f4\u63a5\u6307\u5411VO\u3002\n- \u51fd\u6570AA\u6267\u884c\u73af\u5883\uff1a`[[scope]]---->VO[[y,s,val]VO[[handle,val]]`\uff0c\u9996\u5148\u5168\u5c40VO\u538b\u5165\u6808\uff0c\u7136\u540e\u51fd\u6570AA VO\u538b\u5165\u6808\u9876\uff0c`[[scope]]`\u5c5e\u6027\u6307\u5411\u6808\u9876\uff0c\u53d8\u91cf\u3001\u51fd\u6570\u641c\u7d22\u5c31\u4ece\u6808\u9876\u5f00\u59cb\u3002\n- \u51fd\u6570s\u6267\u884c\u73af\u5883:`[[scope]]--->VO[[z]]VO[[y,s,val]VO[[handle,val]]`\uff0c\u9996\u5148\u5168\u5c40VO\u538b\u5165\u6808\uff0c\u7136\u540e\u4f9d\u6b21AA\uff0cs\u538b\u5165\u6808\uff0cs\u5904\u4e8e\u6808\u9876\uff0c`[[scope]]`\u5c5e\u6027\u76f4\u63a5\u6307\u5411s\u7684VO\u3002\n- \u5e94\u7528\uff1a\u6bd4\u5982\u8c03\u7528s\uff0c\u8fdb\u5165s\u6267\u884c\u73af\u5883\uff0c\u5728\u6267\u884calert\u65f6\uff0c\u9996\u5148\u4f1a\u53bb\u67e5\u627ebb\u7684\u7533\u660e\uff0c\u4f1a\u5148\u5728\u4f5c\u7528\u57df\u94fe\u7684\u9876\u7aef\u67e5\u627e\uff0c\u6ca1\u67e5\u5230\u5c31\u4f1a\u6cbf\u7740\u94fe\u7ee7\u7eed\u5f80\u4e0b\u67e5\u627e\uff0c\u76f4\u5230\u67e5\u5230\u5c31\u505c\u6b62\u3002\n\n**\u603b\u7ed3\uff1a**\n\n\u51fd\u6570\u6267\u884c\u65f6\uff0c\u5c06\u5f53\u524d\u7684\u51fd\u6570\u7684VO\u653e\u5728\u94fe\u8868\u5f00\u5934\uff0c\u540e\u9762\u4f9d\u6b21\u662f\u4e0a\u5c42\u51fd\u6570\uff0c\u6700\u540e\u662f\u5168\u5c40\u5bf9\u8c61\u3002\u53d8\u91cf\u67e5\u627e\u5219\u4f9d\u6b21\u4ece\u94fe\u8868\u7684\u9876\u7aef\u5f00\u59cb\u3002JS\u6709\u4e2a\u5185\u90e8\u5c5e\u6027`[[scope]]`\uff0c\u8fd9\u4e2a\u5c5e\u6027\u5305\u542b\u4e86\u51fd\u6570\u7684\u4f5c\u7528\u57df\u5bf9\u8c61\u7684\u96c6\u5408\uff0c\u8fd9\u4e2a\u96c6\u5408\u5c31\u79f0\u4e3a\u51fd\u6570\u7684\u4f5c\u7528\u57df\u94fe\u3002\u5b83\u51b3\u5b9a\u4e86\uff0c\u54ea\u4e9b\u53d8\u91cf\u6216\u8005\u51fd\u6570\u80fd\u5728\u5f53\u524d\u51fd\u6570\u4e2d\u88ab\u8bbf\u95ee\uff0c\u4ee5\u53ca\u5b83\u7684\u8bbf\u95ee\u987a\u5e8f\u3002", "answer": "", "biz_type": 1, "qid": 429, "subject": "", "title": "\u8bf4\u4e00\u4e0b\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u94fe"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613", "\u6709\u8d5e", "\u5fae\u533b"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 6, "qid": 367, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u533a\u522b\n\n\u8ba2\u9605-\u53d1\u5e03\u662f\u89c2\u5bdf\u8005\u6a21\u5f0f\u7684\u4e00\u4e2a\u53d8\u79cd\n\n\u8ba2\u9605-\u53d1\u5e03\uff0c\u89c2\u5bdf\u8005\u53ea\u6709\u8ba2\u9605\u4e86\u624d\u80fd\u63a5\u53d7\u5230\u88ab\u89c2\u5bdf\u8005\u7684\u6d88\u606f\uff0c\u540c\u65f6\u89c2\u5bdf\u8005\u8fd8\u53ef\u4ee5\u53d6\u6d88\u63a5\u53d7\u88ab\u89c2\u5bdf\u8005\u7684\u6d88\u606f\uff0c\u4e5f\u5c31\u662f\u8bf4\u5728\u89c2\u5bdf\u8005\u548c\u88ab\u89c2\u5bdf\u8005\u4e4b\u95f4\u5b58\u5728\u4e00\u4e2a\u7ecf\u7eaa\u4ebaBroker\u6765\u7ba1\u7406\u89c2\u5bdf\u8005\u548c\u88ab\u89c2\u5bdf\u8005\u3002\n\n\u4ece\u8868\u9762\u4e0a\u770b\uff1a\n\n1. \u89c2\u5bdf\u8005\u6a21\u5f0f\u91cc\uff0c\u53ea\u6709\u4e24\u4e2a\u89d2\u8272 \u2014\u2014 \u89c2\u5bdf\u8005 + \u88ab\u89c2\u5bdf\u8005\n2. \u800c\u53d1\u5e03\u8ba2\u9605\u6a21\u5f0f\u91cc\uff0c\u5374\u4e0d\u4ec5\u4ec5\u53ea\u6709\u53d1\u5e03\u8005\u548c\u8ba2\u9605\u8005\u4e24\u4e2a\u89d2\u8272\uff0c\u8fd8\u6709\u4e00\u4e2a\u7ecf\u5e38\u88ab\u6211\u4eec\u5ffd\u7565\u7684 \u2014\u2014 \u7ecf\u7eaa\u4ebaBroker\n\n\u5f80\u66f4\u6df1\u5c42\u6b21\u8bb2\uff1a\n\n1. \u89c2\u5bdf\u8005\u548c\u88ab\u89c2\u5bdf\u8005\uff0c\u662f\u677e\u8026\u5408\u7684\u5173\u7cfb\n2. \u53d1\u5e03\u8005\u548c\u8ba2\u9605\u8005\uff0c\u5219\u5b8c\u5168\u4e0d\u5b58\u5728\u8026\u5408\n\n\u4ece\u4f7f\u7528\u5c42\u9762\u4e0a\u8bb2\uff1a \n\n1. \u89c2\u5bdf\u8005\u6a21\u5f0f\uff0c\u591a\u7528\u4e8e\u5355\u4e2a\u5e94\u7528\u5185\u90e8\n2. \u53d1\u5e03\u8ba2\u9605\u6a21\u5f0f\uff0c\u5219\u66f4\u591a\u7684\u662f\u4e00\u79cd\u8de8\u5e94\u7528\u7684\u6a21\u5f0f(cross-application pattern)\uff0c\u6bd4\u5982\u6d88\u606f\u4e2d\u95f4\u4ef6\n\n### \u5176\u4ed6\u8bf4\u660e\n\n\u53d1\u5e03-\u8ba2\u9605\u6a21\u5f0f\u662f\u524d\u7aef\u5e38\u7528\u7684\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u73b0\u5728\u4e3b\u6d41\u7684MVVM\u6846\u67b6\uff0c\u90fd\u5927\u91cf\u4f7f\u7528\u4e86\u6b64\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5176\u4e3b\u8981\u4f5c\u7528\u6709\u4ee5\u4e0b\u4e24\u70b9\uff1a\n\n- \u4e00\u662f\u53ef\u4ee5\u5b9e\u73b0\u6a21\u5757\u95f4\u901a\u4fe1\n- \u4e8c\u662f\u53ef\u4ee5\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\u3002\n\n\u524d\u7aef\u7684\u4e8b\u4ef6\u7ed1\u5b9a\u6709\u4e09\u8981\u7d20\uff1a\n\n- \u4e00\u662f\u4f20\u5165\u4e8b\u4ef6\u7c7b\u578b\n- \u4e8c\u662f\u58f0\u660e\u5bf9\u5e94\u7684\u56de\u8c03\u65b9\u6cd5\n- \u4e09\u662f\u89e6\u53d1\u6761\u4ef6\uff1b\u89e6\u53d1\u6761\u4ef6\u4e3a\u5bf9\u5e94\u7684\u4e8b\u4ef6\u7c7b\u578b\u3002\u524d\u7aefDOM\u7684\u4e8b\u4ef6\u7cfb\u7edf\u672c\u8d28\u4e5f\u662f\u53d1\u5e03-\u8ba2\u9605\u6a21\u5f0f\uff0c\u800c\u6211\u4eec\u5728\u4e1a\u52a1\u5904\u7406\u4e2d\u6240\u5e94\u6709\u7684\u6a21\u5f0f\u4e5f\u4e0e\u6b64\u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u53d1\u5e03\u8ba2\u9605\u6a21\u5f0f\u5e94\u7528\u7684\u662f\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u7c7b\u578b\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u3002", "answer": "", "biz_type": 1, "qid": 367, "subject": "", "title": "\u89c2\u5bdf\u8005\u548c\u8ba2\u9605-\u53d1\u5e03\u7684\u533a\u522b\uff0c\u5404\u81ea\u7528\u5728\u54ea\u91cc"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 331, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n```js\nvar str1 = '<div class=\"div\">51515<p class=\"odd\" id=\"odd\">123</p>123456</div>';\n\nvar regHtml =/<\\s*\\/?\\s*[a-zA-z_]([^>]*?[\"][^\"]*[\"])*[^>\"]*>/g;\n\nconsole.log('test html <>', str1.match(regHtml));\n```", "answer": "", "biz_type": 1, "qid": 331, "subject": "", "title": "\u5199\u4e00\u4e2a\u5339\u914d Html \u6807\u7b7e\u7684\u6b63\u5219"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 4, "qid": 330, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n```js\nvar regIp = /^((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}$/;\n\nconsole.log('test 192.168.4.154', regIp.test('192.168.4.154'));\n```", "answer": "", "biz_type": 1, "qid": 330, "subject": "", "title": "\u5199\u4e00\u4e2a\u5339\u914d ip \u5730\u5740\u7684\u6b63\u5219"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 9, "qid": 312, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n```js\nArray.prototype.myReduce = function(fn, initialValue) {\n if (this.length === 0) {\n if (initialValue === undefined) {\n console.error(\"reduce of empty array with no initialValue\")\n } else {\n return initialValue\n }\n } else {\n var prev = initialValue !== undefined ? initialValue : this[0]\n var startIndex = initialValue !== undefined ? 0 : 1\n for (var i = startIndex; i < this.length; i++) {\n prev = fn(prev, this[i])\n }\n return prev\n }\n}\nvar arr1 = [1, 2, 3, 4]\nvar res = arr1.myReduce(function (sum, item) {\n return sum + item}, 1)\nconsole.log(res);\n```", "answer": "", "biz_type": 1, "qid": 312, "subject": "", "title": "reduce \u51fd\u6570\u7684\u529f\u80fd\uff0c\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u52a8\u624b\u5b9e\u73b0\u4e00\u4e0b"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 6, "qid": 403, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4e00\u3001\u7406\u89e3\n\nasync \u662fGenerator\u51fd\u6570\u7684\u8bed\u6cd5\u7cd6\uff0c\u5e76\u5bf9Generator\u51fd\u6570\u8fdb\u884c\u4e86\u6539\u8fdb\n\n**async:** \u58f0\u660e\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\uff0c\u81ea\u52a8\u5c06\u5e38\u89c4\u51fd\u6570\u8f6c\u6362\u6210promise\uff0c\u8fd4\u56de\u503c\u4e5f\u662f\u4e00\u4e2apromise\u5bf9\u8c61\uff0c\u53ea\u6709async\u51fd\u6570\u5185\u90e8\u7684\u5f02\u6b65\u64cd\u4f5c\u6267\u884c\u5b8c\uff0c\u624d\u4f1a\u6267\u884cthen\u65b9\u6cd5\u6307\u5b9a\u7684\u56de\u8c03\u51fd\u6570\uff0c\u5185\u90e8\u53ef\u4ee5\u4f7f\u7528await\uff1b\n\n**await:** \u6682\u505c\u5f02\u6b65\u7684\u529f\u80fd\u6267\u884c\uff0c\u653e\u5728promise\u8c03\u7528\u4e4b\u524d\uff0cawait\u5f3a\u5236\u5176\u4ed6\u4ee3\u7801\u7b49\u5f85\uff0c\u76f4\u5230promise\u5b8c\u6210\u5e76\u8fd4\u56de\u7ed3\u679c\uff0c\u53ea\u80fd\u4e0epromise\u4e00\u8d77\u4f7f\u7528\uff0c\u4e0d\u9002\u7528\u4e8e\u56de\u8c03\uff0c\u53ea\u80fd\u5728async\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u3002\n\n**1\uff09\u66f4\u597d\u7684\u8bed\u4e49\u5316**\n\nasync \u548c await, \u6bd4\u8d77\u661f\u53f7\u548cyield\uff0c\u8bed\u4e49\u66f4\u52a0\u6e05\u695a\uff0casync\u8868\u793a\u51fd\u6570\u91cc\u9762\u6709\u5f02\u6b65\u64cd\u4f5c\uff0cawait\u8868\u793a\u7d27\u8ddf\u5728\u540e\u9762\u7684\u8868\u8fbe\u5f0f\u9700\u8981\u7b49\u5f85\u7ed3\u679c\u3002\n\n**2\uff09\u66f4\u5e7f\u7684\u9002\u7528\u6027**\n\nco\u6a21\u5757\u7ea6\u5b9a\uff0cyield\u547d\u4ee4\u540e\u9762\u53ea\u80fd\u662fThunk\u51fd\u6570\u6216\u8005Promise\u5bf9\u8c61\uff0c \u800casync\u51fd\u6570\u7684await\u540e\u9762\uff0c\u53ef\u4ee5\u662fPromise\u548c\u539f\u59cb\u7c7b\u578b\u503c(\u6570\u503c\u3001\u5b57\u7b26\u4e32\u548c\u5e03\u5c14\u503c\uff0c\u4f46\u8fd9\u65f6\u4f1a\u81ea\u52a8\u8f6c\u6210\u7acb\u5373 resolved \u7684 Promise \u5bf9\u8c61, \u67e5\u770bspawn\u51fd\u6570\u4e2dPromise.resolve(next.value))\n\n**3\uff09\u8fd4\u56de\u503c\u662fPromise**\n\n\u6bd4Generator\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662fIterator\u5bf9\u8c61\u65b9\u4fbf\uff0c\u53ef\u4ee5\u4f7f\u7528then\u65b9\u6cd5\u6307\u5b9a\u4e0b\u4e00\u6b65\u64cd\u4f5c\n\n\n### \u4e8c\u3001\u539f\u7406\n\nAsync\u51fd\u6570\u7684\u5b9e\u73b0\u539f\u7406\u5c31\u662f\u5c06Generator\u51fd\u6570\u548c\u81ea\u52a8\u6267\u884c\u5668\u5305\u88c5\u5728\u4e00\u4e2a\u51fd\u6570\u91cc\u3002\n\n\u4e0e generator \u76f8\u6bd4\uff0c\u591a\u4e86\u4ee5\u4e0b\u51e0\u4e2a\u7279\u6027\uff1a\n\n- \u5185\u7f6e\u6267\u884c\u5668\uff0c\u65e0\u9700\u624b\u52a8\u6267\u884c next() \u65b9\u6cd5\n- await \u540e\u9762\u7684\u51fd\u6570\u53ef\u4ee5\u662f promise \u5bf9\u8c61\u4e5f\u53ef\u4ee5\u662f\u666e\u901a function\uff0c\u800c yield \u5173\u952e\u5b57\u540e\u9762\u5fc5\u987b\u5f97\u662f thunk \u51fd\u6570\u6216 promise \u5bf9\u8c61\n\n```js\nasync function fn(args) {\n // ...\n}\n\n// \u7b49\u540c\u4e8e\n\nfunction fn(args) {\n return spawn(function* () {\n // ...\n });\n}\n```\n\n\u800c spawn \u51fd\u6570\u5c31\u662f\u6240\u8c13\u7684\u81ea\u52a8\u6267\u884c\u5668\u4e86\n\n```js\nfunction spawn(genF){\n return new Promise((resolve, reject)=>{\n const gen = genF() // \u5148\u5c06Generator\u51fd\u6570\u6267\u884c\u4e0b\uff0c\u62ff\u5230\u904d\u5386\u5668\u5bf9\u8c61\n function step(nextF) {\n let next\n try {\n next = nextF()\n } catch(e){\n return reject(e)\n }\n if(next.done){\n return resolve(next.value)\n }\n Promise.resolve(next.value).then((v)=>{\n step(()=>{return gen.next(v)})\n }, (e)=>{\n step(()=>{return gen.throw(e)})\n })\n }\n step(()=> {return gen.next(undefinex)})\n })\n}\n\n```\n", "answer": "", "biz_type": 1, "qid": 403, "subject": "", "title": "\u5bf9 async\u3001await \u7684\u7406\u89e3\uff0c\u5185\u90e8\u539f\u7406\u662f\u600e\u6837\u7684\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5151\u5427"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 9, "qid": 478, "qtype": "short_answer", "short_answer": {"analysis": "\n### new\u505a\u4e86\u4ec0\u4e48\n\nnew \u8fd0\u7b97\u7b26\u521b\u5efa\u4e00\u4e2a\u7528\u6237\u5b9a\u4e49\u7684\u5bf9\u8c61\u7c7b\u578b\u7684\u5b9e\u4f8b\u6216\u5177\u6709\u6784\u9020\u51fd\u6570\u7684\u5185\u7f6e\u5bf9\u8c61\u7684\u5b9e\u4f8b\u3002new \u5173\u952e\u5b57\u4f1a\u8fdb\u884c\u5982\u4e0b\u7684\u64cd\u4f5c\uff1a\n\n- \u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u7b80\u5355JavaScript\u5bf9\u8c61\uff08\u5373{}\uff09\uff1b\n- \u94fe\u63a5\u8be5\u5bf9\u8c61\uff08\u5373\u8bbe\u7f6e\u8be5\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\uff09\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61 \uff1b\n- \u5c06\u6b65\u9aa41\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u4f5c\u4e3athis\u7684\u4e0a\u4e0b\u6587 \uff1b\n- \u5982\u679c\u8be5\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u5bf9\u8c61\uff0c\u5219\u8fd4\u56dethis\u3002\n\n### \u4ee3\u7801\u5b9e\u73b0\n\n#### 1.\u7b80\u5355\u5b9e\u73b0\n\n```js\nfunction newOperator(ctor) {\n if (typeof ctor !== 'function'){\n throw 'newOperator function the first param must be a function';\n }\n var args = Array.prototype.slice.call(arguments, 1);\n // 1.\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u7b80\u5355JavaScript\u5bf9\u8c61\uff08\u5373{}\uff09\n var obj = {};\n // 2.\u94fe\u63a5\u8be5\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\uff08\u5373\u8bbe\u7f6e\u8be5\u5bf9\u8c61\u7684__proto__\uff09\u5230\u8be5\u51fd\u6570\u7684\u539f\u578b\u5bf9\u8c61prototype\u4e0a\n obj.__proto__ = ctor.prototype;\n // 3.\u5c06\u6b65\u9aa41\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u4f5c\u4e3athis\u7684\u4e0a\u4e0b\u6587\n var result = ctor.apply(obj, args);\n // 4.\u5982\u679c\u8be5\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u5bf9\u8c61\uff0c\u5219\u8fd4\u56de\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\n\n var isObject = typeof result === 'object' && result !== null;\n var isFunction = typeof result === 'function';\n return isObject || isFunction ? result : obj;\n}\n\n// \u6d4b\u8bd5\nfunction company(name, address) {\n this.name = name;\n this.address = address;\n }\n \nvar company1 = newOperator(company, 'yideng', 'beijing');\nconsole.log('company1: ', company1);\n```\n\n#### 2.\u66f4\u5b8c\u6574\u7684\u5b9e\u73b0\n\n```js\n/**\n * \u6a21\u62df\u5b9e\u73b0 new \u64cd\u4f5c\u7b26\n * @param {Function} ctor [\u6784\u9020\u51fd\u6570]\n * @return {Object|Function|Regex|Date|Error} [\u8fd4\u56de\u7ed3\u679c]\n */\nfunction newOperator(ctor){\n if(typeof ctor !== 'function'){\n throw 'newOperator function the first param must be a function';\n }\n // ES6 new.target \u662f\u6307\u5411\u6784\u9020\u51fd\u6570\n newOperator.target = ctor;\n // 1.\u521b\u5efa\u4e00\u4e2a\u5168\u65b0\u7684\u5bf9\u8c61\uff0c\n // 2.\u5e76\u4e14\u6267\u884c[[Prototype]]\u94fe\u63a5\n // 4.\u901a\u8fc7`new`\u521b\u5efa\u7684\u6bcf\u4e2a\u5bf9\u8c61\u5c06\u6700\u7ec8\u88ab`[[Prototype]]`\u94fe\u63a5\u5230\u8fd9\u4e2a\u51fd\u6570\u7684`prototype`\u5bf9\u8c61\u4e0a\u3002\n var newObj = Object.create(ctor.prototype);\n // ES5 arguments\u8f6c\u6210\u6570\u7ec4 \u5f53\u7136\u4e5f\u53ef\u4ee5\u7528ES6 [...arguments], Aarry.from(arguments);\n // \u9664\u53bbctor\u6784\u9020\u51fd\u6570\u7684\u5176\u4f59\u53c2\u6570\n var argsArr = [].slice.call(arguments, 1);\n // 3.\u751f\u6210\u7684\u65b0\u5bf9\u8c61\u4f1a\u7ed1\u5b9a\u5230\u51fd\u6570\u8c03\u7528\u7684`this`\u3002\n // \u83b7\u53d6\u5230ctor\u51fd\u6570\u8fd4\u56de\u7ed3\u679c\n var ctorReturnResult = ctor.apply(newObj, argsArr);\n // \u5c0f\u7ed34 \u8fd9\u4e9b\u7c7b\u578b\u4e2d\u5408\u5e76\u8d77\u6765\u53ea\u6709Object\u548cFunction\u4e24\u79cd\u7c7b\u578b typeof null \u4e5f\u662f'object'\u6240\u4ee5\u8981\u4e0d\u7b49\u4e8enull\uff0c\u6392\u9664null\n var isObject = typeof ctorReturnResult === 'object' && ctorReturnResult !== null;\n var isFunction = typeof ctorReturnResult === 'function';\n if(isObject || isFunction){\n return ctorReturnResult;\n }\n // 5.\u5982\u679c\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u5bf9\u8c61\u7c7b\u578b`Object`(\u5305\u542b`Functoin`, `Array`, `Date`, `RegExg`, `Error`)\uff0c\u90a3\u4e48`new`\u8868\u8fbe\u5f0f\u4e2d\u7684\u51fd\u6570\u8c03\u7528\u4f1a\u81ea\u52a8\u8fd4\u56de\u8fd9\u4e2a\u65b0\u7684\u5bf9\u8c61\u3002\n return newObj;\n}\n```", "answer": "", "biz_type": 1, "qid": 478, "subject": "", "title": "new \u7684\u5b9e\u73b0\u539f\u7406\uff0c\u52a8\u624b\u5b9e\u73b0\u4e00\u4e2a new"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4", "\u6316\u8d22", "\u5b9d\u5b9d\u6811", "\u6d77\u5eb7\u5a01\u89c6"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 6, "qid": 386, "qtype": "short_answer", "short_answer": {"analysis": "\n### JavaScript \u5f02\u6b65\u89e3\u51b3\u65b9\u6848\u53d1\u5c55\u5386\u7a0b\n\n- 1.callback\n - \u4f18\u70b9\uff1a\u903b\u8f91\u7b80\u5355\u3002\n - \u7f3a\u70b9\uff1a\u6df1\u5c42\u7ea7\u4ea7\u751f\u56de\u8c03\u5730\u72f1\u3002\n- 2.Promise\n - \u4f18\u70b9\uff1a\u4e00\u65e6\u72b6\u6001\u6539\u53d8\uff0c\u5c31\u4e0d\u4f1a\u518d\u53d8\uff0c\u4efb\u4f55\u65f6\u5019\u90fd\u53ef\u4ee5\u5f97\u5230\u8fd9\u4e2a\u7ed3\u679c\uff1b\u53ef\u4ee5\u5c06\u5f02\u6b65\u64cd\u4f5c\u4ee5\u540c\u6b65\u64cd\u4f5c\u7684\u6d41\u7a0b\u8868\u8fbe\u51fa\u6765\uff0c\u907f\u514d\u4e86\u5c42\u5c42\u5d4c\u5957\u7684\u56de\u8c03\u51fd\u6570\n - \u7f3a\u70b9\uff1a\u65e0\u6cd5\u53d6\u6d88\uff1b\u5f53\u5904\u4e8e pending \u72b6\u6001\u65f6\uff0c\u65e0\u6cd5\u5f97\u77e5\u76ee\u524d\u8fdb\u5c55\u5230\u54ea\u4e00\u4e2a\u9636\u6bb5\u3002\n- 3.Generator\n - \u4f18\u70b9\uff1a\u6267\u884c\u53ef\u63a7\uff1b\u6bcf\u4e00\u6b65\u53ef\u4ee5\u4f20\u9012\u6570\u636e\uff0c\u4e5f\u53ef\u4ee5\u4f20\u9012\u5f02\u5e38\u3002\n - \u7f3a\u70b9\uff1a\u63a7\u5236\u6d41\u7a0b\u590d\u6742\uff0c\u6210\u672c\u8f83\u9ad8\u3002\n- 4.async/await\n - \u4f18\u70b9\uff1a\u4ee3\u7801\u6e05\u6670\uff0c\u4e0d\u9700\u94fe\u5f0f\u8c03\u7528\u5c31\u53ef\u4ee5\u5904\u7406\u56de\u8c03\u5730\u72f1\u7684\u95ee\u9898\uff1b\u9519\u8bef\u53ef\u4ee5\u88ab try catch\u3002\n - \u7f3a\u70b9\uff1a\u63a7\u5236\u6d41\u7a0b\u590d\u6742\uff0c\u6210\u672c\u8f83\u9ad8\u3002", "answer": "", "biz_type": 1, "qid": 386, "subject": "", "title": "JavaScript \u5f02\u6b65\u89e3\u51b3\u65b9\u6848\u7684\u53d1\u5c55\u5386\u7a0b\u4ee5\u53ca\u4f18\u7f3a\u70b9"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5151\u5427"], "date": "Fri, 19 Jun 2020 16:20:42 GMT", "favorite_num": 5, "qid": 485, "qtype": "short_answer", "short_answer": {"analysis": "\n### base64\n\n\u56fe\u7247\u7684 base64 \u7f16\u7801\u5c31\u662f\u53ef\u4ee5\u5c06\u4e00\u526f\u56fe\u7247\u6570\u636e\u7f16\u7801\u6210\u4e00\u4e32\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528\u8be5\u5b57\u7b26\u4e32\u4ee3\u66ff\u56fe\u50cf\u5730\u5740\u3002\n\n\u610f\u4e49:\u7f51\u9875\u4e0a\u7684\u6bcf\u4e00\u4e2a\u56fe\u7247\uff0c\u90fd\u662f\u9700\u8981\u6d88\u8017\u4e00\u4e2a http \u8bf7\u6c42\u4e0b\u8f7d\u800c\u6765\u7684\uff08\u6240\u6709\u624d\u6709\u4e86 csssprites \u6280\u672f\u7684\u5e94\u8fd0\u800c\u751f\uff0c\u4f46\u662f csssprites \u6709\u81ea\u8eab\u7684\u5c40\u9650\u6027\uff09\u3002\n\n\u56fe\u7247\u7684\u4e0b\u8f7d\u59cb\u7ec8\u90fd\u8981\u5411\u670d\u52a1\u5668\u53d1\u51fa\u8bf7\u6c42\uff0c\u8981\u662f\u56fe\u7247\u7684\u4e0b\u8f7d\u4e0d\u7528\u5411\u670d\u52a1\u5668\u53d1\u51fa\u8bf7\u6c42\uff0cbase64 \u53ef\u4ee5\u968f\u7740 HTML \u7684\u4e0b\u8f7d\u540c\u65f6\u4e0b\u8f7d\u5230\u672c\u5730.\u51cf\u5c11 https \u8bf7\u6c42\u3002\n\nbase64\u51cf\u5c11\u8bf7\u6c42\u6570\u91cf\uff0c\u662f\u8fd9\u51e0\u5e74\u7684\u4e00\u4e2a\u4f18\u79c0\u6027\u80fd\u5efa\u8bae\u3002\u867d\u7136\u5982\u6b64\uff0c\u4e5f\u4e0d\u662f\u8bf4\u5b83\u5c31\u6ca1\u6709\u7f3a\u9677\u3002\u4e3a\u4e86\u4f7f\u9875\u9762\u52a0\u8f7d\u66f4\u5feb\uff0c\u6211\u4eec\u5b9e\u9645\u4e0a\u53ef\u4ee5\u901a\u8fc7\u9ad8\u6548\u7684\u4f20\u8f93\u9759\u6001\u8d44\u6e90\u6765\u5b9e\u73b0\uff0c\u800c\u4e0d\u53ea\u662f\u51cf\u5c11\u51e0\u4e2a\u8bf7\u6c42\u3002\n\n\u5176\u4e2d\u4e00\u4e2a\u4ece\u51cf\u5c11\u8bf7\u6c42\u6570\u91cf\u8bde\u751f\u5e76\u88ab\u63a8\u5d07\u7684\u5b9e\u8df5\u662f\u4f7f\u7528Base64\u7f16\u7801\uff1a\u5c06\u5916\u90e8\u8d44\u6e90\uff08e.g. \u56fe\u7247\uff09\u76f4\u63a5\u5d4c\u5165\u5230\u4f7f\u7528\u5b83\u7684\u6587\u672c\uff08e.g.\u6837\u5f0f\u8868\uff09\u4e2d\u3002\u51cf\u5c11HTTP\u8bf7\u6c42\u6570\u91cf\u7684\u5173\u952e\u662f\uff0c\u6240\u6709\u8d44\u6e90\uff08\u6837\u5f0f\u8868\u6216\u56fe\u7247\uff09\u80fd\u591f\u5728\u540c\u4e00\u65f6\u95f4\u5230\u8fbe\u3002\n\n**\u7f3a\u70b9\uff1a**\n\n\u5185\u5bb9\u7f16\u7801\u540e\u7684\u4f53\u79ef\u4f1a\u53d8\u5927\uff0c\u7f16\u7801\u548c\u89e3\u7801\u9700\u8981\u989d\u5916\u7684\u5de5\u4f5c\u91cf\u3002\n\n1\u3001CRP \u7684\u963b\u585e \u4f7f\u7528 Base64 \u7684\u597d\u5904\u662f\u80fd\u591f\u51cf\u5c11\u4e00\u4e2a\u56fe\u7247\u7684 HTTP \u8bf7\u6c42\uff0c\u7136\u800c\uff0c\u4e0e\u4e4b\u540c\u65f6\u4ed8\u51fa\u7684\u4ee3\u4ef7\u5219\u662f CSS \u6587\u4ef6\u4f53\u79ef\u7684\u589e\u5927\u3002 \u800c CSS \u6587\u4ef6\u4f53\u79ef\u7684\u589e\u5927\u610f\u5473\u7740\u4ec0\u4e48\u5462\uff1f\u610f\u5473\u7740 CRP \u7684\u963b\u585e\u3002 CRP\uff08Critical Rendering Path\uff0c\u5173\u952e\u6e32\u67d3\u8def\u5f84\uff09\uff1a\u5f53\u6d4f\u89c8\u5668\u4ece\u670d\u52a1\u5668\u63a5\u6536\u5230\u4e00\u4e2a HTML \u9875\u9762\u7684\u8bf7\u6c42\u65f6\uff0c\u5230\u5c4f\u5e55\u4e0a\u6e32\u67d3\u51fa\u6765\u8981\u7ecf\u8fc7\u5f88\u591a\u4e2a\u6b65\u9aa4\u3002\u6d4f\u89c8\u5668\u5b8c\u6210\u8fd9\u4e00\u7cfb\u5217\u7684\u8fd0\u884c\uff0c\u6216\u8005\u8bf4\u6e32\u67d3\u51fa\u6765\u6211\u4eec\u5e38\u5e38\u79f0\u4e4b\u4e3a\u201c\u5173\u952e\u6e32\u67d3\u8def\u5f84\u201d\u3002 \u901a\u4fd7\u800c\u8a00\uff0c\u5c31\u662f\u56fe\u7247\u4e0d\u4f1a\u5bfc\u81f4\u5173\u952e\u6e32\u67d3\u8def\u5f84\u7684\u963b\u585e\uff0c\u800c\u8f6c\u5316\u4e3a Base64 \u7684\u56fe\u7247\u5927\u5927\u589e\u52a0\u4e86 CSS \u6587\u4ef6\u7684\u4f53\u79ef\uff0cCSS \u6587\u4ef6\u7684\u4f53\u79ef\u76f4\u63a5\u5f71\u54cd\u6e32\u67d3\uff0c\u5bfc\u81f4\u7528\u6237\u4f1a\u957f\u65f6\u95f4\u6ce8\u89c6\u7a7a\u767d\u5c4f\u5e55\u3002HTML \u548c CSS \u4f1a\u963b\u585e\u6e32\u67d3\uff0c\u800c\u56fe\u7247\u4e0d\u4f1a\u3002\n\n2\u3001\u9875\u9762\u89e3\u6790 CSS \u751f\u6210\u7684 CSSOM \u65f6\u95f4\u589e\u52a0 Base64 \u8ddf CSS \u6df7\u5728\u4e00\u8d77\uff0c\u5927\u5927\u589e\u52a0\u4e86\u6d4f\u89c8\u5668\u9700\u8981\u89e3\u6790 CSS \u6811\u7684\u8017\u65f6\u3002\u5176\u5b9e\u89e3\u6790 CSS \u6811\u7684\u8fc7\u7a0b\u662f\u5f88\u5feb\u7684\uff0c\u4e00\u822c\u5728\u51e0\u5341\u5fae\u5999\u5230\u51e0\u6beb\u79d2\u4e4b\u95f4\u3002 CSS \u5bf9\u8c61\u6a21\u578b (CSSOM)\uff1aCSSOM \u662f\u4e00\u4e2a\u5efa\u7acb\u5728 web \u9875\u9762\u4e0a\u7684 CSS \u6837\u5f0f\u7684\u6620\u5c04\uff0c\u5b83\u548c DOM \u7c7b\u4f3c\uff0c\u4f46\u662f\u53ea\u9488\u5bf9 CSS \u800c\u4e0d\u662f HTML\u3002 CSSOM \u751f\u6210\u8fc7\u7a0b\u5927\u81f4\u662f\uff0c\u89e3\u6790 HTML \uff0c\u5728\u6587\u6863\u7684 head \u90e8\u5206\u9047\u5230\u4e86\u4e00\u4e2a link \u6807\u8bb0\uff0c\u8be5\u6807\u8bb0\u5f15\u7528\u4e00\u4e2a\u5916\u90e8 CSS \u6837\u5f0f\u8868\uff0c\u4e0b\u8f7d\u8be5\u6837\u5f0f\u8868\u540e\u6839\u636e\u4e0a\u8ff0\u8fc7\u7a0b\u751f\u6210 CSSOM \u6811\u3002 \u8fd9\u91cc\u6211\u4eec\u8981\u77e5\u9053\u7684\u662f\uff0cCSSOM \u963b\u6b62\u4efb\u4f55\u4e1c\u897f\u6e32\u67d3\uff0c\uff08\u610f\u5473\u7740\u5728 CSS \u6ca1\u5904\u7406\u597d\u4e4b\u524d\u6240\u6709\u4e1c\u897f\u90fd\u4e0d\u4f1a\u5c55\u793a\uff09\uff0c\u800c\u5982\u679c CSS \u6587\u4ef6\u4e2d\u6df7\u5165\u4e86 Base64\uff0c\u90a3\u4e48\uff08\u56e0\u4e3a\u6587\u4ef6\u4f53\u79ef\u7684\u5927\u5e45\u589e\u957f\uff09\u89e3\u6790\u65f6\u95f4\u4f1a\u589e\u957f\u5230\u5341\u500d\u4ee5\u4e0a\u3002 \u800c\u4e14\uff0c\u6700\u91cd\u8981\u7684\u662f\uff0c\u589e\u52a0\u7684\u89e3\u6790\u65f6\u95f4\u5168\u90e8\u90fd\u5728\u5173\u952e\u6e32\u67d3\u8def\u5f84\u4e0a\u3002 \u6240\u4ee5\uff0c\u5f53\u6211\u4eec\u9700\u8981\u4f7f\u7528\u5230 Base64 \u6280\u672f\u7684\u65f6\uff0c\u4e00\u5b9a\u8981\u610f\u8bc6\u5230\u4e0a\u8ff0\u7684\u95ee\u9898\uff0c\u6709\u53d6\u820d\u7684\u8fdb\u884c\u4f7f\u7528\u3002", "answer": "", "biz_type": 1, "qid": 485, "subject": "", "title": "base64 \u4e3a\u4ec0\u4e48\u80fd\u63d0\u5347\u6027\u80fd\uff0c\u7f3a\u70b9"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9ad8\u5fb7"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 194, "qtype": "short_answer", "short_answer": {"analysis": "#### \u7b80\u5355\u8868\u8fbe\n\n```\n[] == ! [] -> [] == false -> [] == 0 -> '' == 0 -> 0 == 0 -> true\n```\n\n#### \u8be6\u7ec6\n\n\u2460 \u6839\u636e\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7 \uff0c\uff01 \u7684\u4f18\u5148\u7ea7\u662f\u5927\u4e8e == \u7684\uff0c\u6240\u4ee5\u5148\u4f1a\u6267\u884c ![]\n\n\uff01\u53ef\u5c06\u53d8\u91cf\u8f6c\u6362\u6210boolean\u7c7b\u578b\uff0cnull\u3001undefined\u3001NaN\u4ee5\u53ca\u7a7a\u5b57\u7b26\u4e32('')\u53d6\u53cd\u90fd\u4e3atrue\uff0c\u5176\u4f59\u90fd\u4e3afalse\u3002\n\n\u6240\u4ee5 ! [] \u8fd0\u7b97\u540e\u7684\u7ed3\u679c\u5c31\u662f false\n\n\u4e5f\u5c31\u662f [] == ! [] \u76f8\u5f53\u4e8e [] == false\n\n\u2461 \u6839\u636e\u4e0a\u9762\u63d0\u5230\u7684\u89c4\u5219\uff08\u5982\u679c\u6709\u4e00\u4e2a\u64cd\u4f5c\u6570\u662f\u5e03\u5c14\u503c\uff0c\u5219\u5728\u6bd4\u8f83\u76f8\u7b49\u6027\u4e4b\u524d\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u503c\u2014\u2014false\u8f6c\u6362\u4e3a0\uff0c\u800ctrue\u8f6c\u6362\u4e3a1\uff09\uff0c\u5219\u9700\u8981\u628a false \u8f6c\u6210 0\n\n\u4e5f\u5c31\u662f [] == ! [] \u76f8\u5f53\u4e8e [] == false \u76f8\u5f53\u4e8e [] == 0\n\n\u2462 \u6839\u636e\u4e0a\u9762\u63d0\u5230\u7684\u89c4\u5219\uff08\u5982\u679c\u4e00\u4e2a\u64cd\u4f5c\u6570\u662f\u5bf9\u8c61\uff0c\u53e6\u4e00\u4e2a\u64cd\u4f5c\u6570\u4e0d\u662f\uff0c\u5219\u8c03\u7528\u5bf9\u8c61\u7684valueOf()\u65b9\u6cd5\uff0c\u7528\u5f97\u5230\u7684\u57fa\u672c\u7c7b\u578b\u503c\u6309\u7167\u524d\u9762\u7684\u89c4\u5219\u8fdb\u884c\u6bd4\u8f83\uff0c\u5982\u679c\u5bf9\u8c61\u6ca1\u6709valueOf()\u65b9\u6cd5\uff0c\u5219\u8c03\u7528 toString()\uff09\n\n\u800c\u5bf9\u4e8e\u7a7a\u6570\u7ec4\uff0c[].toString() -> '' (\u8fd4\u56de\u7684\u662f\u7a7a\u5b57\u7b26\u4e32)\n\n\u4e5f\u5c31\u662f [] == 0 \u76f8\u5f53\u4e8e '' == 0\n\n\u2463 \u6839\u636e\u4e0a\u9762\u63d0\u5230\u7684\u89c4\u5219\uff08\u5982\u679c\u4e00\u4e2a\u64cd\u4f5c\u6570\u662f\u5b57\u7b26\u4e32\uff0c\u53e6\u4e00\u4e2a\u64cd\u4f5c\u6570\u662f\u6570\u503c\uff0c\u5728\u6bd4\u8f83\u76f8\u7b49\u6027\u4e4b\u524d\u5148\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u503c\uff09\n\nNumber('') -> \u8fd4\u56de\u7684\u662f 0\n\n\u76f8\u5f53\u4e8e 0 == 0 \u81ea\u7136\u5c31\u8fd4\u56de true\n", "answer": "", "biz_type": 1, "qid": 194, "subject": "", "title": "`[] == ![]`\u4e3a\u4ec0\u4e48"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u638c\u95e8\u4e00\u5bf9\u4e00"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 5, "qid": 262, "qtype": "short_answer", "short_answer": {"analysis": "#### \u51fd\u6570\u5f0f\u7f16\u7a0b\u63cf\u8ff0\n\n\u51fd\u6570\u5f0f\u7f16\u7a0b\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u6211\u4eec\u5e38\u89c1\u7684\u7f16\u7a0b\u8303\u5f0f\u6709\u547d\u4ee4\u5f0f\u7f16\u7a0b\u3001\u903b\u8f91\u5f0f\u7f16\u7a0b\uff0c\u5e38\u89c1\u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e5f\u662f\u4e00\u79cd\u547d\u4ee4\u5f0f\u7f16\u7a0b\u3002\n\n\u5982\u679c\u8bf4\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u601d\u7ef4\u65b9\u5f0f\uff1a\u628a\u73b0\u5b9e\u4e16\u754c\u4e2d\u7684\u4e8b\u7269\u62bd\u8c61\u6210\u7a0b\u5e8f\u4e16\u754c\u4e2d\u7684\u7c7b\u548c\u5bf9\u8c61\uff0c\u901a\u8fc7\u5c01\u88c5\u3001\u7ee7\u627f\u548c\u591a\u6001\u6765\u6f14\u793a\u4e8b\u7269\u4e8b\u4ef6\u7684\u8054\u7cfb\uff0c\n\n\u90a3\u4e48\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u601d\u7ef4\u65b9\u5f0f\u662f\uff1a\u628a\u73b0\u5b9e\u4e16\u754c\u7684\u4e8b\u7269\u548c\u4e8b\u7269\u4e4b\u95f4\u7684\u8054\u7cfb\u62bd\u8c61\u5230\u7a0b\u5e8f\u4e16\u754c\uff08\u5bf9\u8fd0\u7b97\u8fc7\u7a0b\u8fdb\u884c\u62bd\u8c61\uff09\u3002\u51fd\u6570\u7f16\u7a0b\u4e2d\u7684\u51fd\u6570\uff0c\u4e0d\u662f\u6307\u8ba1\u7b97\u673a\u4e2d\u7684\u51fd\u6570\uff0c\u800c\u662f\u6307\u6570\u5b66\u4e2d\u7684\u51fd\u6570\u3002\u4e5f\u5c31\u662f\u8bf4\u4e00\u4e2a\u51fd\u6570\u7684\u503c\u4ec5\u51b3\u5b9a\u4e8e\u51fd\u6570\u53c2\u6570\u7684\u503c\uff0c\u4e0d\u4f9d\u8d56\u5176\u4ed6\u72b6\u6001\u3002\u4e14\uff0c\u76f8\u540c\u7684\u8f93\u5165\u59cb\u7ec8\u5f97\u5230\u76f8\u540c\u7684\u8f93\u51fa\uff08\u7eaf\u51fd\u6570\uff09\u3002\u5728\u51fd\u6570\u5f0f\u8bed\u8a00\u4e2d\uff0c\u51fd\u6570\u4f5c\u4e3a\u4e00\u7b49\u516c\u6c11\uff0c\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u5b9a\u4e49\uff0c\u5728\u51fd\u6570\u5185\u6216\u51fd\u6570\u5916\uff0c\u53ef\u4ee5\u4f5c\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\uff0c\u53ef\u4ee5\u5bf9\u51fd\u6570\u8fdb\u884c\u7ec4\u5408\u3002\u7b80\u5355\u7684\u63cf\u8ff0\uff1a\u51fd\u6570\u5f0f\u7f16\u7a0b\u7528\u6765\u63cf\u8ff0\u6570\u636e\uff08\u51fd\u6570\uff09\u76f4\u63a5\u7684\u6620\u5c04 \u4f8b\u5982 x->f(\u8054\u7cfb\u3001\u6620\u5c04)->y,y=f(x)\u3002\n\n#### \u51fd\u6570\u5f0f\u7f16\u7a0b\u4f18\u70b9\n\n\u5728\u524d\u7aef\u4e2d\uff0c\u51fd\u6570\u5f0f\u7f16\u7a0b\u662f\u968f\u7740React\u7684\u6d41\u884c\u6536\u5230\u8d8a\u6765\u8d8a\u591a\u7684\u5173\u6ce8\uff0cVue3\u4e5f\u5f00\u59cb\u62e5\u62b1\u51fd\u6570\u5f0f\u7f16\u7a0b\u3002\u90a3\u4e48\u51fd\u6570\u5f0f\u7f16\u7a0b\u6709\u4ec0\u4e48\u4f18\u70b9\u5462\uff1f\u5b83\u7684\u4f18\u70b9\u5176\u5b9e\u662f\u7531\u5b83\u7684\u4e0d\u53ef\u53d8\u6027\u5e26\u6765\u7684\uff0c\u7b80\u5355\u63cf\u8ff0\u5982\u4e0b\uff1a\n\n1. \u51fd\u6570\u5f0f\u7f16\u7a0b\u53ef\u4ee5\u629b\u5f03this\n2. \u6253\u5305\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u66f4\u597d\u7684\u5229\u7528tree shaking\u8fc7\u6ee4\u65e0\u7528\u4ee3\u7801\n3. \u51fd\u6570\u4e0d\u4f9d\u8d56\u5916\u90e8\u7684\u72b6\u6001\uff0c\u4e5f\u4e0d\u4fee\u6539\u5916\u90e8\u7684\u72b6\u6001\uff0c\u51fd\u6570\u8c03\u7528\u7684\u7ed3\u679c\u4e0d\u4f9d\u8d56\u51fd\u6570\u8c03\u7528\u7684\u65f6\u95f4\u548c\u8c03\u7528\u7684\u4f4d\u7f6e\uff0c\u8fd9\u6837\u7684\u4ee3\u7801\u5bb9\u6613\u8fdb\u884c\u63a8\u7406\uff0c\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u8fd8\u53ef\u4ee5\u628a\u8fd0\u884c\u7ed3\u679c\u8fdb\u884c\u7f13\u5b58\u3002\u540c\u65f6\uff0c\u8fd8\u65b9\u4fbf\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\uff0c\u65b9\u4fbf\u5e76\u884c\u5904\u7406\n4. \u7531\u4e8e\u51fd\u6570\u5f0f\u8bed\u8a00\u662f\u9762\u5411\u6570\u5b66\u7684\u62bd\u8c61\uff0c\u66f4\u63a5\u8fd1\u4eba\u7684\u8bed\u8a00\uff0c\u800c\u4e0d\u662f\u673a\u5668\u8bed\u8a00\uff0c\u4ee3\u7801\u4f1a\u6bd4\u8f83\u7b80\u6d01\uff0c\u4e5f\u66f4\u5bb9\u6613\u7406\u89e3\n\n#### \u51fd\u6570\u5f0f\u7f16\u7a0b\u98ce\u9669\n\n\u81f3\u4e8e\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u98ce\u9669\uff0c\u5219\u662f\u7531\u4e8e\u4e0d\u53ef\u53d8\u6027\u5982\u679c\u63ba\u5165\u4e86\u53ef\u53d8\u6027\uff0c\u5c31\u5e26\u6765\u4e86\u98ce\u9669\u3002\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u7eaf\u51fd\u6570\u53d8\u5f97\u4e0d\u7eaf\u4e86\uff0c\u5982\u679c\u51fd\u6570\u4f9d\u8d56\u4e8e\u5916\u90e8\u7684\u72b6\u6001\u5c31\u65e0\u6cd5\u4fdd\u8bc1\u8f93\u51fa\u76f8\u540c\uff0c\u5c31\u4f1a\u5e26\u6765\u526f\u4f5c\u7528\n\n\u7b80\u5355\u793a\u4f8b\u5982\u4e0b\uff1a\n\n```js\nlet mini = 18\uff1b\nfunction checkAge(age){\n\treturn age>=mini\n}\n```\n\n\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0ccheckAge\u51fd\u6570\u4f9d\u8d56\u5916\u90e8\u7684mini\uff0c\u5982\u679cmini\u53d8\u621020\u6216\u8005\u522b\u7684\u6570\u5b57\uff0c\u90a3\u4e48\u76f8\u540c\u7684\u8f93\u5165\u4fbf\u4e0d\u662f\u76f8\u540c\u7684\u8f93\u51fa\u4e86\u3002\u5f53\u7136\u4e5f\u53ef\u4ee5\u5bf9\u4e0a\u9762\u7684\u51fd\u6570\u8fdb\u884c\u6539\u9020\uff0c\u4f46\u662f\u526f\u4f5c\u7528\u5176\u5b9e\u662f\u65e0\u6cd5\u5b8c\u5168\u907f\u514d\u7684\u3002\u4e00\u822c\u526f\u4f5c\u7528\u7684\u6765\u6e90\uff1a\u914d\u7f6e\u6587\u4ef6\u3001\u6570\u636e\u5e93\u3001\u83b7\u53d6\u7528\u6237\u7684\u8f93\u5165\uff0c\u7b49\u7b49\u2026\u2026\n\n\u6240\u6709\u7684\u5916\u90e8\u4ea4\u4e92\u90fd\u6709\u53ef\u80fd\u5e26\u51fa\u526f\u4f5c\u7528\uff0c\u526f\u4f5c\u7528\u4e5f\u4f7f\u5f97\u65b9\u6cd5\u901a\u7528\u6027\u4e0b\u964d\u4e0d\u9002\u5408\u6269\u5c55\u548c\u53ef\u91cd\u7528\u6027\uff0c\u540c\u65f6\u526f\u4f5c\u7528\u4f1a\u7ed9\u7a0b\u5e8f\u4e2d\u5e26\u6765\u5b89\u5168\u9690\u60a3\uff0c\u7ed9\u7a0b\u5e8f\u5e26\u6765\u4e0d\u786e\u5b9a\u6027\uff0c\u4f46\u662f\u526f\u4f5c\u7528\u4e0d\u53ef\u80fd\u5b8c\u5168\u7981\u6b62\uff0c\u53ea\u80fd\u5c3d\u529b\u63a7\u5236\u5b83\u4eec\u5728\u53ef\u63a7\u8303\u56f4\u5185\u53d1\u751f\u3002\n\n#### \u51fd\u6570\u5f0f\u7f16\u7a0b\u8bed\u8a00\u7279\u6027\n\n- \u9ad8\u9636\u51fd\u6570\n - \u6240\u8c13\u9ad8\u9636\u51fd\u6570\uff0c\u5c31\u662f\u4f20\u53c2\u4e3a\u51fd\u6570\u6216\u8005\u8fd4\u56de\u4e3a\u51fd\u6570\u7684\u51fd\u6570\u3002\n- \u67ef\u91cc\u5316\n - \u8fd9\u91cc\u91c7\u7528\u7ef4\u57fa\u767e\u79d1\u7684\u89e3\u91ca\uff1a\u67ef\u91cc\u5316\uff0c\u82f1\u8bed\uff1aCurrying\uff0c\u662f\u628a\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\u7684\u51fd\u6570\u53d8\u6362\u6210\u63a5\u53d7\u4e00\u4e2a\u5355\u4e00\u53c2\u6570\uff08\u6700\u521d\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff09\u7684\u51fd\u6570\uff0c\u5e76\u4e14\u8fd4\u56de\u63a5\u53d7\u4f59\u4e0b\u7684\u53c2\u6570\u800c\u4e14\u8fd4\u56de\u7ed3\u679c\u7684\u65b0\u51fd\u6570\u7684\u6280\u672f\u3002\n- \u95ed\u5305\n - MDN\u89e3\u91ca\uff1a\u5728JavaScript\u4e2d\uff0c\u6bcf\u5f53\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u95ed\u5305\u4fbf\u4ea7\u751f\u3002\u95ed\u5305\u662f\u5c06\u51fd\u6570\u4e0e\u5176\u5f15\u7528\u7684\u5468\u8fb9\u72b6\u6001\u7ed1\u5b9a\u5728\u4e00\u8d77\u5f62\u6210\uff08\u5c01\u88c5\uff09\u7684\u7ec4\u5408\u3002\n\n#### \u603b\u7ed3\n\n\u51fd\u6570\u5f0f\u7f16\u7a0b\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u53e6\u4e00\u79cd\u62bd\u8c61\u548c\u601d\u8003\u65b9\u5f0f\u3002\u5f53\u7136\u5728\u5904\u7406\u53ef\u53d8\u72b6\u6001\u548c\u5904\u7406IO\u7684\u65f6\u5019\uff0c\u51fd\u6570\u5f0f\u7f16\u7a0b\u867d\u7136\u53ef\u4ee5\u901a\u8fc7\u5f15\u5165\u53d8\u91cf\u6765\u89e3\u51b3\uff0c\u4f46\u662f\u5176\u5b9e\u51fd\u6570\u5f0f\u7f16\u7a0b\u5e76\u4e0d\u592a\u9002\u5408\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\u3002", "answer": "", "biz_type": 1, "qid": 262, "subject": "", "title": "\u8bb2\u4e00\u4e0b\u51fd\u6570\u5f0f\u7f16\u7a0b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5b8c\u7f8e\u4e16\u754c"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 146, "qtype": "short_answer", "short_answer": {"analysis": "#### base64\u7f16\u7801\n\n**\u672f\u8bed\u7248\u6982\u5ff5\u89e3\u91ca**\n\nBase64\u662f\u4e00\u79cd\u7f16\u7801\u65b9\u5f0f\u3002\u9009\u7528\u5927\u5c0f\u5199\u5b57\u6bcd\u30010-9\u3001+ \u548c / \u768464\u4e2a\u53ef\u6253\u5370\u5b57\u7b26\u6765\u8868\u793a\u4e8c\u8fdb\u5236\u6570\u636e\u3002\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u6bcf\u4e09\u4e2a\u5b57\u8282\u4e00\u7ec4\uff0c\u4e00\u5171\u662f 3*8=24 bit\uff0c\u5212\u4e3a 4\u7ec4\uff0c \u6bcf\u7ec4 6\u4e2abit\u3002\u5982\u679c\u8981\u7f16\u7801\u7684\u4e8c\u8fdb\u5236\u4e0d\u662f 3 \u7684\u500d\u6570\uff0c\u4f1a\u7528 `x\\00` \u5728\u672b\u5c3e\u8865\u8db3\uff0c\u7136\u540e\u5728\u7f16\u7801\u7684\u672b\u5c3e\u52a0\u4e0a 1-2 \u4e2a `=`\u53f7\uff0c\u8868\u793a\u8865\u4e86\u591a\u5c11\u5b57\u8282\uff0c\u89e3\u7801\u7684\u65f6\u5019\u4f1a\u53bb\u6389\u3002\u5c063 \u5b57\u8282\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u7f16\u7801\u4e3a 4 \u5b57\u8282\u7684\u6587\u672c\uff0c\u662f\u53ef\u4ee5\u8ba9\u6570\u636e\u5728\u90ae\u4ef6\u6b63\u6587\u3001\u7f51\u9875\u7b49\u76f4\u63a5\u663e\u793a\u3002\n\n**\u901a\u4fd7\u6982\u5ff5\u89e3\u91ca**\n\nBase64\u662f\u4f20\u8f938Bit\u5b57\u8282\u7801\u7684\u7f16\u7801\u65b9\u5f0f\uff0cBase64\u53ef\u4ee5\u5c06ASCII\u5b57\u7b26\u4e32\u6216\u8005\u662f\u4e8c\u8fdb\u5236\u7f16\u7801\u6210\u53ea\u5305\u542bA\u2014Z\uff0ca\u2014z\uff0c0\u20149\uff0c+\uff0c/ \u8fd964\u4e2a\u5b57\u7b26\uff08 26\u4e2a\u5927\u5199\u5b57\u6bcd\uff0c26\u4e2a\u5c0f\u5199\u5b57\u6bcd\uff0c10\u4e2a\u6570\u5b57\uff0c1\u4e2a+\uff0c\u4e00\u4e2a / \u521a\u597d64\u4e2a\u5b57\u7b26\u7ec4\u6210\uff09\uff1b\u8fd964\u4e2a\u5b57\u7b26\u75286\u4e2abit\u4f4d\u5c31\u53ef\u4ee5\u5168\u90e8\u8868\u793a\u51fa\u6765\uff0c\u4e00\u4e2a\u5b57\u8282\u67098\u4e2abit \u4f4d\uff0c\u90a3\u4e48\u8fd8\u5269\u4e0b\u4e24\u4e2abit\u4f4d\uff0c\u8fd9\u4e24\u4e2abit\u4f4d\u75280\u6765\u8865\u5145\u3002\u8f6c\u6362\u5b8c\u7a7a\u51fa\u7684\u7ed3\u679c\u5c31\u7528\u5c31\u7528\u201c=\u201d\u6765\u8865\u4f4d\uff0c\u603b\u4e4b\u8981\u4fdd\u8bc1\u6700\u540e\u7f16\u7801\u51fa\u6765\u5f97\u5b57\u8282\u6570\u662f4\u7684\u500d\u6570\u3002\n\n**\u6ce8\u610f**\n\n\u56e0\u4e3a\u6807\u51c6\u7684 Base64 \u4f1a\u6709 `+`\u548c`\\`\u5728 URL \u4e2d\u4e0d\u80fd\u76f4\u63a5\u505a\u53c2\u6570\uff0c\u4e8e\u662f\u51fa\u73b0\u4e86\u4e00\u79cd \"url safe\"\u7684 Base64\uff0c\u5c06 `+` \u548c `\\` \u8f6c\u6362\u4e3a `-` \u548c `_`\u3002\u56e0\u4e3a `=` \u7528\u5728 URL \u548c Cookie \u4f1a\u6709\u6b67\u4e49\uff0c\u6240\u4ee5\u5f88\u591a Base64 \u4f1a\u628a `=` \u53bb\u6389\u3002\u7531\u4e8e Base64 \u7684\u957f\u5ea6\u6c38\u8fdc\u662f 4 \u7684\u500d\u6570\uff0c\u6240\u4ee5\u53ea\u8981\u52a0\u4e0a `=` \u628a\u957f\u5ea6\u53d8\u4e3a 4 \u7684\u500d\u6570\uff0c\u5c31\u53ef\u4ee5\u89e3\u7801\u3002", "answer": "", "biz_type": 1, "qid": 146, "subject": "", "title": "\u8bf4\u4e00\u4e0b base64 \u7684\u7f16\u7801\u65b9\u5f0f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u8109\u8109"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 5, "qid": 231, "qtype": "short_answer", "short_answer": {"analysis": "#### 1\uff09ajax\n\najax\u662f\u5bf9\u539f\u751fXHR\u7684\u5c01\u88c5\uff0c\u9664\u6b64\u4ee5\u5916\u8fd8\u589e\u6dfb\u4e86\u5bf9JSONP\u7684\u652f\u6301\u3002\u6709\u4e00\u8bf4\u4e00\u7684\u8bf4\u4e00\u53e5\uff0cJQuery ajax\u7ecf\u8fc7\u591a\u5e74\u7684\u66f4\u65b0\u7ef4\u62a4\uff0c\u771f\u7684\u5df2\u7ecf\u662f\u975e\u5e38\u7684\u65b9\u4fbf\u4e86\uff0c\u4f18\u70b9\u65e0\u9700\u591a\u8a00\uff1b\u5982\u679c\u662f\u786c\u8981\u4e3e\u51fa\u51e0\u4e2a\u7f3a\u70b9\uff0c\u90a3\u53ef\u80fd\u53ea\u6709\n\n- \u672c\u8eab\u662f\u9488\u5bf9MVC\u7684\u7f16\u7a0b,\u4e0d\u7b26\u5408\u73b0\u5728\u524d\u7aefMVVM\u7684\u6d6a\u6f6e\n- \u57fa\u4e8e\u539f\u751f\u7684XHR\u5f00\u53d1\uff0cXHR\u672c\u8eab\u7684\u67b6\u6784\u4e0d\u6e05\u6670\uff0c\u5df2\u7ecf\u6709\u4e86fetch\u7684\u66ff\u4ee3\u65b9\u6848\n- JQuery\u6574\u4e2a\u9879\u76ee\u592a\u5927\uff0c\u5355\u7eaf\u4f7f\u7528ajax\u5374\u8981\u5f15\u5165\u6574\u4e2aJQuery\u975e\u5e38\u7684\u4e0d\u5408\u7406\uff08\u91c7\u53d6\u4e2a\u6027\u5316\u6253\u5305\u7684\u65b9\u6848\u53c8\u4e0d\u80fd\u4eab\u53d7CDN\u670d\u52a1\uff09\n\n\u5c3d\u7ba1JQuery\u5bf9\u6211\u4eec\u524d\u7aef\u7684\u5f00\u53d1\u5de5\u4f5c\u66fe\u6709\u7740\uff08\u73b0\u5728\u4e5f\u4ecd\u7136\u6709\u7740\uff09\u6df1\u8fdc\u7684\u5f71\u54cd\uff0c\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u968f\u7740VUE\uff0cREACT\u65b0\u4e00\u4ee3\u6846\u67b6\u7684\u5174\u8d77\uff0c\u4ee5\u53caES\u89c4\u8303\u7684\u5b8c\u5584\uff0c\u66f4\u591aAPI\u7684\u66f4\u65b0\uff0cJQuery\u8fd9\u79cd\u5927\u800c\u5168\u7684JS\u5e93\uff0c\u672a\u6765\u7684\u8def\u4f1a\u8d8a\u8d70\u8d8a\u7a84\u3002\n\n#### 2\uff09axios\n\nVue2.0\u4e4b\u540e\uff0c\u5c24\u96e8\u6eaa\u63a8\u8350\u5927\u5bb6\u7528axios\u66ff\u6362JQuery ajax\uff0c\u60f3\u5fc5\u8ba9Axios\u8fdb\u5165\u4e86\u5f88\u591a\u4eba\u7684\u76ee\u5149\u4e2d\u3002Axios\u672c\u8d28\u4e0a\u4e5f\u662f\u5bf9\u539f\u751fXHR\u7684\u5c01\u88c5\uff0c\u53ea\u4e0d\u8fc7\u5b83\u662fPromise\u7684\u5b9e\u73b0\u7248\u672c\uff0c\u7b26\u5408\u6700\u65b0\u7684ES\u89c4\u8303\uff0c\u4ece\u5b83\u7684\u5b98\u7f51\u4e0a\u53ef\u4ee5\u770b\u5230\u5b83\u6709\u4ee5\u4e0b\u51e0\u6761\u7279\u6027\uff1a\n\n- \u4ece node.js \u521b\u5efa http \u8bf7\u6c42\n- \u652f\u6301 Promise API\n- \u5ba2\u6237\u7aef\u652f\u6301\u9632\u6b62CSRF\n- **\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5e76\u53d1\u8bf7\u6c42\u7684\u63a5\u53e3**\uff08\u91cd\u8981\uff0c\u65b9\u4fbf\u4e86\u5f88\u591a\u7684\u64cd\u4f5c\uff09\n\n\u8fd9\u4e2a\u652f\u6301\u9632\u6b62CSRF\u5176\u5b9e\u633a\u597d\u73a9\u7684\uff0c\u662f\u600e\u4e48\u505a\u5230\u7684\u5462\uff0c\u5c31\u662f\u8ba9\u4f60\u7684\u6bcf\u4e2a\u8bf7\u6c42\u90fd\u5e26\u4e00\u4e2a\u4ececookie\u4e2d\u62ff\u5230\u7684key, \u6839\u636e\u6d4f\u89c8\u5668\u540c\u6e90\u7b56\u7565\uff0c\u5047\u5192\u7684\u7f51\u7ad9\u662f\u62ff\u4e0d\u5230\u4f60cookie\u4e2d\u5f97key\u7684\uff0c\u8fd9\u6837\uff0c\u540e\u53f0\u5c31\u53ef\u4ee5\u8f7b\u677e\u8fa8\u522b\u51fa\u8fd9\u4e2a\u8bf7\u6c42\u662f\u5426\u662f\u7528\u6237\u5728\u5047\u5192\u7f51\u7ad9\u4e0a\u7684\u8bef\u5bfc\u8f93\u5165\uff0c\u4ece\u800c\u91c7\u53d6\u6b63\u786e\u7684\u7b56\u7565\u3002\n\nAxios\u65e2\u63d0\u4f9b\u4e86\u5e76\u53d1\u7684\u5c01\u88c5\uff0c\u4e5f\u6ca1\u6709\u4e0b\u6587\u4f1a\u63d0\u5230\u7684fetch\u7684\u5404\u79cd\u95ee\u9898\uff0c\u800c\u4e14\u4f53\u79ef\u4e5f\u8f83\u5c0f\uff0c\u5f53\u4e4b\u65e0\u6127\u73b0\u5728\u6700\u5e94\u8be5\u9009\u7528\u7684\u8bf7\u6c42\u7684\u65b9\u5f0f\u3002\n\n#### 3\uff09fetch \n\nfetch\u53f7\u79f0\u662fajax\u7684\u66ff\u4ee3\u54c1\uff0c\u5b83\u7684\u597d\u5904\u6709\u4ee5\u4e0b\u51e0\u70b9\uff1a\n\n- \u7b26\u5408\u5173\u6ce8\u5206\u79bb\uff0c\u6ca1\u6709\u5c06\u8f93\u5165\u3001\u8f93\u51fa\u548c\u7528\u4e8b\u4ef6\u6765\u8ddf\u8e2a\u7684\u72b6\u6001\u6df7\u6742\u5728\u4e00\u4e2a\u5bf9\u8c61\u91cc\n- \u66f4\u597d\u66f4\u65b9\u4fbf\u7684\u5199\u6cd5\uff0c\u8bf8\u5982\uff1a\n\n```js\ntry {\n let response = await fetch(url);\n let data = response.json();\n console.log(data);\n} catch(e) {\n console.log(\"Oops, error\", e);\n}\n```\n\n\u4e0d\u7ba1\u662fJquery\u8fd8\u662fAxios\u90fd\u5df2\u7ecf\u5e2e\u6211\u4eec\u628axhr\u5c01\u88c5\u7684\u8db3\u591f\u597d\uff0c\u4f7f\u7528\u8d77\u6765\u4e5f\u8db3\u591f\u65b9\u4fbf\uff0c\u4e3a\u4ec0\u4e48\u6211\u4eec\u8fd8\u8981\u82b1\u8d39\u5927\u529b\u6c14\u53bb\u5b66\u4e60fetch\uff1f\n\nfetch\u7684\u4f18\u52bf\u4e3b\u8981\u4f18\u52bf\uff1a\n\n- \u8bed\u6cd5\u7b80\u6d01\uff0c\u66f4\u52a0\u8bed\u4e49\u5316\n- \u57fa\u4e8e\u6807\u51c6 Promise \u5b9e\u73b0\uff0c\u652f\u6301 async/await\n- \u66f4\u52a0\u5e95\u5c42\uff0c\u63d0\u4f9b\u7684API\u4e30\u5bcc\uff08request, response\uff09\n- \u8131\u79bb\u4e86XHR\uff0c\u662fES\u89c4\u8303\u91cc\u65b0\u7684\u5b9e\u73b0\u65b9\u5f0f\n\n#### 4\uff09\u603b\u7ed3 \n\n\u73b0\u5728Jquery\u8001\u8fc8\u7b28\u62d9\uff0cfetch\u5e74\u8f7b\u7a1a\u5ae9\uff0c\u53ea\u6709Axios\u6b63\u5f53\u5176\u5e74\uff01", "answer": "", "biz_type": 1, "qid": 231, "subject": "", "title": "\u8bf4\u4e00\u4e0b ajax/axios/fetch \u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 5, "qid": 304, "qtype": "short_answer", "short_answer": {"analysis": "#### 1.\u6982\u5ff5\n\nsuper \u5173\u952e\u8bcd\u7528\u4e8e\u8bbf\u95ee\u548c\u8c03\u7528\u4e00\u4e2a\u5bf9\u8c61\u7684\u7236\u5bf9\u8c61\u4e0a\u7684\u51fd\u6570\u3002\n\nsuper \u65e2\u53ef\u4ee5\u5f53\u4f5c \u51fd\u6570 \u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u5f53\u4f5c \u5bf9\u8c61 \u4f7f\u7528\u3002\n\u8bed\u6cd5\uff1a\n\n```js\nsuper([arguments]);\n//\u8c03\u7528\u7236\u5bf9\u8c61/\u7236\u7c7b \u7684\u6784\u9020\u51fd\u6570\nsuper.functionOnParent([arguments]);\n//\u8c03\u7528 \u7236\u5bf9\u8c61/\u7236\u7c7b \u4e0a\u7684\u65b9\u6cd5\n```\n\n#### 2.super\u5f53\u4f5c\u51fd\u6570\n\nsuper \u4f5c\u4e3a\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u4ee3\u8868\u7236\u7c7b\u7684 \u6784\u9020\u51fd\u6570 \u3002ES6\u8981\u6c42\uff0c\u5b50\u7c7b\u7684\u6784\u9020\u51fd\u6570\u5fc5\u987b\u6267\u884c\u4e00\u6b21 super \u51fd\u6570\u3002\n\n```js\nclass A{}\nclass B extends A{\n\tconstructor(){\n \tsuper();\n }\n}\n```\n\nsuper \u4ee3\u8868\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u6709\u4ee5\u4e0b\u51e0\u70b9\u9700\u8981\u6ce8\u610f\n\n1. super \u8fd4\u56de\u7684\u5bf9\u8c61\u662f \u5b50\u7c7b \u7684\u5b9e\u4f8b\u3002super \u5185\u90e8\u7684 this \u6307\u7684\u662f B \u7684\u5b9e\u4f8b\uff0c\u8fd9\u91cc\u7684\u7684 super() \u76f8\u5f53\u4e8e A.prototypeof.constructor.call(this) \u3002\n2. super \u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u65f6\uff0c\u5fc5\u987b\u5728\u4f7f\u7528 this \u5173\u952e\u8bcd\u4e4b\u524d\u4f7f\u7528\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002\n3. super() \u53ea\u80fd\u7528\u5728\u5b50\u7c7b\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u7528\u5728\u5176\u4ed6\u5730\u65b9\u4f1a\u62a5\u9519 \n\n#### 3.super\u5f53\u4f5c\u5bf9\u8c61\n\n**super \u4f5c\u4e3a\u5bf9\u8c61\u65f6\uff0c\u5728\u666e\u901a\u65b9\u6cd5\u4e2d\uff0c\u6307\u5411\u7236\u7c7b\u7684\u539f\u578b\u5bf9\u8c61\uff1b\u9759\u6001\u65b9\u6cd5\u4e2d\u6307\u5411\u7236\u7c7b\u3002**\n\n**1\uff09\u666e\u901a\u65b9\u6cd5\u4e2d**\n\n- super \u6307\u5411\u7684\u662f\u7236\u7c7b\u539f\u578b\u5bf9\u8c61,\u80fd\u83b7\u53d6\u7236\u7c7b\u539f\u578b\u5bf9\u8c61\u4e0a\u9762\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\uff0c\u4e0d\u80fd\u83b7\u53d6\u7236\u7c7b\u5b9e\u4f8b\u4e0a\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\n- \u901a\u8fc7 super \u8c03\u7528\u7236\u7c7b\u539f\u578b\u5bf9\u8c61\u4e0a\u7684\u65b9\u6cd5\u65f6\uff0c this \u6307\u5411\u5b50\u7c7b\u5b9e\u4f8b\u3002\n\n```js\nclass A {\n constructor() {\n this.x = 1;\n }\n print() {\n console.log(this.x);\n }\n}\nclass B extends A {\n constructor() {\n super();\n this.x = 2;\n }\n m() {\n super.print();\n }\n}\nlet b = new B();\nb.m() // 2\n```\n\n`super.print()`\u867d\u7136\u8c03\u7528\u7684\u662f`A.prototype.print()`\uff0c\u4f46\u662f`A.prototype.print()`\u5185\u90e8\u7684`this`\u6307\u5411\u5b50\u7c7b`B`\u7684\u5b9e\u4f8b\uff0c\u5bfc\u81f4\u8f93\u51fa\u7684\u662f`2`\uff0c\u800c\u4e0d\u662f`1`\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b9e\u9645\u4e0a\u6267\u884c\u7684\u662f`super.print.call(this)`\u3002\n\n\u901a\u8fc7 super \u548c\u5bf9\u67d0\u4e2a\u5c5e\u6027\u8d4b\u503c\uff0c\u8fd9\u65f6 super \u5c31\u662f this \uff0c\u8d4b\u503c\u7684\u5c5e\u6027\u4f1a\u53d8\u4e3a\u5b50\u7c7b\u5b9e\u4f8b\u7684\u5c5e\u6027\u3002\n\n\u4e3e\u4e2a\u4f8b\u5b50\uff1a\n\n```js\nclass A {\n constructor() {\n this.x = 1;\n }\n}\n\nclass B extends A {\n constructor() {\n console.log(super());\n this.x = 2;\n super.x = 3;\n console.log(super.x); // undefined\n console.log(this.x); // 3\n }\n}\n\nlet b = new B();\n```\n\n- `super() `\u00a0\uff1a\u4ee3\u8868\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u8fd4\u56de\u7684\u662f\u4e00\u4e2aB\u7684\u5b9e\u4f8b\uff1b\n- `super.x=3`\u00a0\uff1asuper\u6b64\u5904\u6307\u7684\u5b50\u7c7b\u5b9e\u4f8b\u7684 `this`\u00a0\u3002 `super.x = 3`\u00a0\uff0c\u5c31\u7c7b\u4f3c `this.x = 3`\u00a0;\n- `super.x`\u00a0\uff1a\u83b7\u53d6\u539f\u578b\u5bf9\u8c61\u7684 `x`\u00a0\uff0c\u6ca1\u5b9a\u4e49\u6240\u4ee5\u8fd4\u56de `undefined`\u00a0\n- `this.x`\u00a0\uff1a\u6700\u540e `this.x`\u00a0\u8fd4\u56de\u7684\u662f `super.x`\u00a0\u8bbe\u7f6e\u7684\u503c\u3002\n\n**2\uff09\u9759\u6001\u65b9\u6cd5\u4e2d**\n\n\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\uff0csuper \u5bf9\u8c61\u6307\u7684\u662f\u7236\u7c7b\uff0c\u800c\u4e0d\u662f\u7236\u7c7b\u7684\u539f\u578b\u5bf9\u8c61.\n\n```js\nclass Parent {\n static myMehtod(msg) {\n console.log('static', msg);\n }\n myMehtod(msg) {\n console.log('instance', msg);\n }\n}\nclass Child extends Parent {\n static myMethod(msg) {\n super.myMethod(msg);\n }\n myMethod(msg) {\n super.myMehtod(msg);\n }\n}\nChild.myMehtod(1);\nvar child = new Child();\nchild.myMehtod(2);\n```\n\n\u9759\u6001\u65b9\u6cd5\u4e2d\uff0csuper\u51fd\u6570\u5185\u90e8\u7684this\u6307\u5411\u7684\u662f\u5b50\u7c7b\uff0c\u800c\u4e0d\u662f\u5b50\u7c7b\u5b9e\u4f8b\u3002\n\n\u4e3e\u4e2a\u4f8b\u5b50\uff1a\n\n```js\nclass A {\n constructor() {\n this.x = 1;\n console.log(this.x)\n }\n static print() {\n console.log(this.x);\n }\n}\nclass B extends A {\n constructor() {\n super();\n this.x = 2;\n console.log(this.x);\n }\n static m() {\n super.print();//1\n }\n}\nconst b = new B();\nB.m();//undefined\nB.x = 3;\nB.m() // 3\n```\n\n- `B.m()`\u00a0\u9759\u6001\u65b9\u6cd5\u4e2d\uff0c\u8c03\u7528\u4e86 `super`\u00a0\uff0c\u6b64\u65f6\u6307\u5411\u7684\u662f \u7236\u7c7b , \u6267\u884c\u7236\u7c7b\u9759\u6001\u65b9\u6cd5 `super.print()`\u00a0\u3002\n- \u6b64\u65f6\u7236\u7c7b\u9759\u6001\u65b9\u6cd5\u4e2d\u7684 `this`\u00a0\u6307\u5411\u7684\u662f\u5b50\u7c7b `B`\u00a0\u3002\u56e0\u4e3a `B`\u00a0\u7c7b\u4e0a\u9762\u6ca1\u6709\u5c5e\u6027 `x`\u00a0,\u6240\u4ee5\u8fd4\u56de `undefined`\u00a0\u3002\n- \u901a\u8fc7\u8bbe\u7f6e `B.x`\u00a0\uff0c\u518d\u6b21\u8c03\u7528 `B.m()`\u00a0\u8fd4\u56de\u8bbe\u7f6e\u503c\u3002\n\n#### 4.super\u4e2d\u7684this\u6307\u5411\n\n- \u4ee3\u8868\u7236\u7c7b\u6784\u9020\u51fd\u6570\u7528\u7684 `super()`\u00a0,\u51fd\u6570\u5185\u90e8\u7684 `this`\u00a0 \u6307\u5411\u5b50\u7c7b\u5b9e\u4f8b\u3002\n- \u5728\u666e\u901a\u65b9\u6cd5\u4e2d\uff0c\u4ee3\u8868\u7236\u7c7b\u539f\u578b\u5bf9\u8c61\u4f7f\u7528\u7684 `super`\u00a0\uff0c\u51fd\u6570\u5185\u90e8 `this`\u00a0\u6307\u5411 \u5b50\u7c7b\u5b9e\u4f8b \u3002\n- \u5728\u9759\u6001\u65b9\u6cd5\u4e2d\uff0c\u4ee3\u8868\u7236\u7c7b\u4f7f\u7528\u7684 `super`\u00a0,\u51fd\u6570\u5185\u90e8 `this`\u00a0\u6307\u5411 \u5b50\u7c7b \u3002\n\n> tips\uff1a\u6ce8\u610f\uff0c\u4f7f\u7528`super`\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u663e\u5f0f\u6307\u5b9a\u662f\u4f5c\u4e3a\u51fd\u6570\u3001\u8fd8\u662f\u4f5c\u4e3a\u5bf9\u8c61\u4f7f\u7528\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002", "answer": "", "biz_type": 1, "qid": 304, "subject": "", "title": "es6 \u7c7b\u7ee7\u627f\u4e2d super \u7684\u4f5c\u7528"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 5, "qid": 158, "qtype": "short_answer", "short_answer": {"analysis": "### \u7cbe\u5ea6\u635f\u5931\u539f\u56e0\u5206\u6790\n\nJavaScript \u4e2d\u6240\u6709\u6570\u5b57\u5305\u62ec\u6574\u6570\u548c\u5c0f\u6570\u90fd\u53ea\u6709\u4e00\u79cd\u7c7b\u578b \u2014 Number\u3002\u5b83\u7684\u5b9e\u73b0\u9075\u5faa IEEE 754 \u6807\u51c6\uff0c\u4f7f\u7528 64 \u4f4d\u56fa\u5b9a\u957f\u5ea6\u6765\u8868\u793a\uff0c\u4e5f\u5c31\u662f\u6807\u51c6\u7684 double \u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\uff08\u76f8\u5173\u7684\u8fd8\u6709 float 32\u4f4d\u5355\u7cbe\u5ea6\uff09\u3002\u4e3a\u4ec0\u4e48\u5462\uff0c\u56e0\u4e3a\u8fd9\u6837\u8282\u7701\u5b58\u50a8\u7a7a\u95f4\u3002\n\n\u6700\u7b80\u5355\u7684\u4f8b\u5b50\u662f 0.1+0.2 = 0.30000000000000004\n\n0.1\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u7684\u662f\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u5c0f\u6570\uff0c\u8be5\u7248\u672c\u7684 JS \u91c7\u7528\u7684\u662f\u6d6e\u70b9\u6570\u6807\u51c6\u9700\u8981\u5bf9\u8fd9\u79cd\u65e0\u9650\u5faa\u73af\u7684\u4e8c\u8fdb\u5236\u8fdb\u884c\u622a\u53d6\uff0c\u4ece\u800c\u5bfc\u81f4\u4e86\u7cbe\u5ea6\u4e22\u5931\uff0c\u9020\u6210\u4e860.1\u4e0d\u518d\u662f0.1\uff0c\u622a\u53d6\u4e4b\u540e0.1\u53d8\u6210\u4e86 0.100\u2026001\uff0c0.2\u53d8\u6210\u4e860.200\u2026002\u3002\u6240\u4ee5\u4e24\u8005\u76f8\u52a0\u7684\u6570\u5927\u4e8e0.3\u3002\n\n### \u89e3\u51b3\n\n#### 1.toFixed()\n\n\u56e0\u4e3atoFixed() \u8fdb\u884c\u5e76\u8f6c\u6362\u4e4b\u540e\u662fstring\u7c7b\u578b\u7684\uff0c\u9700\u8981\u5728\u8fdb\u884c\u5f3a\u5236Number() \u8f6c\u6362\uff0c\u9700\u8981\u5904\u7406\u4e00\u4e0b\u517c\u5bb9\u6027\u95ee\u9898\uff0c\u901a\u8fc7\u5224\u65ad\u6700\u540e\u4e00\u4f4d\u662f\u5426\u5927\u4e8e\u7b49\u4e8e5\u6765\u51b3\u5b9a\u9700\u4e0d\u9700\u8981\u8fdb\u4f4d\uff0c\u5982\u679c\u9700\u8981\u8fdb\u4f4d\u5148\u628a\u5c0f\u6570\u4e58\u4ee5\u500d\u6570\u53d8\u4e3a\u6574\u6570\uff0c\u52a01\u4e4b\u540e\uff0c\u518d\u9664\u4ee5\u500d\u6570\u53d8\u4e3a\u5c0f\u6570\uff0c\u8fd9\u6837\u5c31\u4e0d\u7528\u4e00\u4f4d\u4e00\u4f4d\u7684\u8fdb\u884c\u5224\u65ad\u3002\n\n#### 2.\u4e00\u4e9b\u7c7b\u5e93\n\n- math.js\uff0c\n- decimal.js,\n- D.js\n\n#### 3.ES6\u5728Number\u5bf9\u8c61\u4e0a\u65b0\u589e\u4e86\u4e00\u4e2a\u6781\u5c0f\u7684\u5e38\u91cf\u2014\u2014Number.EPSILON\n\n```js\nNumber.EPSILON\n// 2.220446049250313e-16\nNumber.EPSILON.toFixed(20)\n// \"0.00000000000000022204\"\n```", "answer": "", "biz_type": 1, "qid": 158, "subject": "", "title": "number \u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u7cbe\u5ea6\u635f\u5931\uff0c\u600e\u6837\u907f\u514d"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 299, "qtype": "short_answer", "short_answer": {"analysis": "### \u7b54\u6848\n\nfor..of \u4e0d\u80fd\u76f4\u63a5\u904d\u5386\u5bf9\u8c61\uff0cfor...in \u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u5bf9\u8c61\n\n### \u539f\u56e0\n\n\u5bf9\u4e8e\u666e\u901a\u7684\u5bf9\u8c61\uff0cfor...of \u7ed3\u6784\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528\uff0c\u4f1a\u62a5\u9519\uff0c\u5fc5\u987b\u90e8\u7f72\u4e86 Iterator \u63a5\u53e3\u540e\u624d\u80fd\u4f7f\u7528\n\nfor...in \u8bed\u53e5\u4ee5\u4efb\u610f\u987a\u5e8f\u904d\u5386\u4e00\u4e2a\u5bf9\u8c61\u7684\u9664 Symbol \u4ee5\u5916\u7684\u53ef\u679a\u4e3e\u5c5e\u6027\u3002\n\n\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u3002\u88ab\u8c03\u7528\u65f6\u8fed\u4ee3\u4f20\u5165\u5bf9\u8c61\u7684\u6240\u6709\u53ef\u679a\u4e3e\u5c5e\u6027\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u6240\u6709\u5c5e\u6027\u540d\u548c\u5176\u5bf9\u5e94\u503c\u7684\u5b57\u7b26\u4e32\u3002\n\n```js\nconst obj = { a: 1, b: 2, c: 3 };\nfor (const prop in obj) {\n console.log(prop);\n}\n// a\n// b\n// c\n```\n\n### \u89e3\u51b3\u529e\u6cd5\n\n#### 1.\u4f7f\u7528 Object.key()\u3001Object.values()\u3001Object.entries()\n\n```js\nconst obj = { a: 1, b: 2, c: 3 };\nfor (const prop of Object.keys(obj)) {\n console.log(prop);\n}\n// a\n// b\n// c\nfor (const val of Object.values(obj)) {\n console.log(val);\n}\n// 1\n// 2\n// 3\nfor (const item of Object.entries(obj)) {\n console.log(item);\n}\n// [\"a\", 1]\n// [\"b\", 2]\n// [\"c\", 3]\n```\n\n#### 2.\u90e8\u7f72 Iterator \u63a5\u53e3\n\n**Iterator\u904d\u5386\u5668**\n\nIterator \u7684\u904d\u5386\u8fc7\u7a0b\u662f\u8fd9\u6837\u7684\u3002\n\n1. \u521b\u5efa\u4e00\u4e2a\u6307\u9488\u5bf9\u8c61\uff0c\u6307\u5411\u5f53\u524d\u6570\u636e\u7ed3\u6784\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u904d\u5386\u5668\u5bf9\u8c61\u672c\u8d28\u4e0a\uff0c\u5c31\u662f\u4e00\u4e2a\u6307\u9488\u5bf9\u8c61\u3002\n2. \u7b2c\u4e00\u6b21\u8c03\u7528\u6307\u9488\u5bf9\u8c61\u7684next\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u6307\u9488\u6307\u5411\u6570\u636e\u7ed3\u6784\u7684\u7b2c\u4e00\u4e2a\u6210\u5458\u3002\n3. \u7b2c\u4e8c\u6b21\u8c03\u7528\u6307\u9488\u5bf9\u8c61\u7684next\u65b9\u6cd5\uff0c\u6307\u9488\u5c31\u6307\u5411\u6570\u636e\u7ed3\u6784\u7684\u7b2c\u4e8c\u4e2a\u6210\u5458\u3002\n4. \u4e0d\u65ad\u8c03\u7528\u6307\u9488\u5bf9\u8c61\u7684next\u65b9\u6cd5\uff0c\u76f4\u5230\u5b83\u6307\u5411\u6570\u636e\u7ed3\u6784\u7684\u7ed3\u675f\u4f4d\u7f6e\u3002\n\u6bcf\u4e00\u6b21\u8c03\u7528next\u65b9\u6cd5\uff0c\u90fd\u4f1a\u8fd4\u56de\u6570\u636e\u7ed3\u6784\u7684\u5f53\u524d\u6210\u5458\u7684\u4fe1\u606f\n\n**iterator\u548cfor..of**\n\n\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u53ea\u8981\u90e8\u7f72\u4e86Symbol.iterator\u5c5e\u6027\uff0c\u5c31\u88ab\u89c6\u4e3a\u5177\u6709 iterator \u63a5\u53e3\uff0c\u5c31\u53ef\u4ee5\u7528for...of\u5faa\u73af\u904d\u5386\u5b83\u7684\u6210\u5458\u3002\n\n\u4e5f\u5c31\u662f\u8bf4\uff0cfor...of\u5faa\u73af\u5185\u90e8\u8c03\u7528\u7684\u662f\u6570\u636e\u7ed3\u6784\u7684Symbol.iterator\u65b9\u6cd5\u3002\n\nfor...of\u5faa\u73af\u53ef\u4ee5\u4f7f\u7528\u7684\u8303\u56f4\u5305\u62ec\u6570\u7ec4\u3001Set \u548c Map \u7ed3\u6784\u3001\u67d0\u4e9b\u7c7b\u4f3c\u6570\u7ec4\u7684\u5bf9\u8c61\n\n```js\nobj[Symbol.iterator] = function () {\n let index = 0;\n const _this = this;\n const keys = Object.keys(_this);\n const len = keys.length;\n return {\n next() {\n if (index < len) {\n return {\n value: _this[keys[index++]],\n done: false,\n };\n }\n return {\n value: null,\n done: true,\n };\n },\n };\n};\n\n// \u4f7f\u7528 Generator\u7248\nobj[Symbol.iterator] = function* () {\n const keys = Object.keys(this);\n for (let i = 0, len = keys.length; i < len; i++) {\n yield this[keys[i]];\n }\n};\n\nfor (const item of obj) {\n console.log(item);\n}\n// 1\n// 2\n// 3\n```", "answer": "", "biz_type": 1, "qid": 299, "subject": "", "title": "for..of \u548c for...in \u662f\u5426\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u5bf9\u8c61\uff0c\u4e3a\u4ec0\u4e48"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 212, "qtype": "short_answer", "short_answer": {"analysis": "### \u6b7b\u9501\n\n\u5982\u679c\u4e00\u7ec4\u8fdb\u7a0b\u4e2d\u6bcf\u4e00\u4e2a\u8fdb\u7a0b\u90fd\u5728\u7b49\u5f85\u4ec5\u7531\u8be5\u7ec4\u8fdb\u7a0b\u4e2d\u7684\u5176\u4ed6\u8fdb\u7a0b\u624d\u80fd\u5f15\u53d1\u7684\u4e8b\u4ef6\uff0c\u90a3\u4e48\u8be5\u7ec4\u8fdb\u7a0b\u662f\u6b7b\u9501\u7684\u3002\u6362\u8a00\u4e4b\uff1a\u6b7b\u9501\u5c31\u662f\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5360\u7528\u4e24\u4e2a\u8d44\u6e90\uff0c\u4f46\u53c8\u5728\u5f7c\u6b64\u7b49\u5f85\u5bf9\u65b9\u91ca\u653e\u9501\u3002\n\n\u4e3e\u4f8b\u6765\u8bf4\uff1a\u6709\u4e24\u4e2a\u8fdb\u7a0bA\u548cB,A\u6301\u6709\u8d44\u6e90a\u7b49\u5f85b\u8d44\u6e90\uff0cB\u6301\u6709\u8d44\u6e90b\u7b49\u5f85a\u8d44\u6e90\uff0c\u4e24\u4e2a\u8fdb\u7a0b\u90fd\u5728\u7b49\u5f85\u53e6\u4e00\u4e2a\u8d44\u6e90\u7684\u540c\u65f6\u4e0d\u91ca\u653e\u8d44\u6e90\uff0c\u5c31\u5f62\u6210\u6b7b\u9501\u3002\n\n#### 1\uff09\u6b7b\u9501\u4ea7\u751f\u7684\u56db\u4e2a\u5fc5\u8981\u6761\u4ef6\uff1a\n\n1. \u4e92\u65a5\u6761\u4ef6\uff1a\u4e00\u4e2a\u8d44\u6e90\u6bcf\u6b21\u53ea\u80fd\u88ab\u4e00\u4e2a\u8fdb\u7a0b\u4f7f\u7528\u3002\n2. \u8bf7\u6c42\u4e0e\u4fdd\u6301\u6761\u4ef6\uff1a\u4e00\u4e2a\u8fdb\u7a0b\u56e0\u8bf7\u6c42\u8d44\u6e90\u800c\u963b\u585e\u65f6\uff0c\u5bf9\u5df2\u83b7\u5f97\u7684\u8d44\u6e90\u4fdd\u6301\u4e0d\u653e\u3002\n3. \u4e0d\u5265\u593a\u6761\u4ef6:\u8fdb\u7a0b\u5df2\u83b7\u5f97\u7684\u8d44\u6e90\uff0c\u5728\u672b\u4f7f\u7528\u5b8c\u4e4b\u524d\uff0c\u4e0d\u80fd\u5f3a\u884c\u5265\u593a\u3002\n4. \u5faa\u73af\u7b49\u5f85\u6761\u4ef6:\u82e5\u5e72\u8fdb\u7a0b\u4e4b\u95f4\u5f62\u6210\u4e00\u79cd\u5934\u5c3e\u76f8\u63a5\u7684\u5faa\u73af\u7b49\u5f85\u8d44\u6e90\u5173\u7cfb\u3002\n\n#### 2\uff09\u6b7b\u9501\u5904\u7406\uff1a\n\n**\u9884\u9632\u6b7b\u9501**\uff1a\u7834\u574f\u56db\u4e2a\u5fc5\u8981\u6761\u4ef6\u4e2d\u7684\u4e00\u4e2a\u6216\u591a\u4e2a\u6765\u9884\u9632\u6b7b\u9501\n\n**\u907f\u514d\u6b7b\u9501**\uff1a\u5728\u8d44\u6e90\u52a8\u6001\u5206\u914d\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7528\u67d0\u79cd\u65b9\u5f0f\u9632\u6b62\u7cfb\u7edf\u8fdb\u5165\u4e0d\u5b89\u5168\u7684\u72b6\u6001\u3002\n\n**\u68c0\u6d4b\u6b7b\u9501**\uff1a\u8fd0\u884c\u65f6\u4ea7\u751f\u6b7b\u9501\uff0c\u53ca\u65f6\u53d1\u73b0\u601d\u7d22\uff0c\u5c06\u7a0b\u5e8f\u89e3\u8131\u51fa\u6765\u3002\n\n**\u89e3\u9664\u6b7b\u9501**\uff1a\u53d1\u751f\u6b7b\u9501\u540e\uff0c\u64a4\u9500\u8fdb\u7a0b\uff0c\u56de\u6536\u8d44\u6e90\uff0c\u5206\u914d\u7ed9\u6b63\u5728\u963b\u585e\u72b6\u6001\u7684\u8fdb\u7a0b\u3002\n\n**1.\u9884\u9632\u6b7b\u9501\u7684\u529e\u6cd5\uff1a**\n\n**\u7834\u574f\u8bf7\u6c42\u548c\u4fdd\u6301\u6761\u4ef6**\uff1a\u4e00\u6b21\u6027\u7684\u7533\u8bf7\u6240\u6709\u8d44\u6e90\u3002\u4e4b\u540e\u4e0d\u5728\u7533\u8bf7\u8d44\u6e90\uff0c\u5982\u679c\u4e0d\u6ee1\u8db3\u8d44\u6e90\u6761\u4ef6\u5219\u5f97\u4e0d\u5230\u8d44\u6e90\u5206\u914d\u3002\u53ea\u83b7\u5f97\u521d\u671f\u8d44\u6e90\u8fd0\u884c\uff0c\u4e4b\u540e\u5c06\u8fd0\u884c\u5b8c\u7684\u8d44\u6e90\u91ca\u653e\uff0c\u8bf7\u6c42\u65b0\u7684\u8d44\u6e90\u3002\n\n**\u7834\u574f\u4e0d\u53ef\u62a2\u5360\u6761\u4ef6**\uff1a\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u83b7\u5f97\u67d0\u79cd\u4e0d\u53ef\u62a2\u5360\u8d44\u6e90\uff0c\u63d0\u51fa\u65b0\u7684\u8d44\u6e90\u7533\u8bf7\uff0c\u82e5\u4e0d\u80fd\u6ee1\u8db3\uff0c\u5219\u91ca\u653e\u6240\u6709\u8d44\u6e90\uff0c\u4ee5\u540e\u9700\u8981\uff0c\u518d\u6b21\u91cd\u65b0\u7533\u8bf7\u3002\n\n**\u7834\u574f\u5faa\u73af\u7b49\u5f85\u6761\u4ef6**\uff1a\u5bf9\u8d44\u6e90\u8fdb\u884c\u6392\u53f7\uff0c\u6309\u7167\u5e8f\u53f7\u9012\u589e\u7684\u987a\u5e8f\u8bf7\u6c42\u8d44\u6e90\u3002\u82e5\u8fdb\u7a0b\u83b7\u5f97\u5e8f\u53f7\u9ad8\u7684\u8d44\u6e90\u60f3\u8981\u83b7\u53d6\u5e8f\u53f7\u4f4e\u7684\u8d44\u6e90\uff0c\u5c31\u9700\u8981\u5148\u91ca\u653e\u5e8f\u53f7\u9ad8\u7684\u8d44\u6e90\u3002\n\n**2.\u6b7b\u9501\u7684\u89e3\u9664\u529e\u6cd5\uff1a**\n\n- \u62a2\u5360\u8d44\u6e90\u3002\u4ece\u4e00\u4e2a\u6216\u591a\u4e2a\u8fdb\u7a0b\u4e2d\u62a2\u5360\u8db3\u591f\u6570\u91cf\u7684\u8d44\u6e90\uff0c\u5206\u914d\u7ed9\u6b7b\u9501\u8fdb\u7a0b\uff0c\u4ee5\u89e3\u9664\u6b7b\u9501\u72b6\u6001\u3002\n- \u7ec8\u6b62\uff08\u64a4\u9500\uff09\u8fdb\u7a0b\uff1a\u5c06\u4e00\u4e2a\u6216\u591a\u4e2a\u601d\u7d22\u8fdb\u7a0b\u7ec8\u6b62\uff08\u64a4\u9500\uff09\uff0c\u76f4\u81f3\u6253\u7834\u5faa\u73af\u73af\u8def\uff0c\u4f7f\u7cfb\u7edf\u4ece\u6b7b\u9501\u72b6\u6001\u89e3\u8131\u3002", "answer": "", "biz_type": 1, "qid": 212, "subject": "", "title": "\u8bf4\u4e00\u4e0b\u4ec0\u4e48\u662f\u6b7b\u9501"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u638c\u95e8\u4e00\u5bf9\u4e00"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 260, "qtype": "short_answer", "short_answer": {"analysis": "### \u6570\u7ec4\u65b9\u6cd5\n\n- valueOf\n - \u8fd4\u56de\u5305\u88c5\u5bf9\u8c61\u5b9e\u4f8b\u5bf9\u5e94\u7684\u539f\u59cb\u7c7b\u578b\u7684\u503c\u3002\n- toString\n - \u8fd4\u56de\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\n- push\n - \u6570\u7ec4\u540e\u65b9\u6dfb\u52a0\u5143\u7d20\n- pop\n - \u5220\u9664\u6570\u7ec4\u540e\u65b9\u5143\u7d20\u5e76\u8fd4\u56de\uff0c\u539f\u6570\u7ec4\u4f1a\u53d1\u751f\u6539\u53d8\uff1b\n- shift\n - \u7528\u4e8e\u5220\u9664\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u8be5\u5143\u7d20\u3002\n - \u8be5\u65b9\u6cd5\u4f1a\u6539\u53d8\u539f\u6570\u7ec4\u3002\n- unshift\n - \u7528\u4e8e\u5728\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u6dfb\u52a0\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u6dfb\u52a0\u65b0\u5143\u7d20\u540e\u7684\u6570\u7ec4\u957f\u5ea6\u3002\n - \u8be5\u65b9\u6cd5\u4f1a\u6539\u53d8\u539f\u6570\u7ec4\u3002\n- join\n - \u4ee5\u6307\u5b9a\u53c2\u6570\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u5c06\u6240\u6709\u6570\u7ec4\u6210\u5458\u8fde\u63a5\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002\u5982\u679c\u4e0d\u63d0\u4f9b\u53c2\u6570\uff0c\u9ed8\u8ba4\u7528\u9017\u53f7\u5206\u9694\u3002\n - \u5982\u679c\u6570\u7ec4\u6210\u5458\u662fundefined\u6216null\u6216\u7a7a\u4f4d\uff0c\u4f1a\u88ab\u8f6c\u6210\u7a7a\u5b57\u7b26\u4e32\u3002\n- concat\n - \u7528\u4e8e\u591a\u4e2a\u6570\u7ec4\u7684\u5408\u5e76\u3002\u5b83\u5c06\u65b0\u6570\u7ec4\u7684\u6210\u5458\uff0c\u6dfb\u52a0\u5230\u539f\u6570\u7ec4\u6210\u5458\u7684\u540e\u90e8\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u539f\u6570\u7ec4\u4e0d\u53d8\u3002\n - \u5982\u679c\u6570\u7ec4\u6210\u5458\u5305\u62ec\u5bf9\u8c61\uff0cconcat\u65b9\u6cd5\u8fd4\u56de\u5f53\u524d\u6570\u7ec4\u7684\u4e00\u4e2a\u6d45\u62f7\u8d1d\u3002\u6240\u8c13\u201c\u6d45\u62f7\u8d1d\u201d\uff0c\u6307\u7684\u662f\u65b0\u6570\u7ec4\u62f7\u8d1d\u7684\u662f\u5bf9\u8c61\u7684\u5f15\u7528\u3002\n- reverse\n - \u7528\u4e8e\u98a0\u5012\u6392\u5217\u6570\u7ec4\u5143\u7d20\uff0c\u8fd4\u56de\u6539\u53d8\u540e\u7684\u6570\u7ec4\u3002\n - \u8be5\u65b9\u6cd5\u5c06\u6539\u53d8\u539f\u6570\u7ec4\u3002\n- slice\n - \u7528\u4e8e\u63d0\u53d6\u76ee\u6807\u6570\u7ec4\u7684\u4e00\u90e8\u5206\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u539f\u6570\u7ec4\u4e0d\u53d8\n - arr.slice(start, end)\n- splice\n - \u7528\u4e8e\u5220\u9664\u539f\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u6210\u5458\uff0c\u5e76\u53ef\u4ee5\u5728\u5220\u9664\u7684\u4f4d\u7f6e\u6dfb\u52a0\u65b0\u7684\u6570\u7ec4\u6210\u5458\uff0c\u8fd4\u56de\u503c\u662f\u88ab\u5220\u9664\u7684\u5143\u7d20\u3002\n - \u8be5\u65b9\u6cd5\u4f1a\u6539\u53d8\u539f\u6570\u7ec4\u3002\n - arr.splice(start, count, addElement1, addElement2, ...);\n- sort\n - \u5bf9\u6570\u7ec4\u6210\u5458\u8fdb\u884c\u6392\u5e8f\uff0c\u9ed8\u8ba4\u662f\u6309\u7167\u5b57\u5178\u987a\u5e8f\u6392\u5e8f\u3002\n - \u539f\u6570\u7ec4\u5c06\u88ab\u6539\u53d8\u3002\n - \u51fd\u6570\u672c\u8eab\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u8868\u793a\u8fdb\u884c\u6bd4\u8f83\u7684\u4e24\u4e2a\u6570\u7ec4\u6210\u5458\u3002\u5982\u679c\u8be5\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5927\u4e8e0\uff0c\u8868\u793a\u7b2c\u4e00\u4e2a\u6210\u5458\u6392\u5728\u7b2c\u4e8c\u4e2a\u6210\u5458\u540e\u9762\uff1b\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u90fd\u662f\u7b2c\u4e00\u4e2a\u5143\u7d20\u6392\u5728\u7b2c\u4e8c\u4e2a\u5143\u7d20\u524d\u9762\n- map\n - \u5c06\u6570\u7ec4\u7684\u6240\u6709\u6210\u5458\u4f9d\u6b21\u4f20\u5165\u53c2\u6570\u51fd\u6570\uff0c\u7136\u540e\u628a\u6bcf\u4e00\u6b21\u7684\u6267\u884c\u7ed3\u679c\u7ec4\u6210\u4e00\u4e2a\u65b0\u6570\u7ec4\u8fd4\u56de\n - \u5f53\u524d\u6210\u5458\u3001\u5f53\u524d\u4f4d\u7f6e\u548c\u6570\u7ec4\u672c\u8eab\n- forEach\n - \u5bf9\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u4e00\u6b21\u7ed9\u5b9a\u7684\u51fd\u6570\u3002\n- filter\n - \u7528\u4e8e\u8fc7\u6ee4\u6570\u7ec4\u6210\u5458\uff0c\u6ee1\u8db3\u6761\u4ef6\u7684\u6210\u5458\u7ec4\u6210\u4e00\u4e2a\u65b0\u6570\u7ec4\u8fd4\u56de\n - \u5b83\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u6240\u6709\u6570\u7ec4\u6210\u5458\u4f9d\u6b21\u6267\u884c\u8be5\u51fd\u6570\uff0c\u8fd4\u56de\u7ed3\u679c\u4e3atrue\u7684\u6210\u5458\u7ec4\u6210\u4e00\u4e2a\u65b0\u6570\u7ec4\u8fd4\u56de\u3002\n - \u8be5\u65b9\u6cd5\u4e0d\u4f1a\u6539\u53d8\u539f\u6570\u7ec4\u3002\n- some\n - \u53ea\u8981\u4e00\u4e2a\u6210\u5458\u7684\u8fd4\u56de\u503c\u662ftrue\uff0c\u5219\u6574\u4e2asome\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u5c31\u662ftrue\uff0c\u5426\u5219\u8fd4\u56defalse\u3002\n- every\n - \u6240\u6709\u6210\u5458\u7684\u8fd4\u56de\u503c\u90fd\u662ftrue\uff0c\u6574\u4e2aevery\u65b9\u6cd5\u624d\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse\u3002\n - \u5bf9\u4e8e\u7a7a\u6570\u7ec4\uff0csome\u65b9\u6cd5\u8fd4\u56defalse\uff0cevery\u65b9\u6cd5\u8fd4\u56detrue\uff0c\u56de\u8c03\u51fd\u6570\u90fd\u4e0d\u4f1a\u6267\u884c\u3002\n- reduce\n - reduce\u65b9\u6cd5\u548creduceRight\u65b9\u6cd5\u4f9d\u6b21\u5904\u7406\u6570\u7ec4\u7684\u6bcf\u4e2a\u6210\u5458\uff0c\u6700\u7ec8\u7d2f\u8ba1\u4e3a\u4e00\u4e2a\u503c\u3002\u5b83\u4eec\u7684\u5dee\u522b\u662f\uff0creduce\u662f\u4ece\u5de6\u5230\u53f3\u5904\u7406\uff08\u4ece\u7b2c\u4e00\u4e2a\u6210\u5458\u5230\u6700\u540e\u4e00\u4e2a\u6210\u5458\uff09\uff0creduceRight\u5219\u662f\u4ece\u53f3\u5230\u5de6\uff08\u4ece\u6700\u540e\u4e00\u4e2a\u6210\u5458\u5230\u7b2c\u4e00\u4e2a\u6210\u5458\uff09\uff0c\u5176\u4ed6\u5b8c\u5168\u4e00\u6837\u3002\n - \u56db\u4e2a\u53c2\u6570\n - \u7d2f\u79ef\u53d8\u91cf\uff0c\u9ed8\u8ba4\u4e3a\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u6210\u5458\n - \u5f53\u524d\u53d8\u91cf\uff0c\u9ed8\u8ba4\u4e3a\u6570\u7ec4\u7684\u7b2c\u4e8c\u4e2a\u6210\u5458\n - \u5f53\u524d\u4f4d\u7f6e\uff08\u4ece0\u5f00\u59cb\uff09\n - \u539f\u6570\u7ec4\n- indexOf\n - \u8fd4\u56de\u7ed9\u5b9a\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u6ca1\u6709\u51fa\u73b0\u5219\u8fd4\u56de-1\n - \u8fd8\u53ef\u4ee5\u63a5\u53d7\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u8868\u793a\u641c\u7d22\u7684\u5f00\u59cb\u4f4d\u7f6e\n- lastIndexOf\n - \u8fd4\u56de\u7ed9\u5b9a\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u6ca1\u6709\u51fa\u73b0\u5219\u8fd4\u56de-1\u3002", "answer": "", "biz_type": 1, "qid": 260, "subject": "", "title": "\u6570\u7ec4\u6709\u54ea\u4e9b\u65b9\u6cd5 \u8bb2\u8bb2\u533a\u522b\u8ddf\u4f7f\u7528\u573a\u666f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fc3\u5a31", "\u5b89\u5c45\u5ba2"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 295, "qtype": "short_answer", "short_answer": {"analysis": "### \u6267\u884c\u7ed3\u679c\n\n```js\nTest3\nTest1\n```\n\n### \u89e3\u6790\n\n\u5728\u5168\u5c40\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\uff0cthis\u7684\u503c\u6307\u5411\u5168\u5c40\u5bf9\u8c61\u3002\n\n\u5728\u51fd\u6570\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\uff0cthis\u7684\u503c\u53d6\u51b3\u4e8e\u8be5\u51fd\u6570\u662f\u5982\u4f55\u88ab\u8c03\u7528\u7684\u3002\u5982\u679cthis\u88ab\u4e00\u4e2a\u5f15\u7528\u7684\u5bf9\u8c61\u8c03\u7528\uff0c\u90a3\u4e48this\u7684\u503c\u5c31\u88ab\u8bbe\u7f6e\u4e3a\u8be5\u5bf9\u8c61\uff0c\u5426\u5219\u88ab\u8bbe\u7f6e\u4e3a\u5168\u5c40\u5bf9\u8c61\u6216\u8005undefined\uff08\u4e25\u683c\u6a21\u5f0f\uff09", "answer": "", "biz_type": 1, "qid": 295, "subject": "```js\nvar fullname = \"Test1\";\nvar obj = {\n fullname: \"Test2\",\n prop: {\n fullname: \"Test3\",\n getFullname: function () {\n return this.fullname;\n },\n },\n};\nconsole.log(obj.prop.getFullname());\nvar test = obj.prop.getFullname;\nconsole.log(test());\n```\n", "title": "\u5199\u51fa\u4ee3\u7801\u8f93\u51fa\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 208, "qtype": "short_answer", "short_answer": {"analysis": "### \u6267\u884c\u7ed3\u679c\n\n```js\n1\n5\n6\n3\n7\n8\n9\n2\n4\n```\n\n### \u89e3\u6790\n\n- **macro-task**: script (\u6574\u4f53\u4ee3\u7801)\uff0csetTimeout, setInterval, setImmediate, I/O, UI rendering.\n- **micro-task**: process.nextTick, Promise(\u539f\u751f)\uff0cObject.observe\uff0cMutationObserver\n\n\u9664\u4e86script\u6574\u4f53\u4ee3\u7801\uff0cmicro-task\u7684\u4efb\u52a1\u4f18\u5148\u7ea7\u9ad8\u4e8emacro-task\u7684\u4efb\u52a1\u4f18\u5148\u7ea7\u3002\n\n### \u6267\u884c\u8fc7\u7a0b\u5206\u6790\n\n```js\nconsole.log(1); // \u811a\u672c\u5b8f\u4efb\u52a1 \u7acb\u5373\u6267\u884c\nsetTimeout(() => {\n console.log(2); // \u63a8\u5230\u5b8f\u4efb\u52a1\u961f\u5217 \u7b49\u5f85\u6267\u884c\n});\nprocess.nextTick(() => {\n console.log(3);\n});\nsetImmediate(() => {\n console.log(4); // \u63a8\u5230check\u56de\u8c03\u961f\u5217 \u7b49\u5f85\u6267\u884c\n});\nnew Promise((resolve) => {\n console.log(5); // \u811a\u672c\u5b8f\u4efb\u52a1 \u7acb\u5373\u6267\u884c\n resolve(); // \u5373:\u5c06 then \u63a8\u5230\u5fae\u4efb\u52a1\u961f\u5217 \u7b49\u5f85\u6267\u884c\n console.log(6); // \u811a\u672c\u5b8f\u4efb\u52a1 \u7acb\u5373\u6267\u884c\n}).then(() => {\n console.log(7);\n});\nPromise.resolve() // \u5373:\u5c06 then \u63a8\u5230\u5fae\u4efb\u52a1\u961f\u5217 \u7b49\u5f85\u6267\u884c\n .then(() => {\n console.log(8);\n process.nextTick(() => {\n console.log(9);\n });\n});\n\n// \u7531\u4e0a\u53ef\u7acb\u5373\u5f97\n// 1\n// 5\n// 6\n\n```\n------\n\u6b64\u65f6\u961f\u5217\u5b58\u50a8\u7684 log \u60c5\u51b5\n\ntick: 3\n\nmicro: 7, 8, (tick: 9)\n\ntimeout: 2\n\ncheck: 4\n\n```js\n// \u5df2\u6709tick\u5168\u90e8\u6267\u884c, \u518d\u5f97\n// 3\n// \u7136\u540e\u6e05\u7a7a microtask, \u518d\u5f97\n// 7\n// 8\n```\n------\n\n\u6b64\u65f6\u961f\u5217\u5b58\u50a8\u7684 log \u60c5\u51b5\n\ntick: 9\n\nmicro: \u7a7a\n\ntimeout: 2\n\ncheck: 4\n\n```js\n// \u5df2\u6709tick\u5168\u90e8\u6267\u884c, \u518d\u5f97\n// 9\n// \u6ca1\u6709 IO \u65f6, timeout\u961f\u5217\u68c0\u67e5\u5728check\u961f\u5217\u524d, \u6700\u540e\u5f97\n// 2\n// 4\n```\n------\n\n\u7efc\u4e0a\u7ed3\u679c\u4e3a\uff1a\n\n\u6ca1\u6709 IO \u65f6:\n\n`1, 5, 6, 3, 7, 8, 9, 2, 4`\n\n\u6709 IO \u65f6:\n\n`1, 5, 6, 3, 7, 8, 9, 4, 2`\n\n", "answer": "", "biz_type": 1, "qid": 208, "subject": "```js\nconsole.log(1);\nsetTimeout(() => {\n console.log(2);\n});\nprocess.nextTick(() => {\n console.log(3);\n});\nsetImmediate(() => {\n console.log(4);\n});\nnew Promise((resolve) => {\n console.log(5);\n resolve();\n console.log(6);\n}).then(() => {\n console.log(7);\n});\nPromise.resolve().then(() => {\n console.log(8);\n process.nextTick(() => {\n console.log(9);\n });\n});\n// \u5199\u51fa\u6267\u884c\u7ed3\u679c\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee5\u4e0b\u4ee3\u7801\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u65b0\u4e1c\u65b9"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 9, "qid": 174, "qtype": "short_answer", "short_answer": {"analysis": "### \u5b9e\u73b0\u65b9\u5f0f\n\n#### 1.\u7ec4\u5408\u7ee7\u627f\n\n\u79f0\u4e4b\u4e3a\u4f2a\u7ecf\u5178\u7ee7\u627f\n\n\u6307\u7684\u662f\u5c06\u539f\u578b\u94fe\u548c\u501f\u7528\u6784\u9020\u51fd\u6570\u7684\u6280\u672f\u7ec4\u5408\u5230\u4e00\u8d77\uff0c\u4ece\u800c\u53d1\u6325\u4e8c\u8005\u4e4b\u957f\u7684\u4e00\u79cd\u7ee7\u627f\u6a21\u5f0f\uff0c\u601d\u8def\u662f\u4f7f\u7528\u539f\u578b\u94fe\u5b9e\u73b0\u5bf9\u539f\u578b\u5c5e\u6027\u3001\u65b9\u6cd5\u7684\u7ee7\u627f\uff0c\u800c\u901a\u8fc7\u501f\u7528\u6784\u9020\u51fd\u6570\u6765\u5b9e\u73b0\u5bf9\u5b9e\u4f8b\u5c5e\u6027\u7684\u7ee7\u627f\uff0c\u8fd9\u6837\u5373\u901a\u8fc7\u5728\u539f\u578b\u4e0a\u5b9a\u4e49\u65b9\u6cd5\u5b9e\u73b0\u4e86\u590d\u7528\uff0c\u53c8\u80fd\u4fdd\u8bc1\u6bcf\u4e2a\u5b9e\u4f8b\u90fd\u6709\u81ea\u5df1\u7684\u5c5e\u6027\n\n**\u7f3a\u70b9**\uff1a\u5728\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u90fd\u4f1a\u8c03\u7528\u4e24\u6b21\u8d85\u7c7b\u578b\u7684\u6784\u9020\u51fd\u6570\u5bf9\u8c61(\u5373A)\uff1a\u4e00\u6b21\u5f0f\u5728\u521b\u5efa\u5b50\u7c7b\u539f\u578b\u7684\u65f6\u5019\uff0c\u4e00\u6b21\u662f\u5728\u5b50\u7c7b\u884c\u6784\u9020\u51fd\u6570\u65f6\u91cd\u5199\u8fd9\u4e9b\u5c5e\u6027\uff0c \u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684\u5c5e\u6027\uff0c\u4e00\u7ec4\u5728\u5b50\u7c7b\u5b9e\u4f8b\u4e0a\uff0c\u4e00\u7ec4\u5728\u5b50\u7c7b\u539f\u578b\u4e0a(\u5728\u5b50\u7c7b\u539f\u578b\u4e0a\u521b\u5efa\u4e86\u4e0d\u5fc5\u8981\u591a\u4f59\u7684\u5c5e\u6027) \n\n\u4ee3\u7801\u5b9e\u73b0\n\n```js\nfunction A(name) {\n this.name = name;\n}\n\nA.prototype.getName = function () {\n console.log(this.name)\n}\n\nfunction B(name,age){\n // \u7b2c\u4e00\u6b21\u8c03\u7528 A\n A.call(this,name);\n this.age = age;\n this.firends=['\u524d\u7aef','\u8d44\u6df1'];\n}\n\n // \u7b2c\u4e8c\u6b21\u8c03\u7528 A\nB.prototype = new A();\nB.prototype.constructor = B;\n// \u7ed9\u5b50\u7c7b\u6dfb\u52a0\u7279\u6709\u7684\u65b9\u6cd5\uff0c\u9700\u8981\u5728\u7ee7\u627f\u4e4b\u540e\nB.prototype.getFirends=function(){\n console.log(this.firends);\n}\n\n\nconst instance1 = new B('jingcheng',3);\ninstance1.getName(); // jingcheng\ninstance1.firends.push('React')\nconst instance2 = new B('yideng',4);\ninstance2.getName(); // yideng\n\nconsole.log(instance1, instance2)\n```\n\n#### 2.\u5bc4\u751f\u7ec4\u5408\u5f0f\u7ee7\u627f\n\n\u901a\u8fc7\u6784\u9020\u51fd\u6570\u6765\u7ee7\u627f\u5c5e\u6027\uff0c\u901a\u8fc7\u539f\u578b\u94fe\u7684\u6df7\u6210\u5f62\u5f0f\u6765\u7ee7\u627f\u65b9\u6cd5\uff0c\u601d\u8def\u662f\uff1a\u4e0d\u7528\u6307\u5b9a\u5b50\u7c7b\u578b\u7684\u539f\u578b\u800c\u8c03\u7528\u8d85\u7c7b\u578b\u7684\u6784\u9020\u51fd\u6570\uff0c\u6211\u4eec\u9700\u8981\u7684\u65e0\u975e\u5c31\u662f\u8d85\u7c7b\u578b\u7684\u4e00\u4e2a\u526f\u672c\u800c\u5df2\uff0c\u672c\u8d28\u4e0a\u5c31\u662f\u4f7f\u7528\u5bc4\u751f\u5f0f\u7ee7\u627f\u6765\u7ee7\u627f\u8d85\u7c7b\u578b\u7684\u539f\u578b\uff0c\u7136\u540e\u518d\u5c06\u7ed3\u679c\u6307\u5b9a\u7ed9\u5b50\u7c7b\u578b\u7684\u539f\u578b\n\n**\u4f18\u70b9**\uff1a\u8fd9\u4e2a\u7ee7\u627f\u7684\u9ad8\u6548\u7387\u4f53\u73b0\u5728\u5b83\u53ea\u8c03\u7528\u4e86\u4e00\u6b21\u8d85\u7c7b\u578b\u7684\u6784\u9020\u51fd\u6570\u5bf9\u8c61(\u5373A)\uff0c\u56e0\u6b64\u907f\u514d\u4e86\u5b50\u7c7b\u578b\u7684\u6784\u9020\u51fd\u6570\u7684\u539f\u578b\u4e0a(`B.prototype`) \u4e0a\u521b\u5efa\u4e0d\u5fc5\u8981\u3001\u591a\u4f59\u7684\u5c5e\u6027\uff0c\u4e0e\u6b64\u540c\u65f6\uff0c\u539f\u578b\u94fe\u8fd8\u53ef\u4ee5\u4fdd\u6301\u4e0d\u53d8\n\n\u4ee3\u7801\u793a\u4f8b\n\n```js\nfunction inheritPrototype(subType,superType){\n var prototype = Object.create(superType.prototype);\n prototype.constructor = subType;\n subType.prototype = prototype;\n}\n\n\nfunction A(name) {\n this.name = name;\n}\n\nA.prototype.getName = function () {\n console.log(this.name)\n}\n\nfunction B(name, age) {\n A.call(this, name);\n this.age = age;\n this.firends = ['\u524d\u7aef', '\u8d44\u6df1'];\n}\n\ninheritPrototype(B,A)\nB.prototype.getFirends = function () {\n console.log(this.firends);\n}\n\nconst instance1 = new B('jingcheng', 3);\ninstance1.getName(); // jingcheng\ninstance1.firends.push('React')\nconst instance2 = new B('yideng', 4);\ninstance2.getName(); // yideng\n\nconsole.log(instance1, instance2)\n```\n\n\u4ee5\u4e0a\u7684\u65b9\u6cd5\u5747\u53ef\u4ee5\u5b9e\u73b0\u6784\u9020\u4e00\u4e2a\u51fd\u6570B \u7ee7\u627fA\uff0c\u4e0b\u9762\u662f\u7528ES6 \u7684class \u6765\u5b9e\u73b0\u7ee7\u627f\n\n#### 3.class\u65b9\u5f0f\u5b9e\u73b0\u7ee7\u627f\n\n```js\nclass A {\n constructor(name) {\n this.name = name;\n }\n\n getName=()=>{\n console.log('\u6211\u662fPublic',this.name)\n }\n}\n\n\nclass B extends A {\n constructor(name,age) {\n super();\n this.name = name;\n this.age =age;\n this.firends = ['\u524d\u7aef', '\u8d44\u6df1'];\n }\n\n getFirends=()=>{\n console.log(this.firends)\n }\n\n // \u5bf9\u4e8e\u7ee7\u627f\u7684\u65b9\u6cd5\u8fdb\u884c\u91cd\u5199\n getName=()=>{\n console.log('\u6211\u662f\u5b50\u7c7b\u7684getName',this.name)\n }\n\n}\n\nconst instance1 = new B('jingcheng', 3);\ninstance1.getName();\ninstance1.firends.push('React')\nconst instance2 = new B('yideng', 4);\ninstance2.getName();\nconst instance3 = new A('laowang')\ninstance3.getName();\n\nconsole.log(instance1, instance2)\n```\n\n", "answer": "", "biz_type": 1, "qid": 174, "subject": "```js\nfunction A(name) {\n this.name = name;\n}\nA.prototype.getName = function () {\n console.log(this.name);\n};\n```\n", "title": "\u5df2\u77e5\u51fd\u6570 A\uff0c\u8981\u6c42\u6784\u9020\u2f00\u4e2a\u51fd\u6570 B \u7ee7\u627f A"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9ad8\u5fb7"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 11, "qid": 197, "qtype": "short_answer", "short_answer": {"analysis": "### \u5b9e\u73b0\u65b9\u5f0f\n\n#### 1.\u5c06DOM\u7ed3\u6784\u8f6c\u6362\u6210\u5bf9\u8c61\u4fdd\u5b58\u5230\u5185\u5b58\u4e2d\n\n- `<img /> => { tag: 'img'}`\n- `\u6587\u672c\u8282\u70b9 => { tag: undefined, value: '\u6587\u672c\u8282\u70b9' }`\n- `<img title=\"img\" class=\"c\" /> => { tag: 'img', data: { title = \"img\", class=\"c\" } }`\n- `<div><img /></div> => { tag: 'div', children: [{ tag: 'img' }]}`\n\n#### 2.\u6839\u636e\u4e0a\u9762\u53ef\u4ee5\u5199\u51fa\u865a\u62dfDOM\u7684\u6570\u636e\u7ed3\u6784\n\n```js\nfunction VNode (tag, data, value, type) {\n this.tag = tag && tag.toLowerCase()\n this.data = data\n this.value = value\n this.type = type\n this.children = []\n}\nVNode.prototype.appendChild = function (vnode) {\n this.children.push(vnode)\n}\n```\n\n#### 3.\u53ef\u80fd\u7528\u5230\u7684\u57fa\u7840\u77e5\u8bc6\n\n- \u5224\u65ad\u5143\u7d20\u7684\u8282\u70b9\u7c7b\u578b: `node.nodeType`\n\n```js\nlet nodeType = node.nodeType\nif(nodeType === 1) {\n // \u5143\u7d20\u7c7b\u578b\n} else if (nodeType === 3) {\n\t// \u8282\u70b9\u7c7b\u578b\n}\n```\n\n- \u83b7\u53d6\u5143\u7d20\u7c7b\u578b\u7684\u6807\u7b7e\u540d\u548c\u5c5e\u6027 && \u5c5e\u6027\u4e2d\u5177\u4f53\u7684\u952e\u503c\u5bf9,\u4fdd\u5b58\u5728\u4e00\u4e2a\u5bf9\u8c61\u4e2d\n\n```js\nlet nodeName = node.nodeName\t// \u6807\u7b7e\u540d\nlet attrs = node.attributes\t// \u5c5e\u6027\nlet _data = {} // \u4fdd\u5b58\u5404\u4e2a\u5177\u4f53\u7684\u5c5e\u6027\u7684\u952e\u503c\u5bf9,\u76f8\u5f53\u4e8e\u865a\u62dfDOM\u4e2d\u7684data\u5c5e\u6027\nlet attrLen = attrs.length // \u5c5e\u6027\u957f\u5ea6\nfor(let i = 0; i < attrLen; i++){\n _data[attrs[i].nodeName] = attrs[i].nodeValue\n}\n```\n\n- \u83b7\u53d6\u5f53\u524d\u8282\u70b9\u7684\u5b50\u8282\u70b9\n\n```js\nlet childNodes = node.childNodes\nlet childLen = childNodes.length\nfor(let i = 0; i < childLen; i++){\n\tconsole.log(childNodes[i])\n}\n```\n\n#### 4.\u7b97\u6cd5\u601d\u8def\n\n- \u4f7f\u7528`document.querySelector`\u83b7\u53d6\u8981\u8f6c\u6362\u6210\u865a\u62dfDOM\u7684\u6a21\u677f\n- \u4f7f\u7528`nodeType`\u65b9\u6cd5\u6765\u83b7\u53d6\u662f\u5143\u7d20\u7c7b\u578b\u8fd8\u662f\u6587\u672c\u7c7b\u578b\n- \u82e5\u662f\u5143\u7d20\u7c7b\u578b\n - \u4f7f\u7528`nodeName`\u83b7\u53d6\u6807\u7b7e\u540d\n - \u4f7f\u7528`attributes`\u83b7\u53d6\u5c5e\u6027\u540d,\u5e76\u5c06\u5177\u4f53\u7684\u5c5e\u6027\u4fdd\u5b58\u5230\u4e00\u4e2a\u5bf9\u8c61`_data`\u4e2d\n - \u521b\u5efa\u865a\u62dfDOM\u8282\u70b9\n - \u8003\u8651\u5143\u7d20\u7c7b\u578b\u662f\u5426\u6709\u5b50\u8282\u70b9,\u4f7f\u7528\u9012\u5f52,\u5c06\u5b50\u8282\u70b9\u7684\u865a\u62dfDOM\u5b58\u5165\u5176\u4e2d\n- \u82e5\u662f\u6587\u672c\u7c7b\u578b\n - \u76f4\u63a5\u521b\u5efa\u865a\u62dfDOM,\u4e0d\u9700\u8981\u8003\u8651\u5b50\u8282\u70b9\u7684\u95ee\u9898\n\n```js\n// \u83b7\u53d6\u8981\u8f6c\u6362\u7684DOM\u7ed3\u6784\nlet root = document.querySelector('#root')\n// \u4f7f\u7528getVNode\u65b9\u6cd5\u5c06 \u771f\u5b9e\u7684DOM\u7ed3\u6784\u8f6c\u6362\u6210\u865a\u62dfDOM\nlet vroot = getVNode(root)\n```\n\n\u4ee5\u4e0a\u5199\u4e86\u865a\u62dfDOM\u7684\u6570\u636e\u7ed3\u6784,\u4ee5\u53ca\u4f7f\u7528`getVNode`\u65b9\u6cd5\u5c06\u771f\u5b9eDOM\u7ed3\u6784\u8f6c\u6362\u6210\u865a\u62dfDOM\uff0c\n\n#### 5.\u4e0b\u9762\u5f00\u59cb\u9010\u6b65\u5b9e\u73b0getVNode\u65b9\u6cd5\n\n\u4ee5\u4e0a\u5199\u4e86\u865a\u62dfDOM\u7684\u6570\u636e\u7ed3\u6784,\u4ee5\u53ca\u4f7f\u7528`getVNode`\u65b9\u6cd5\u5c06\u771f\u5b9eDOM\u7ed3\u6784\u8f6c\u6362\u6210\u865a\u62dfDOM\uff0c\u4e0b\u9762\u5f00\u59cb\u9010\u6b65\u5b9e\u73b0getVNode\u65b9\u6cd5\n\n- \u5224\u65ad\u8282\u70b9\u7c7b\u578b,\u5e76\u8fd4\u56de\u865a\u62dfDOM\n\n```js\nfunction getVNode(node){\n // \u83b7\u53d6\u8282\u70b9\u7c7b\u578b\n let nodeType = node.nodeType\n if(nodeType === 1) {\n // \u5143\u7d20\u7c7b\u578b: \u83b7\u53d6\u5176\u5c5e\u6027,\u5224\u65ad\u5b50\u5143\u7d20,\u521b\u5efa\u865a\u62dfDOM\n } \n else if(nodeType === 3) {\n // \u6587\u672c\u7c7b\u578b: \u76f4\u63a5\u521b\u5efa\u865a\u62dfDOM\n }\n let _vnode = null\n return _vnode\n}\n```\n\n- \u4e0b\u9762\u6839\u636e\u5143\u7d20\u7c7b\u578b\u548c\u6587\u672c\u7c7b\u578b\u5206\u522b\u521b\u5efa\u865a\u62dfDOM\n\n```js\nif(nodeType === 1){\n // \u6807\u7b7e\u540d\n let tag = node.nodeName\n // \u5c5e\u6027\n let attrs = node.attributes\n /*\n \u5c5e\u6027\u8f6c\u6362\u6210\u5bf9\u8c61\u5f62\u5f0f: <div title =\"marron\" class=\"1\"></div>\n { tag: 'div', data: { title: 'marron', class: '1' }}\n */\n let _data = {} // \u8fd9\u4e2a_data\u5c31\u662f\u865a\u62dfDOM\u4e2d\u7684data\u5c5e\u6027\n let arrtLen = attrs.length // \u5c5e\u6027\u957f\u5ea6\n for(let i = 0; i < arrtLen; i++){\n _data[attrs[i].nodeName] = attrs[i].nodeValue\n }\n // \u521b\u5efa\u5143\u7d20\u7c7b\u578b\u7684\u865a\u62dfDOM\n _vnode = new VNode(tag, _data, undefined, nodeType)\n // \u8003\u8651node\u7684\u5b50\u5143\u7d20\n let childNodes = node.childNodes\n let childLen = childNodes.length\n for(let i =0; i < childLen; i++){\n _vnode.appendChild(getVNode(childNodes[i]))\n }\n}\n// \u63a5\u4e0b\u6765\u8003\u8651\u6587\u672c\u7c7b\u578b\nelse if(nodeType === 3){\n _vnode = new VNode(undefined, undefined, node.nodeValue, nodeType)\n}\n```\n\n#### 6.\u603b\u4f53\u5b9e\u73b0\u4ee3\u7801\n\n```html\n// html\n<div id=\"root\">\n <img class=\"img\" />\n <div title=\"div-box\"><p>box</p></div>\n \u6587\u672c\u8282\u70b9\n</div>\n```\n\n```js\n// javascript\nfunction VNode (tag, data, value, type) {\n this.tag = tag && tag.toLowerCase()\n this.data = data\n this.value = value\n this.type = type\n this.children = []\n}\nVNode.prototype.appendChild = function (vnode) {\n this.children.push(vnode)\n}\nfunction getVNode (node) {\n let nodeType = node.nodeType\n let _vnode = null\n if (nodeType === 1) {\n let tag = node.nodeName\n let attrs = node.attributes\n let _data = {}\n let attrLen = attrs.length\n for (let i = 0; i < attrLen; i++) {\n _data[attrs[i].nodeName] = attrs[i].nodeValue\n }\n _vnode = new VNode(tag, _data, undefined, nodeType)\n let childNodes = node.childNodes\n let childLen = childNodes.length\n for (let i = 0; i < childLen; i++) {\n _vnode.appendChild(getVNode(childNodes[i]))\n }\n }\n else if (nodeType === 3) {\n _vnode = new VNode(undefined, undefined, node.nodeValue, nodeType)\n }\n return _vnode\n}\n\nlet root = document.querySelector('#root')\nlet vroot = getVNode(root)\nconsole.log(vroot)\n```\n", "answer": "", "biz_type": 1, "qid": 197, "subject": "", "title": "\u5982\u4f55\u628a\u771f\u5b9e dom \u8f6c\u53d8\u4e3a\u865a\u62df dom\uff0c\u4ee3\u7801\u5b9e\u73b0\u4e00\u4e0b"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u8109\u8109", "\u5151\u5427", "\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 229, "qtype": "short_answer", "short_answer": {"analysis": "### \u539f\u578b\u94fe\n\n![prototype](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-828-prototype.jpg)\n\n#### 1.\u6982\u5ff5\n\n\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u6307\u5411\u5b83\u7684\u539f\u578b\uff08prototype\uff09\u5bf9\u8c61\u7684\u5185\u90e8\u94fe\u63a5\u3002\u8fd9\u4e2a\u539f\u578b\u5bf9\u8c61\u53c8\u6709\u81ea\u5df1\u7684\u539f\u578b\uff0c\u76f4\u5230\u67d0\u4e2a\u5bf9\u8c61\u7684\u539f\u578b\u4e3a null \u4e3a\u6b62\uff08\u4e5f\u5c31\u662f\u4e0d\u518d\u6709\u539f\u578b\u6307\u5411\uff09\uff0c\u7ec4\u6210\u8fd9\u6761\u94fe\u7684\u6700\u540e\u4e00\u73af\u3002\u8fd9\u79cd\u4e00\u7ea7\u4e00\u7ea7\u7684\u94fe\u7ed3\u6784\u5c31\u79f0\u4e3a\u539f\u578b\u94fe\u3002\u53ef\u4ee5\u770b\u4e0b\u4e0a\u8fb9\u7684\u56fe\u3002\n\n#### 2.\u539f\u578b\u94fe\u7279\u70b9\n\n1. \u6240\u6709\u7684\u5bf9\u8c61\u90fd\u6709\"[[prototype]]\"\u5c5e\u6027\uff08\u901a\u8fc7__proto__\u8bbf\u95ee\uff09\uff0c\u8be5\u5c5e\u6027\u5bf9\u5e94\u5bf9\u8c61\u7684\u539f\u578b\n2. \u6240\u6709\u7684\u51fd\u6570\u5bf9\u8c61\u90fd\u6709\"prototype\"\u5c5e\u6027\uff0c\u8be5\u5c5e\u6027\u7684\u503c\u4f1a\u88ab\u8d4b\u503c\u7ed9\u8be5\u51fd\u6570\u521b\u5efa\u7684\u5bf9\u8c61\u7684\"__proto__\"\u5c5e\u6027\n3. \u6240\u6709\u7684\u539f\u578b\u5bf9\u8c61\u90fd\u6709\"constructor\"\u5c5e\u6027\uff0c\u8be5\u5c5e\u6027\u5bf9\u5e94\u521b\u5efa\u6240\u6709\u6307\u5411\u8be5\u539f\u578b\u7684\u5b9e\u4f8b\u7684\u6784\u9020\u51fd\u6570\n4. \u51fd\u6570\u5bf9\u8c61\u548c\u539f\u578b\u5bf9\u8c61\u901a\u8fc7\"prototype\"\u548c\"constructor\"\u5c5e\u6027\u8fdb\u884c\u76f8\u4e92\u5173\u8054", "answer": "", "biz_type": 1, "qid": 229, "subject": "", "title": "\u8bf4\u4e00\u4e0b\u5bf9\u539f\u578b\u94fe\u7684\u7406\u89e3\uff0c\u753b\u4e00\u4e2a\u7ecf\u5178\u7684\u539f\u578b\u94fe\u56fe\u793a"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 297, "qtype": "short_answer", "short_answer": {"analysis": "### \u4f5c\u7528\u57df\u94fe\n\n\u5df2\u77e5 JavaScript \u4ee3\u7801\u6267\u884c\u4e00\u6bb5\u53ef\u6267\u884c\u4ee3\u7801\u65f6\uff0c\u4f1a\u521b\u5efa\u5bf9\u5e94\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u3002\u5728\u521b\u5efa\u6267\u884c\u4e0a\u4e0b\u6587\u65f6\uff0c\u4f1a\u521b\u5efa\u4f5c\u7528\u57df\u94fe\uff08\u56e0\u4e3a\u5f15\u7528\u4e86\u5916\u90e8\u73af\u5883\uff09\u3002\n\n\u67e5\u627e\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u4f1a\u5148\u4ece\u5f53\u524d\u4e0a\u4e0b\u6587\u7684\u73af\u5883\u8bb0\u5f55\u4e2d\u67e5\u627e\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0c\u5c31\u4f1a\u4ece\u4e0a\u7ea7\u6267\u884c\u4e0a\u4e0b\u6587\u7684\u73af\u5883\u8bb0\u5f55\u4e2d\u67e5\u627e\uff0c\u4e00\u76f4\u627e\u5230\u5168\u5c40\u4e0a\u4e0b\u6587\u7684\u73af\u5883\u8bb0\u5f55\u3002\u8fd9\u6837\u6709\u591a\u4e2a\u6267\u884c\u4e0a\u4e0b\u6587\u7684\u73af\u5883\u8bb0\u5f55\u6784\u6210\u7684\u94fe\u8868\uff0c\u5c31\u53eb\u505a\u4f5c\u7528\u57df\u94fe\u3002", "answer": "", "biz_type": 1, "qid": 297, "subject": "", "title": "\u4ec0\u4e48\u662f\u4f5c\u7528\u57df\u94fe"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 9, "qid": 296, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n\u8c03\u7528XMLHttpRequest.abort(), \u53ef\u4ee5\u5728\u8bf7\u6c42\u53d1\u51fa\u540e\uff0c\u7acb\u523b\u4e2d\u6b62\u8bf7\u6c42\u3002\n\n\u518d\u8bbe\u7f6e\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u5b9a\u65f6\u5668\u8ba1\u65f6\u5b8c\u6210\u540e\uff0c\u5982\u679c\u6ca1\u6709\u6570\u636e\u8fd4\u56de\u5219\u4e2d\u65ad\u8bf7\u6c42\u3002\n\n\u7b80\u5355\u5b9e\u73b0\n\n```js\n/**\n * @param {Object} params \n */\nconst request = (params) => {\n const option = {\n timeOut: 5000,\n ...params\n }\n return new Promise((resolve, reject) => {\n const xhr = new XMLHttpRequest()\n let isTimeOut = false\n const timer = setTimeout(function () {\n isTimeOut = true;\n xhr.abort();\n reject('request is timeout \uff01\uff01\uff01')\n }, option.timeOut)\n xhr.open(\"GET\", option.url);\n xhr.onreadystatechange = function () {\n if (xhr.readyState === 4) {\n if (isTimeOut) return;//\u5ffd\u7565\u4e2d\u6b62\u8bf7\u6c42\n clearTimeout(timer);//\u53d6\u6d88\u7b49\u5f85\u7684\u8d85\u65f6\n if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {\n resolve(xhr.responseText)\n } else {\n reject(`Request was unsuccessful \uff01\uff01\uff01 ${xhr.status}`)\n }\n }\n }\n // \u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u8bf7\u6c42\u65b9\u6cd5\u53d1\u9001\u6570\u636e\n xhr.send(null);\n })\n}\n```\n\n\u8c03\u7528\uff1a\n\n```js\nconst getData = async () => {\n const opt = {\n url: 'http://localhost:3000/timeout'\n }\n try {\n const res = await request(opt)\n } catch (error) {\n console.log(error) // request is timeout \uff01\uff01\uff01\n }\n}\n\ngetData()\n```", "answer": "", "biz_type": 1, "qid": 296, "subject": "", "title": "\u5b9e\u73b0\u4e00\u4e2a\u529f\u80fd\uff0c\u53d1\u9001\u8bf7\u6c42 5s \u65f6\u95f4\u540e\uff0c\u5982\u679c\u6ca1\u6709\u6570\u636e\u8fd4\u56de\uff0c\u4e2d\u65ad\u8bf7\u6c42,\u63d0\u793a\u9519\u8bef"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 11, "qid": 184, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n```js\nfunction getPathSum(root){\n let num = 0;\n\n\tfunction addNum(node) {\n\t\tif(node.left) {\n\t\t\tnum += (node.value + node.left.value)\n\t\t\tif(node.left.left) {\n\t\t\t\taddNum(node.left)\n\t\t\t} else if(node.left.right) {\n\t\t\t\taddNum(node.left)\n\t\t\t}\n\t\t}\n\t\tif(node.right) {\n\t\t\tnum += (node.value + node.right.value)\n\t\t\tif(node.right.left) {\n\t\t\t\taddNum(node.right)\n\t\t\t} else if(node.right.right) {\n\t\t\t\taddNum(node.right)\n\t\t\t}\n\t\t}\n\t}\n\n\taddNum(node)\n\tconsole.log(num)\n}\nclass TreeNode {}\nconst node = new TreeNode();\nnode.value = 1;\nnode.left = new TreeNode();\nnode.left.value = 2;\nnode.right = new TreeNode();\nnode.right.value = 3;\n// \u4e0b\u9762\u6570\u636e\u4fbf\u4e8e\u6d4b\u8bd5\u4f7f\u7528 \u7ed3\u679c\uff1a39\nnode.left.left = new TreeNode();\nnode.left.left.value = 4\nnode.left.right = new TreeNode();\nnode.left.right.value = 5\nnode.right.left = new TreeNode();\nnode.right.left.value = 6\nnode.right.right = new TreeNode();\nnode.right.right.value = 7\n```\n", "answer": "", "biz_type": 1, "qid": 184, "subject": "```js\nclass TreeNode{\n value:number\n left?:TreeNode\n right?:TreeNode\n}\nfunction getPathSum(root){\n // your code\n}\n// \u4f8b\u5b50\uff0c\u4e00\u5c42\u4e8c\u53c9\u6811\u5982\u4e0b\u5b9a\u4e49\uff0c\u8def\u5f84\u5305\u62ec1 \u2014> 2 ,1 -> 3\nconst node = new TreeNode();\nnode.value = 1;\nnode.left = new TreeNode();\nnode.left.value = 2;\nnode.right = new TreeNode();\nnode.right.value = 3;\ngetPathSum(node); // return 7 = (1+2) + (1+3)\n```\n", "title": "\u5b9e\u73b0\u51fd\u6570\u63a5\u53d7\u4efb\u610f\u4e8c\u53c9\u6811\uff0c\u6c42\u4e8c\u53c9\u6811\u6240\u6709\u6839\u5230\u53f6\u5b50\u8def\u5f84\u7ec4\u6210\u7684\u6570\u5b57\u4e4b\u548c"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 8, "qid": 213, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n```js\nfunction add() {\n // your code\n return arguments[0].reduce((a,b)=>a+b)\n}\nfunction one() {\n // your code\n if(arguments.length==0){\n return 1\n }else{\n return [arguments[0],1]\n }\n \n}\nfunction two() {\n if(arguments.length==0){\n return 2\n }else{\n return [arguments[0],2]\n }\n}\n\nconsole.log(add(one(two()))); //3\nconsole.log(add(two(one()))); //3\n```", "answer": "", "biz_type": 1, "qid": 213, "subject": "```js\nfunction add() {\n // your code\n}\nfunction one() {\n // your code\n}\nfunction two() {\n // your code\n}\nconsole.log(add(one(two()))); //3\nconsole.log(add(two(one()))); //3\n```\n", "title": "\u5b9e\u73b0\u4ee5\u4e0b\u4ee3\u7801"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u8109\u8109"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 9, "qid": 232, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n```js\nconst promiseAjax = function(data){\n function formatParams(param) {\n var arr = [];\n for (var name in param) {\n arr.push(encodeURIComponent(name) + \"=\" + encodeURIComponent(param[name]));\n }\n arr.push((\"v=\" + Math.random()).replace(\".\", \"\"));\n return arr.join(\"&\");\n }\n if (!data) data = {}\n data.params= data.params || {}\n\n return new Promise((resolve, reject) => {\n const xhr = new XMLHttpRequest();\n\n if (data.type === 'get') {\n data.params = formatParams(data.params); //options.data\u8bf7\u6c42\u7684\u6570\u636e\n\n xhr.open(\"GET\", data.url + \"?\" + data.params, true);\n xhr.send(null);\n } else if (options.type == \"post\") {\n xhr.open(\"POST\", data.url, true);\n xhr.setRequestHeader(\"Content-type\", \"application/json\");\n xhr.send(data.params);\n }\n\n xhr.onreadystatechange = function () {\n if (xhr.readyState == 4) {\n if (xhr.status === 200) {\n resolve(xhr.response)\n } else {\n reject(xhr.responseText);\n }\n }\n }\n })\n}\n```", "answer": "", "biz_type": 1, "qid": 232, "subject": "", "title": "\u7528 Promise \u5c01\u88c5\u4e00\u4e2a ajax"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9ad8\u5fb7"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 198, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001\u9519\u8bef\u5206\u7c7b\n\n1. \u8fd0\u884c\u65f6\u9519\u8bef\uff1a\u8fd9\u4e2a\u9519\u8bef\u5f80\u5f80\u662f\u5728\u5199\u4ee3\u7801\u662f\u9020\u6210\u7684\u3002\u5982\u8bed\u6cd5\u9519\u8bef\u3001\u903b\u8f91\u9519\u8bef\u7b49\uff0c\u8fd9\u79cd\u9519\u8bef\u4e00\u822c\u5728\u6d4b\u8bd5\u8fc7\u7a0b\u4e5f\u80fd\u591f\u53d1\u73b0\u3002\n2. \u8d44\u6e90\u52a0\u8f7d\u9519\u8bef\uff1a\u8fd9\u4e2a\u9519\u8bef\u901a\u5e38\u662f\u627e\u4e0d\u5230\u6587\u4ef6\u6216\u8005\u662f\u6587\u4ef6\u52a0\u8f7d\u8d85\u65f6\u9020\u6210\u7684\u3002\n\n### \u4e8c\u3001\u9519\u8bef\u6355\u83b7\n\n#### 1\uff09\u4ee3\u7801\u9519\u8bef\u6355\u83b7\n\n- try\u2026catch\u2026\n\n```js\ntry{\n //\u8fd0\u884c\u53ef\u80fd\u51fa\u9519\u7684\u4ee3\u7801 \n}catch(e) {\n //\u6355\u83b7\u9519\u8bef\n}\n```\n\n- window.onerror\n\n```js\nwindow.onerror = function() {\n //\u6355\u83b7\u9519\u8bef \n```\n\n```js\n/**\n\u540c\u6b65\u9519\u8bef\n * @param {String} msg \u9519\u8bef\u4fe1\u606f\n * @param {String} url \u51fa\u9519\u6587\u4ef6\n * @param {Number} row \u884c\u53f7\n * @param {Number} col \u5217\u53f7\n * @param {Object} error \u9519\u8bef\u8be6\u7ec6\u4fe1\u606f\n */\n window.onerror = function (msg, url, row, col, error) {\n console.log('\u6211\u77e5\u9053\u9519\u8bef\u4e86');\n console.log({\n msg, url, row, col, error\n })\n return true;\n};\n\nerror\n```\n\n```js\n//\u5f02\u6b65\u9519\u8bef\nwindow.onerror = function (msg, url, row, col, error) {\n console.log('\u6211\u77e5\u9053\u5f02\u6b65\u9519\u8bef\u4e86');\n console.log({\n msg, url, row, col, error\n })\n return true;\n};\nsetTimeout(() => {\n error;\n});\n```\n\n\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cwindow.onerror\u51fd\u6570\u53ea\u6709\u5728\u8fd4\u56de true\u7684\u65f6\u5019\uff0c\u5f02\u5e38\u624d\u4e0d\u4f1a\u5411\u4e0a\u629b\u51fa\uff0c\u5426\u5219\u5373\u4f7f\u662f\u77e5\u9053\u5f02\u5e38\u7684\u53d1\u751f\u63a7\u5236\u53f0\u8fd8\u662f\u4f1a\u663e\u793aUncaught Error: xxxxx\u3002\n\n\u7531\u4e8e\u7f51\u7edc\u8bf7\u6c42\u5f02\u5e38\u4e0d\u4f1a\u4e8b\u4ef6\u5192\u6ce1\uff0c\u56e0\u6b64\u5fc5\u987b\u5728\u6355\u83b7\u9636\u6bb5\u5c06\u5176\u6355\u6349\u5230\u624d\u884c\uff0c\u4f46\u662f\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u53ef\u4ee5\u6355\u6349\u5230\u7f51\u7edc\u8bf7\u6c42\u7684\u5f02\u5e38\uff0c\u4f46\u662f\u65e0\u6cd5\u5224\u65adHTTP\u7684\u72b6\u6001\u662f404\u8fd8\u662f\u5176\u4ed6\u6bd4\u5982500\u7b49\u7b49\uff0c\u6240\u4ee5\u8fd8\u9700\u8981\u914d\u5408\u670d\u52a1\u7aef\u65e5\u5fd7\u624d\u8fdb\u884c\u6392\u67e5\u5206\u6790\u624d\u53ef\u4ee5\u3002\n\n```js\n<script>\nwindow.addEventListener('error', (msg, url, row, col, error) => {\n console.log('\u6211\u77e5\u9053 404 \u9519\u8bef\u4e86');\n console.log(\n msg, url, row, col, error\n );\n return true;\n}, true);\n</script>\n<img src=\"./404.png\" alt=\"\">\n```\n\n\u5728\u5b9e\u9645\u7684\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0conerror\u4e3b\u8981\u662f\u6765\u6355\u83b7\u9884\u6599\u4e4b\u5916\u7684\u9519\u8bef\uff0c\u800ctry-catch\u5219\u662f\u7528\u6765\u5728\u53ef\u9884\u89c1\u60c5\u51b5\u4e0b\u76d1\u63a7\u7279\u5b9a\u7684\u9519\u8bef\uff0c\u4e24\u8005\u7ed3\u5408\u4f7f\u7528\u66f4\u52a0\u9ad8\u6548\u3002\n\n#### 2\uff09\u8d44\u6e90\u52a0\u8f7d\u9519\u8bef\n\n- Object.onerror\n\n```js\nvar img=document.getElementById('#img');\nimg.onerror = function() {\n // \u6355\u83b7\u9519\u8bef \n}\n```\n\n\u5229\u7528window\u7684error\u4e8b\u4ef6\u4ee3\u7406\uff0c\u4f46\u662f\u9700\u8981\u6ce8\u610f\u7684\u662ferror\u4e8b\u4ef6\u662f\u4e0d\u5192\u6ce1\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e8b\u4ef6\u6355\u83b7\u8fdb\u884c\u4ee3\u7406\n\n```js\n window.addElementListener(\"error\",function(){\n // \u6355\u83b7\u9519\u8bef\n},true);\n```\n\n### \u4e09\u3001\u9519\u8bef\u4e0a\u62a5\n\n\u5e38\u89c1\u7684\u9519\u8bef\u4e0a\u62a5\u6709\u4e24\u79cd\uff1aajax\u3001image\u5bf9\u8c61\uff08\u63a8\u8350\uff09\n\najax\u4e0a\u62a5\u5c31\u662f\u5728\u4e0a\u6587\u6ce8\u91ca\u9519\u8bef\u6355\u83b7\u7684\u5730\u65b9\u53d1\u8d77ajax\u8bf7\u6c42\uff0c\u6765\u5411\u670d\u52a1\u5668\u53d1\u9001\u9519\u8bef\u4fe1\u606f\n\n- \u5229\u7528image\u5bf9\u8c61\n\n```js\nfunction report(error) {\nvar reportUrl = 'http://xxxx/report';\nnew Image().src = reportUrl + '?' + 'error=' + error;\n}\n```\n\n### \u56db\u3001\u8de8\u57dfjs\u6587\u4ef6\u9519\u8bef\u83b7\u53d6\n\n\u8de8\u57dfjs\u6587\u4ef6\u83b7\u53d6\u662f\u6709\u9650\u5236\u7684\uff0c\u5982\u679c\u60f3\u83b7\u53d6\u5176\u4ed6\u57df\u4e0b\u7684js\u9519\u8bef\u9700\u8981\u518dscript\u6807\u7b7e\u4e2d\u6dfb\u52a0crossorgin\u5c5e\u6027\uff0c\u7136\u540e\u670d\u52a1\u5668\u8981\u8bbe\u7f6eheader('Access-Control-Allow-Origin');\n\n```js\n// http://localhost:8080/index.html\n<script>\n window.onerror = function (msg, url, row, col, error) {\n console.log('\u6211\u77e5\u9053\u9519\u8bef\u4e86\uff0c\u4e5f\u77e5\u9053\u9519\u8bef\u4fe1\u606f');\n console.log({\n msg, url, row, col, error\n })\n return true;\n };\n</script>\n<script src=\"http://localhost:8081/test.js\" crossorigin></script>\n\n// http://localhost:8081/test.js\nsetTimeout(() => {\n console.log(error);\n});\n```\n\n### \u4e94\u3001\u65e5\u5fd7\u5206\u7b49\u7ea7\n\n\u65e5\u5fd7\uff0c\u662f\u6211\u4eec\u7528\u4e8e\u8f93\u51fa\u7cfb\u7edf\u6d88\u606f\u7684\u4e00\u4e9b\u8282\u70b9\uff0c \u4f46\u662f\u7531\u4e8e\u4e1a\u52a1\u3001\u7f16\u7801\u4f18\u5148\u7ea7\u7684\u4e0d\u540c\uff0c\u65e5\u5fd7\u9700\u8981\u901a\u8fc7\u5b9a\u4e49\u4e0d\u540c\u7684\u57fa\u672c\u6765\u8fdb\u884c\u8f93\u51fa\n\n\u6bd4\u5982\u53ef\u4ee5\u53c2\u8003chrome\u7684\u8f93\u51fa\u5212\u5206\n\n- log \u666e\u901a\u65e5\u5fd7\u3002\n- info \u666e\u901a\u4fe1\u606f\u3002\n- warn \u8b66\u544a\u503c\u606f\u3002\n- error \u9519\u8bef\u503c\u606f\u3002", "answer": "", "biz_type": 1, "qid": 198, "subject": "", "title": "\u8bf4\u4e00\u4e0b\u9519\u8bef\u76d1\u63a7\u7684\u5b9e\u73b0\uff0c\u9519\u8bef\u76d1\u63a7\u7684\u6b63\u786e\u4f7f\u7528\u65b9\u5f0f\uff0c\u65e5\u5fd7\u5982\u4f55\u5206\u7b49\u7ea7"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u864e\u6251"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 271, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001Set & Map\n\nSet \u548c Map \u4e3b\u8981\u7684\u5e94\u7528\u573a\u666f\u5728\u4e8e \u6570\u636e\u91cd\u7ec4 \u548c \u6570\u636e\u50a8\u5b58\n\nSet \u662f\u4e00\u79cd\u53eb\u505a\u96c6\u5408\u7684\u6570\u636e\u7ed3\u6784\uff0cMap \u662f\u4e00\u79cd\u53eb\u505a\u5b57\u5178\u7684\u6570\u636e\u7ed3\u6784\n\n#### 1.\u96c6\u5408 (Set)\n\nES6 \u65b0\u589e\u7684\u4e00\u79cd\u65b0\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u4e8e\u6570\u7ec4\uff0c\u4f46\u6210\u5458\u662f\u552f\u4e00\u4e14\u65e0\u5e8f\u7684\uff0c\u6ca1\u6709\u91cd\u590d\u7684\u503c\u3002\n\nSet \u672c\u8eab\u662f\u4e00\u79cd\u6784\u9020\u51fd\u6570\uff0c\u7528\u6765\u751f\u6210 Set \u6570\u636e\u7ed3\u6784\u3002\n\n```js\nnew Set([iterable])\n```\n\n\u4e3e\u4e2a\u4f8b\u5b50\uff1a\n\n```js\nconst s = new Set()\n[1, 2, 3, 4, 3, 2, 1].forEach(x => s.add(x))\n\nfor (let i of s) {\n console.log(i)\t// 1 2 3 4\n}\n\n// \u53bb\u91cd\u6570\u7ec4\u7684\u91cd\u590d\u5bf9\u8c61\nlet arr = [1, 2, 3, 2, 1, 1]\n[... new Set(arr)]\t// [1, 2, 3]\n\n```\n\nSet \u5bf9\u8c61\u5141\u8bb8\u4f60\u50a8\u5b58\u4efb\u4f55\u7c7b\u578b\u7684\u552f\u4e00\u503c\uff0c\u65e0\u8bba\u662f\u539f\u59cb\u503c\u6216\u8005\u662f\u5bf9\u8c61\u5f15\u7528\u3002\n\n\u5411 Set \u52a0\u5165\u503c\u7684\u65f6\u5019\uff0c\u4e0d\u4f1a\u53d1\u751f\u7c7b\u578b\u8f6c\u6362\uff0c\u6240\u4ee55\u548c\"5\"\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u503c\u3002Set \u5185\u90e8\u5224\u65ad\u4e24\u4e2a\u503c\u662f\u5426\u4e0d\u540c\uff0c\u4f7f\u7528\u7684\u7b97\u6cd5\u53eb\u505a\u201cSame-value-zero equality\u201d\uff0c\u5b83\u7c7b\u4f3c\u4e8e\u7cbe\u786e\u76f8\u7b49\u8fd0\u7b97\u7b26\uff08===\uff09\uff0c\u4e3b\u8981\u7684\u533a\u522b\u662f**NaN\u7b49\u4e8e\u81ea\u8eab\uff0c\u800c\u7cbe\u786e\u76f8\u7b49\u8fd0\u7b97\u7b26\u8ba4\u4e3aNaN\u4e0d\u7b49\u4e8e\u81ea\u8eab\u3002**\n\n```js\nlet set = new Set();\nlet a = NaN;\nlet b = NaN;\nset.add(a);\nset.add(b);\nset // Set {NaN}\n\nlet set1 = new Set()\nset1.add(5)\nset1.add('5')\nconsole.log([...set1])\t// [5, \"5\"]\n```\n\n**1)Set \u5b9e\u4f8b\u5c5e\u6027**\n\n- constructor\uff1a \u6784\u9020\u51fd\u6570\n- size\uff1a\u5143\u7d20\u6570\u91cf\n\n```js\nlet set = new Set([1, 2, 3, 2, 1])\n\nconsole.log(set.length)\t// undefined\nconsole.log(set.size)\t// 3\n```\n\n**2)Set \u5b9e\u4f8b\u65b9\u6cd5**\n\n- \u64cd\u4f5c\u65b9\u6cd5\n - add(value)\uff1a\u65b0\u589e\uff0c\u76f8\u5f53\u4e8e array\u91cc\u7684push\n - delete(value)\uff1a\u5b58\u5728\u5373\u5220\u9664\u96c6\u5408\u4e2dvalue\n - has(value)\uff1a\u5224\u65ad\u96c6\u5408\u4e2d\u662f\u5426\u5b58\u5728 value\n - clear()\uff1a\u6e05\u7a7a\u96c6\u5408\n\n```js\nlet set = new Set()\nset.add(1).add(2).add(1)\n\nset.has(1)\t// true\nset.has(3)\t// false\nset.delete(1)\t\nset.has(1)\t// false\n```\n\nArray.from \u65b9\u6cd5\u53ef\u4ee5\u5c06 Set \u7ed3\u6784\u8f6c\u4e3a\u6570\u7ec4\n \n```js\nconst items = new Set([1, 2, 3, 2])\nconst array = Array.from(items)\nconsole.log(array)\t// [1, 2, 3]\n// \u6216\nconst arr = [...items]\nconsole.log(arr)\t// [1, 2, 3]\n```\n\n**3)\u904d\u5386\u65b9\u6cd5\uff08\u904d\u5386\u987a\u5e8f\u4e3a\u63d2\u5165\u987a\u5e8f\uff09**\n\n- keys()\uff1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u96c6\u5408\u4e2d\u6240\u6709\u952e\u7684\u8fed\u4ee3\u5668\n- values()\uff1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u96c6\u5408\u4e2d\u6240\u6709\u503c\u5f97\u8fed\u4ee3\u5668\n- entries()\uff1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542bSet\u5bf9\u8c61\u4e2d\u6240\u6709\u5143\u7d20\u5f97\u952e\u503c\u5bf9\u8fed\u4ee3\u5668\n- forEach(callbackFn, thisArg)\uff1a\u7528\u4e8e\u5bf9\u96c6\u5408\u6210\u5458\u6267\u884ccallbackFn\u64cd\u4f5c\uff0c\u5982\u679c\u63d0\u4f9b\u4e86 thisArg \u53c2\u6570\uff0c\u56de\u8c03\u4e2d\u7684this\u4f1a\u662f\u8fd9\u4e2a\u53c2\u6570\uff0c\u6ca1\u6709\u8fd4\u56de\u503c\n\n```js\nlet set = new Set([1, 2, 3])\nconsole.log(set.keys())\t// SetIterator {1, 2, 3}\nconsole.log(set.values())\t// SetIterator {1, 2, 3}\nconsole.log(set.entries())\t// SetIterator {1, 2, 3}\n\nfor (let item of set.keys()) {\nconsole.log(item);\n}\t// 1\t2\t 3\nfor (let item of set.entries()) {\nconsole.log(item);\n}\t// [1, 1]\t[2, 2]\t[3, 3]\n\nset.forEach((value, key) => {\n console.log(key + ' : ' + value)\n})\t// 1 : 1\t2 : 2\t3 : 3\nconsole.log([...set])\t// [1, 2, 3]\n\n```\n\nSet \u53ef\u9ed8\u8ba4\u904d\u5386\uff0c\u9ed8\u8ba4\u8fed\u4ee3\u5668\u751f\u6210\u51fd\u6570\u662f values() \u65b9\u6cd5\n\n```js\nSet.prototype[Symbol.iterator] === Set.prototype.values\t// true\n```\n\n\u6240\u4ee5\uff0c Set\u53ef\u4ee5\u4f7f\u7528 map\u3001filter \u65b9\u6cd5\n\n```js\nlet set = new Set([1, 2, 3])\nset = new Set([...set].map(item => item * 2))\nconsole.log([...set])\t// [2, 4, 6]\n\nset = new Set([...set].filter(item => (item >= 4)))\nconsole.log([...set])\t//[4, 6]\n```\n\n\u56e0\u6b64\uff0cSet \u5f88\u5bb9\u6613\u5b9e\u73b0\u4ea4\u96c6\uff08Intersect\uff09\u3001\u5e76\u96c6\uff08Union\uff09\u3001\u5dee\u96c6\uff08Difference\uff09\n\n```js\nlet set1 = new Set([1, 2, 3])\nlet set2 = new Set([4, 3, 2])\n\nlet intersect = new Set([...set1].filter(value => set2.has(value)))\nlet union = new Set([...set1, ...set2])\nlet difference = new Set([...set1].filter(value => !set2.has(value)))\n\nconsole.log(intersect)\t// Set {2, 3}\nconsole.log(union)\t\t// Set {1, 2, 3, 4}\nconsole.log(difference)\t// Set {1}\n```\n\n#### 2.\u5b57\u5178\uff08Map\uff09\n\n**\u96c6\u5408 \u4e0e \u5b57\u5178 \u7684\u533a\u522b\uff1a**\n\n- \u5171\u540c\u70b9\uff1a\u96c6\u5408\u3001\u5b57\u5178 \u53ef\u4ee5\u50a8\u5b58\u4e0d\u91cd\u590d\u7684\u503c\n- \u4e0d\u540c\u70b9\uff1a\u96c6\u5408 \u662f\u4ee5 [value, value]\u7684\u5f62\u5f0f\u50a8\u5b58\u5143\u7d20\uff0c\u5b57\u5178 \u662f\u4ee5 [key, value] \u7684\u5f62\u5f0f\u50a8\u5b58\n\n```js\nconst m = new Map()\nconst o = {p: 'haha'}\nm.set(o, 'content')\nm.get(o)\t// content\n\nm.has(o)\t// true\nm.delete(o)\t// true\nm.has(o)\t// false\n```\n\n**\u4efb\u4f55\u5177\u6709 Iterator \u63a5\u53e3\u3001\u4e14\u6bcf\u4e2a\u6210\u5458\u90fd\u662f\u4e00\u4e2a\u53cc\u5143\u7d20\u7684\u6570\u7ec4\u7684\u6570\u636e\u7ed3\u6784**\u90fd\u53ef\u4ee5\u5f53\u4f5cMap\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570\uff0c\u4f8b\u5982\uff1a\n\n```js\nconst set = new Set([\n ['foo', 1],\n ['bar', 2]\n]);\nconst m1 = new Map(set);\nm1.get('foo') // 1\n\nconst m2 = new Map([['baz', 3]]);\nconst m3 = new Map(m2);\nm3.get('baz') // 3\n```\n\n\u5982\u679c\u8bfb\u53d6\u4e00\u4e2a\u672a\u77e5\u7684\u952e\uff0c\u5219\u8fd4\u56deundefined\u3002\n\n```js\nnew Map().get('asfddfsasadf')\n// undefined\n```\n\n\u6ce8\u610f\uff0c\u53ea\u6709\u5bf9\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\uff0cMap \u7ed3\u6784\u624d\u5c06\u5176\u89c6\u4e3a\u540c\u4e00\u4e2a\u952e\u3002\u8fd9\u4e00\u70b9\u8981\u975e\u5e38\u5c0f\u5fc3\u3002\n\n```js\nconst map = new Map();\n\nmap.set(['a'], 555);\nmap.get(['a']) // undefined\n```\n\n\u4e0a\u9762\u4ee3\u7801\u7684`set`\u548c`get`\u65b9\u6cd5\uff0c\u8868\u9762\u662f\u9488\u5bf9\u540c\u4e00\u4e2a\u952e\uff0c\u4f46\u5b9e\u9645\u4e0a\u8fd9\u662f\u4e24\u4e2a\u503c\uff0c\u5185\u5b58\u5730\u5740\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u56e0\u6b64`get`\u65b9\u6cd5\u65e0\u6cd5\u8bfb\u53d6\u8be5\u952e\uff0c\u8fd4\u56de`undefined\u3002`\n\n\u7531\u4e0a\u53ef\u77e5\uff0c`Map` \u7684\u952e\u5b9e\u9645\u4e0a\u662f\u8ddf\u5185\u5b58\u5730\u5740\u7ed1\u5b9a\u7684\uff0c\u53ea\u8981\u5185\u5b58\u5730\u5740\u4e0d\u4e00\u6837\uff0c\u5c31\u89c6\u4e3a\u4e24\u4e2a\u952e\u3002\u8fd9\u5c31\u89e3\u51b3\u4e86\u540c\u540d\u5c5e\u6027\u78b0\u649e\uff08`clash`\uff09\u7684\u95ee\u9898\uff0c\u6211\u4eec\u6269\u5c55\u522b\u4eba\u7684\u5e93\u7684\u65f6\u5019\uff0c\u5982\u679c\u4f7f\u7528\u5bf9\u8c61\u4f5c\u4e3a\u952e\u540d\uff0c\u5c31\u4e0d\u7528\u62c5\u5fc3\u81ea\u5df1\u7684\u5c5e\u6027\u4e0e\u539f\u4f5c\u8005\u7684\u5c5e\u6027\u540c\u540d\u3002\n\n\u5982\u679c `Map` \u7684\u952e\u662f\u4e00\u4e2a\u7b80\u5355\u7c7b\u578b\u7684\u503c\uff08\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u5e03\u5c14\u503c\uff09\uff0c\u5219\u53ea\u8981\u4e24\u4e2a\u503c\u4e25\u683c\u76f8\u7b49\uff0c`Map` \u5c06\u5176\u89c6\u4e3a\u4e00\u4e2a\u952e\uff0c\u6bd4\u5982`0`\u548c`-0`\u5c31\u662f\u4e00\u4e2a\u952e\uff0c\u5e03\u5c14\u503c`true`\u548c\u5b57\u7b26\u4e32`true`\u5219\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u952e\u3002\u53e6\u5916\uff0c`undefined`\u548c`null`\u4e5f\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u952e\u3002\u867d\u7136`NaN`\u4e0d\u4e25\u683c\u76f8\u7b49\u4e8e\u81ea\u8eab\uff0c\u4f46 `Map` \u5c06\u5176\u89c6\u4e3a\u540c\u4e00\u4e2a\u952e\u3002\n\n```js\nlet map = new Map();\n\nmap.set(-0, 123);\nmap.get(+0) // 123\n\nmap.set(true, 1);\nmap.set('true', 2);\nmap.get(true) // 1\n\nmap.set(undefined, 3);\nmap.set(null, 4);\nmap.get(undefined) // 3\n\nmap.set(NaN, 123);\nmap.get(NaN) // 123\n```\n\n**Map \u7684\u5c5e\u6027\u53ca\u65b9\u6cd5**\n\n\u5c5e\u6027\uff1a\n\n- constructor\uff1a\u6784\u9020\u51fd\u6570\n- size\uff1a\u8fd4\u56de\u5b57\u5178\u4e2d\u6240\u5305\u542b\u7684\u5143\u7d20\u4e2a\u6570\n\n```js\nconst map = new Map([\n ['name', 'An'],\n ['des', 'JS']\n]);\n\nmap.size // 2\n```\n\n\u64cd\u4f5c\u65b9\u6cd5\uff1a\n\n- set(key, value)\uff1a\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u5143\u7d20\n- get(key)\uff1a\u901a\u8fc7\u952e\u67e5\u627e\u7279\u5b9a\u7684\u6570\u503c\u5e76\u8fd4\u56de\n- has(key)\uff1a\u5224\u65ad\u5b57\u5178\u4e2d\u662f\u5426\u5b58\u5728\u952ekey\n- delete(key)\uff1a\u901a\u8fc7\u952e key \u4ece\u5b57\u5178\u4e2d\u79fb\u9664\u5bf9\u5e94\u7684\u6570\u636e\n- clear()\uff1a\u5c06\u8fd9\u4e2a\u5b57\u5178\u4e2d\u7684\u6240\u6709\u5143\u7d20\u5220\u9664\n\n\u904d\u5386\u65b9\u6cd5\n\n- `Keys()`\uff1a\u5c06\u5b57\u5178\u4e2d\u5305\u542b\u7684\u6240\u6709\u952e\u540d\u4ee5\u8fed\u4ee3\u5668\u5f62\u5f0f\u8fd4\u56de\n- `values()`\uff1a\u5c06\u5b57\u5178\u4e2d\u5305\u542b\u7684\u6240\u6709\u6570\u503c\u4ee5\u8fed\u4ee3\u5668\u5f62\u5f0f\u8fd4\u56de\n- `entries()`\uff1a\u8fd4\u56de\u6240\u6709\u6210\u5458\u7684\u8fed\u4ee3\u5668\n- `forEach()`\uff1a\u904d\u5386\u5b57\u5178\u7684\u6240\u6709\u6210\u5458\n\n```js\nconst map = new Map([\n ['name', 'An'],\n ['des', 'JS']\n ]);\nconsole.log(map.entries())\t// MapIterator {\"name\" => \"An\", \"des\" => \"JS\"}\n\nconsole.log(map.keys()) // MapIterator {\"name\", \"des\"}\n```\n\n`Map` \u7ed3\u6784\u7684\u9ed8\u8ba4\u904d\u5386\u5668\u63a5\u53e3\uff08`Symbol.iterator\u5c5e\u6027`\uff09\uff0c\u5c31\u662fentries\u65b9\u6cd5\u3002\n\n```js\nmap[Symbol.iterator] === map.entries\n// true\n```\n\n`Map` \u7ed3\u6784\u8f6c\u4e3a\u6570\u7ec4\u7ed3\u6784\uff0c\u6bd4\u8f83\u5feb\u901f\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u6269\u5c55\u8fd0\u7b97\u7b26\uff08`...`\uff09\u3002\n\n\u5bf9\u4e8e `forEach` \uff0c\u770b\u4e00\u4e2a\u4f8b\u5b50\n\n```js\nconst reporter = {\n report: function(key, value) {\n console.log(\"Key: %s, Value: %s\", key, value);\n }\n};\n\nlet map = new Map([\n ['name', 'An'],\n ['des', 'JS']\n])\nmap.forEach(function(value, key, map) {\n this.report(key, value);\n}, reporter);\n// Key: name, Value: An\n// Key: des, Value: JS\n```\n\n\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c forEach \u65b9\u6cd5\u7684\u56de\u8c03\u51fd\u6570\u7684 this\uff0c\u5c31\u6307\u5411 reporter\n\n\n**\u4e0e\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u7684\u76f8\u4e92\u8f6c\u6362**\n\n- Map \u8f6c Array\n\n```js\nconst map = new Map([[1, 1], [2, 2], [3, 3]])\nconsole.log([...map])\t// [[1, 1], [2, 2], [3, 3]]\n```\n\n- Array \u8f6c Map\n\n```js\nconst map = new Map([[1, 1], [2, 2], [3, 3]])\nconsole.log(map)\t// Map {1 => 1, 2 => 2, 3 => 3}\n```\n\n- Map \u8f6c Object\n\n\u56e0\u4e3a Object \u7684\u952e\u540d\u90fd\u4e3a\u5b57\u7b26\u4e32\uff0c\u800cMap \u7684\u952e\u540d\u4e3a\u5bf9\u8c61\uff0c\u6240\u4ee5\u8f6c\u6362\u7684\u65f6\u5019\u4f1a\u628a\u975e\u5b57\u7b26\u4e32\u952e\u540d\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u952e\u540d\u3002\n\n```js\nfunction mapToObj(map) {\n let obj = Object.create(null)\n for (let [key, value] of map) {\n obj[key] = value\n }\n return obj\n}\nconst map = new Map().set('name', 'An').set('des', 'JS')\nmapToObj(map) // {name: \"An\", des: \"JS\"}\n```\n\n- Object \u8f6c Map\n\n```js\nfunction objToMap(obj) {\n let map = new Map()\n for (let key of Object.keys(obj)) {\n map.set(key, obj[key])\n }\n return map\n}\n\nobjToMap({'name': 'An', 'des': 'JS'}) // Map {\"name\" => \"An\", \"des\" => \"JS\"}\n```\n\n- Map \u8f6c JSON\n\n```js\nfunction mapToJson(map) {\n return JSON.stringify([...map])\n}\n\nlet map = new Map().set('name', 'An').set('des', 'JS')\nmapToJson(map)\t// [[\"name\",\"An\"],[\"des\",\"JS\"]]\n```\n\n- JSON \u8f6c Map\n\n```js\nfunction jsonToStrMap(jsonStr) {\n return objToMap(JSON.parse(jsonStr));\n}\n\njsonToStrMap('{\"name\": \"An\", \"des\": \"JS\"}') // Map {\"name\" => \"An\", \"des\" => \"JS\"}\n```\n\n### \u4e8c\u3001weakSet & weakMap\n\n#### 1.weakSet\n\nWeakSet \u5bf9\u8c61\u5141\u8bb8\u4f60\u5c06\u5f31\u5f15\u7528\u5bf9\u8c61\u50a8\u5b58\u5728\u4e00\u4e2a\u96c6\u5408\u4e2d\n\n**WeakSet \u4e0e Set \u7684\u533a\u522b\uff1a**\n\n- WeakSet \u53ea\u80fd\u50a8\u5b58\u5bf9\u8c61\u5f15\u7528\uff0c\u4e0d\u80fd\u5b58\u653e\u503c\uff0c\u800c Set \u5bf9\u8c61\u90fd\u53ef\u4ee5\n- WeakSet \u5bf9\u8c61\u4e2d\u50a8\u5b58\u7684\u5bf9\u8c61\u503c\u90fd\u662f\u88ab\u5f31\u5f15\u7528\u7684\uff0c\u5373\u5783\u573e\u56de\u6536\u673a\u5236\u4e0d\u8003\u8651 WeakSet \u5bf9\u8be5\u5bf9\u8c61\u7684\u5e94\u7528\uff0c\u5982\u679c\u6ca1\u6709\u5176\u4ed6\u7684\u53d8\u91cf\u6216\u5c5e\u6027\u5f15\u7528\u8fd9\u4e2a\u5bf9\u8c61\u503c\uff0c\u5219\u8fd9\u4e2a\u5bf9\u8c61\u5c06\u4f1a\u88ab\u5783\u573e\u56de\u6536\u6389\uff08\u4e0d\u8003\u8651\u8be5\u5bf9\u8c61\u8fd8\u5b58\u5728\u4e8e WeakSet \u4e2d\uff09\uff0c\u6240\u4ee5\uff0cWeakSet \u5bf9\u8c61\u91cc\u6709\u591a\u5c11\u4e2a\u6210\u5458\u5143\u7d20\uff0c\u53d6\u51b3\u4e8e\u5783\u573e\u56de\u6536\u673a\u5236\u6709\u6ca1\u6709\u8fd0\u884c\uff0c\u8fd0\u884c\u524d\u540e\u6210\u5458\u4e2a\u6570\u53ef\u80fd\u4e0d\u4e00\u81f4\uff0c\u904d\u5386\u7ed3\u675f\u4e4b\u540e\uff0c\u6709\u7684\u6210\u5458\u53ef\u80fd\u53d6\u4e0d\u5230\u4e86\uff08\u88ab\u5783\u573e\u56de\u6536\u4e86\uff09\uff0cWeakSet \u5bf9\u8c61\u662f\u65e0\u6cd5\u88ab\u904d\u5386\u7684\uff08ES6 \u89c4\u5b9a WeakSet \u4e0d\u53ef\u904d\u5386\uff09\uff0c\u4e5f\u6ca1\u6709\u529e\u6cd5\u62ff\u5230\u5b83\u5305\u542b\u7684\u6240\u6709\u5143\u7d20\n\n\u5c5e\u6027\uff1a\n\n- constructor\uff1a\u6784\u9020\u51fd\u6570\uff0c\u4efb\u4f55\u4e00\u4e2a\u5177\u6709 Iterable \u63a5\u53e3\u7684\u5bf9\u8c61\uff0c\u90fd\u53ef\u4ee5\u4f5c\u53c2\u6570\n\n```js\nconst arr = [[1, 2], [3, 4]]\nconst weakset = new WeakSet(arr)\nconsole.log(weakset)\n```\n\n\n\u65b9\u6cd5\uff1a\n\n- add(value)\uff1a\u5728WeakSet \u5bf9\u8c61\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20value\n- has(value)\uff1a\u5224\u65ad WeakSet \u5bf9\u8c61\u4e2d\u662f\u5426\u5305\u542bvalue\n- delete(value)\uff1a\u5220\u9664\u5143\u7d20 value\n- clear()\uff1a\u6e05\u7a7a\u6240\u6709\u5143\u7d20\uff0c\u6ce8\u610f\u8be5\u65b9\u6cd5\u5df2\u5e9f\u5f03\n\n```js\nvar ws = new WeakSet()\nvar obj = {}\nvar foo = {}\n\nws.add(window)\nws.add(obj)\n\nws.has(window)\t// true\nws.has(foo)\t// false\n\nws.delete(window)\t// true\nws.has(window)\t// false\n```\n\n#### 2.weakMap\n\n`WeakMap` \u5bf9\u8c61\u662f\u4e00\u7ec4\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u5176\u4e2d\u7684\u952e\u662f\u5f31\u5f15\u7528\u5bf9\u8c61\uff0c\u800c\u503c\u53ef\u4ee5\u662f\u4efb\u610f\u3002\n\n\u6ce8\u610f\uff0c`WeakMap` \u5f31\u5f15\u7528\u7684\u53ea\u662f\u952e\u540d\uff0c\u800c\u4e0d\u662f\u952e\u503c\u3002\u952e\u503c\u4f9d\u7136\u662f\u6b63\u5e38\u5f15\u7528\u3002\n\n`WeakMap` \u4e2d\uff0c\u6bcf\u4e2a\u952e\u5bf9\u81ea\u5df1\u6240\u5f15\u7528\u5bf9\u8c61\u7684\u5f15\u7528\u90fd\u662f\u5f31\u5f15\u7528\uff0c\u5728\u6ca1\u6709\u5176\u4ed6\u5f15\u7528\u548c\u8be5\u952e\u5f15\u7528\u540c\u4e00\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u5c06\u4f1a\u88ab\u5783\u573e\u56de\u6536\uff08\u76f8\u5e94\u7684key\u5219\u53d8\u6210\u65e0\u6548\u7684\uff09\uff0c\u6240\u4ee5\uff0c`WeakMap` \u7684 `key` \u662f\u4e0d\u53ef\u679a\u4e3e\u7684\u3002\n\n- \u5c5e\u6027\uff1a\n - constructor\uff1a\u6784\u9020\u51fd\u6570\n\n- \u65b9\u6cd5\uff1a\n - has(key)\uff1a\u5224\u65ad\u662f\u5426\u6709 key \u5173\u8054\u5bf9\u8c61\n - get(key)\uff1a\u8fd4\u56dekey\u5173\u8054\u5bf9\u8c61\uff08\u6ca1\u6709\u5219\u5219\u8fd4\u56de undefined\uff09\n - set(key)\uff1a\u8bbe\u7f6e\u4e00\u7ec4key\u5173\u8054\u5bf9\u8c61\n - delete(key)\uff1a\u79fb\u9664 key \u7684\u5173\u8054\u5bf9\u8c61\n\n```js\nlet myElement = document.getElementById('logo');\nlet myWeakmap = new WeakMap();\nmyWeakmap.set(myElement, {timesClicked: 0});\n\nmyElement.addEventListener('click', function() {\nlet logoData = myWeakmap.get(myElement);\nlogoData.timesClicked++;\n}, false);\n```\n\n### \u4e09\u3001\u603b\u7ed3\n\n- Set\n - \u6210\u5458\u552f\u4e00\u3001\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\n - [value, value]\uff0c\u952e\u503c\u4e0e\u952e\u540d\u662f\u4e00\u81f4\u7684\uff08\u6216\u8005\u8bf4\u53ea\u6709\u952e\u503c\uff0c\u6ca1\u6709\u952e\u540d\uff09\n - \u53ef\u4ee5\u904d\u5386\uff0c\u65b9\u6cd5\u6709\uff1aadd\u3001delete\u3001has\n- WeakSet\n - \u6210\u5458\u90fd\u662f\u5bf9\u8c61\n - \u6210\u5458\u90fd\u662f\u5f31\u5f15\u7528\uff0c\u53ef\u4ee5\u88ab\u5783\u573e\u56de\u6536\u673a\u5236\u56de\u6536\uff0c\u53ef\u4ee5\u7528\u6765\u4fdd\u5b58DOM\u8282\u70b9\uff0c\u4e0d\u5bb9\u6613\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\n - \u4e0d\u80fd\u904d\u5386\uff0c\u65b9\u6cd5\u6709add\u3001delete\u3001has\n- Map\n - \u672c\u8d28\u4e0a\u662f\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u7c7b\u4f3c\u96c6\u5408\n - \u53ef\u4ee5\u904d\u5386\uff0c\u65b9\u6cd5\u5f88\u591a\u53ef\u4ee5\u8ddf\u5404\u79cd\u6570\u636e\u683c\u5f0f\u8f6c\u6362\n- WeakMap\n - \u53ea\u63a5\u53d7\u5bf9\u8c61\u4f5c\u4e3a\u952e\u540d\uff08null\u9664\u5916\uff09\uff0c\u4e0d\u63a5\u53d7\u5176\u4ed6\u7c7b\u578b\u7684\u503c\u4f5c\u4e3a\u952e\u540d\n - \u952e\u540d\u662f\u5f31\u5f15\u7528\uff0c\u952e\u503c\u53ef\u4ee5\u662f\u4efb\u610f\u7684\uff0c\u952e\u540d\u6240\u6307\u5411\u7684\u5bf9\u8c61\u53ef\u4ee5\u88ab\u5783\u573e\u56de\u6536\uff0c\u6b64\u65f6\u952e\u540d\u662f\u65e0\u6548\u7684\n - \u4e0d\u80fd\u904d\u5386\uff0c\u65b9\u6cd5\u6709get\u3001set\u3001has\u3001delete\n", "answer": "", "biz_type": 1, "qid": 271, "subject": "", "title": "weak-Set\u3001weak-Map \u548c Set\u3001Map \u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u81ea\u5982"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 236, "qtype": "short_answer", "short_answer": {"analysis": "### \u00a0\u4e00\u3001DOM\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\n\n#### 1.DOM0\n\n`element.onClick = function(){}`\n\n#### 2.DOM2\n\n`element.addEventListener('click',function(){},false)` (IE\u4e0b\u9762\u8bf4)\n\n`element.addEventListener('click',function(){},false)`\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570 false \u8868\u793a\u5728\u5192\u6ce1\u9636\u6bb5\u8c03\u7528\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f \u9ed8\u8ba4\u4e3afalse\uff0ctrue\uff0c\u8868\u793a\u5728\u6355\u83b7\u9636\u6bb5\u8c03\u7528\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u3002\n\naddEventListener \u6dfb\u52a0\u7684\u4e8b\u4ef6\u53ea\u80fd\u901a\u8fc7 removeEventListener \u6765\u79fb\u9664\u4e8b\u4ef6\uff0c\u4f46\u662f\u79fb\u9664\u7684\u51fd\u6570\u4e0d\u80fd\u662f\u533f\u540d\u51fd\u6570 \u79fb\u9664\u7684\u51fd\u6570\u65b9\u6cd5\u5fc5\u987b\u662f\u4e00\u81f4\u7684\uff0c\u5982\u679c\u4e0d\u662f\u7279\u522b\u9700\u8981\uff0c\u4e0d\u5efa\u8bae\u5728\u4e8b\u4ef6\u6355\u83b7\u9636\u6bb5\u6ce8\u518c\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\n\n#### 3.DOM3 \n\n- \u6dfb\u52a0\u4e86\u66f4\u591a\u7684\u4e8b\u4ef6\u7c7b\u578b\n - UI \u4e8b\u4ef6\n - \u9f20\u6807\u4e8b\u4ef6\n - \u6eda\u52a8\u4e8b\u4ef6\n - \u952e\u76d8\u4e8b\u4ef6\n - \u7126\u70b9\u4e8b\u4ef6\n - \u6587\u672c\u4e8b\u4ef6\n - \u5408\u6210\u4e8b\u4ef6\n - \u53d8\u52a8\u4e8b\u4ef6\n - \u53d8\u52a8\u540d\u79f0\u4e8b\u4ef6\n- \u5f15\u5165\u4e86\u4ee5\u7edf\u4e00\u65b9\u5f0f\u52a0\u8f7d\u548c\u4fdd\u5b58\u6587\u6863\u7684\u65b9\u6cd5\uff1a\u5728DOM\u52a0\u8f7d\u548c\u4fdd\u5b58\u6a21\u5757\u4e2d\u5b9a\u4e49\n- \u65b0\u589e\u4e86\u9a8c\u8bc1\u6587\u6863\u7684\u65b9\u6cd5\uff0c\u5728DOM\u9a8c\u8bc1\u6a21\u5757\u4e2d\u5b9a\u4e49\n- \u652f\u6301XML1.0\u89c4\u8303\n\n\nDOM3 \u6dfb\u52a0\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\n\n```js\nelement.addEventListener('keyup',function(){},false)\n```\n\n### \u4e8c\u3001\u4e8b\u4ef6\u6d41\u4e0e\u4e8b\u4ef6\u6a21\u578b\n\nW3c \u89c4\u5b9a,\u6700\u5148\u901a\u77e5window\uff0c\u7136\u540e\u662fdocument\uff0c\u7531\u4e0a\u5230\u4e0b\u4f9d\u6b21\u8fdb\u5165\u76f4\u5230\u6700\u5e95\u5c42\u7684\u88ab\u51fa\u53d1\u7684\u5143\u7d20(\u4e5f\u5c31\u662f\u76ee\u6807\u5143\u7d20\uff0c\u901a\u5e38\u7684 event.target \u7684\u503c)\u4e3a\u6b62\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e4b\u4e3a\u6355\u83b7\uff0c\u4e4b\u540e\uff0c\u4e8b\u4ef6\u4f1a\u4ece\u76ee\u6807\u5143\u7d20\u5f00\u59cb\uff0c\u5192\u6ce1\uff0c\u7531\u4e0b\u81f3\u4e0a\u9010\u5c42\u4f20\u9012\u81f3windows\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e4b\u4e3a\u5192\u6ce1\n\n\u4e00\u4e2a\u5b8c\u6574\u7684\u4e8b\u4ef6\u6d41\u5206\u6210\u4e09\u4e2a\u90e8\u5206\n\n- \u6355\u83b7\u9636\u6bb5\n- \u76ee\u6807\u9636\u6bb5\n- \u5192\u6ce1\u9636\u6bb5\n\n![image.png](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-236-buhuo.png)\n\n\n### \u4e09\u3001\u63cf\u8ff0DOM\u4e8b\u4ef6\u6355\u83b7/\u5192\u6ce1\u7684\u5177\u4f53\u6d41\u7a0b\n\n\u4e8b\u4ef6\u6355\u83b7\u4f1a\u6309\u7167\u5982\u4e0b\u987a\u5e8f\u4f20\u64ad\uff1a\n\n- `window`\n- `document`\n- `<html>`\n- `<body>`\n- `div \u7236\u7ea7\u5143\u7d20->\u5b50\u7ea7\u5143\u7d20`\n- `\u76ee\u6807\u5143\u7d20`\n\n> \u6ce8\uff1a\u5192\u6ce1\u6d41\u7a0b\u4e0e\u4e4b\u76f8\u53cd\n\n\n```js\n <div id='box'>\n <a id='alink' href='https://www.baidu.com/'> \u53bb\u767e\u5ea6</a>\n </div>\n\n var btn = document.getElementById('box');\n var alink = document.getElementById('alink');\n window.addEventListener('click', function (event) {\n console.log('window capture');\n }, false);\n\n document.addEventListener('click', function (event) {\n console.log('document capture');\n }, false);\n\n document.documentElement.addEventListener('click', function (event) {\n console.log('html capture');\n }, false);\n\n document.body.addEventListener('click', function (event) {\n console.log('body capture');\n }, false);\n\n alink.addEventListener('click', function (event) {\n event.preventDefault();\n console.log('a clcik')\n }, false)\n\n btn.addEventListener('click', function (event) {\n console.log('box clicked')\n }, false)\n\n/*\n\u6267\u884c\u7ed3\u679c\u662f\nbox clicked\nbody capture\nhtml capture\ndocument capture\nwindow capture\n*/\n```\n\n\u5982\u679c\u5c06\u4e0a\u8ff0\u7684addEventListener\u7b2c\u4e09\u4e2a\u53c2\u6570\u53d8\u4e3afalse\uff0c\u5219\u51fd\u6570\u5c06\u5728\u6355\u83b7\u7684\u9636\u6bb5\u6267\u884c\n\n- window capture\n- document capture\n- html capture\n- body capture\n- box clicked\n\n\n### \u56db\u3001\u4e8b\u4ef6\u5bf9\u8c61\u5e38\u89c1\u7684\u5e94\u7528\n\n\u4e8b\u4ef6\u6355\u83b7\u4e4b\u540e\u6211\u4eec\u6240\u9700\u8981\u7684\u5c5e\u6027\u4ee5\u53ca\u7ecf\u5e38\u4f7f\u7528\u7684\u4e00\u4e9b\u7279\u6027\uff0c\u5982\u4e0b\n\n#### 1.event.preventDefault();\n\n\u4f5c\u7528 \u963b\u6b62\u7279\u5b9a\u4e8b\u4ef6\u7684\u9ed8\u8ba4\u884c\u4e3a\n\n\u5982 a\u6807\u7b7e\u7ed1\u5b9a\u7684click\u4e8b\u4ef6\uff0c\u5728\u54cd\u5e94\u51fd\u6570\u4e2d\u8bbe\u7f6e\u65b9\u6cd5\u4f1a\u963b\u6b62\u94fe\u63a5\u9ed8\u8ba4\u8df3\u8f6c\u7684\u884c\u4e3a \u5982\u4e0a\u8ff0\u4f8b\u5b50\u6240\u89c1\uff0c\u5982\u679c\u5728a\u6807\u7b7e\u4e0a\u589e\u52a0\u963b\u6b62\u8df3\u8f6c\u7684\u4e8b\u4ef6\uff0c\u5c06\u4e0d\u4f1a\u8df3\u8f6c\u81f3baidu.com\uff0c\u5426\u5219\uff0c\u5728\u4e8b\u4ef6\u5904\u7406\u7ed3\u675f\u4e4b\u540e\u5c06\u4f1a\u8df3\u8f6c\u81f3baidu.com\uff1b\n\n#### 2.event.stopPropagation()\n\n\u4f5c\u7528 \u963b\u6b62\u4e8b\u4ef6\u8fdb\u4e00\u6b65\u5192\u6ce1\n\n\u5982\u679c\u5c06\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u6539\u6210\u4ee5\u4e0b\u7684\u4ee3\u7801\n \n```js\n btn.addEventListener('click', function (event) {\n console.log('box clicked')\n event.stopPropagation();\n }, false)\n\n/* \n\u6267\u884c\u7684\u7ed3\u679c\u5c06\u662f \na clcik \nbox clicked \n*/\n```\n\n#### 3.event.stopImmediatePropagation()\n\n\u4f5c\u7528 \u963b\u6b62\u4e8b\u4ef6\u8fdb\u4e00\u6b65\u5192\u6ce1\uff0c\u540c\u65f6\u963b\u6b62\u4efb\u4f55\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u88ab\u8c03\u7528\u3002**\u4e8b\u4ef6\u54cd\u5e94\u4f18\u5148\u7ea7**\uff08DOM3\u7ea7\u4e8b\u4ef6\u4e2d\u65b0\u589e\uff09\n\n\u5982\uff0cdiv\u7ed1\u5b9a\u4e86\u4e24\u4e2aclick\u4e8b\u4ef6\uff0c\u901a\u8fc7\u4f18\u5148\u7ea7\u7684\u65b9\u6cd5\uff0c\u7b2c\u4e00\u4e2a\u54cd\u5e94\u7684\u51fd\u6570\u662fa,\u7b2c\u4e8c\u4e2a\u662fb, \u4e00\u6b21\u6ce8\u518ca,b \u4e24\u4e2aclick \u4e8b \u4ef6\uff0c\u70b9\u51fba \u4e0d\u8981\u54cd\u5e94b\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u6b64\u65b9\u6cd5 \n\n#### 4.event.currentTarget:\u5f53\u524d\u6b63\u5728\u5904\u7406\u4e8b\u4ef6\u7684\u5143\u7d20\n\nevent.target\uff1a**\u4e8b\u4ef6\u771f\u6b63\u7684\u76ee\u6807**\uff0c\u8868\u793a\u5f53\u524d\u88ab\u70b9\u51fb\u7684\u5143\u7d20\n\n### \u4e94\u3001\u8de8\u6d4f\u89c8\u5668\u5904\u7406\n\n\u9488\u5bf9\u4e0d\u540c\u7684\u6d4f\u89c8\u5668\uff0c\u5728\u8fd9\u91cc\u9700\u8981\u533a\u5206\u7684\u662f\u5426\u662fIE\u6d4f\u89c8\u5668\uff0c\u5982\u679c\u662fIE\u6d4f\u89c8\u5668\u9700\u8981\u505a\u4e00\u4e9b\u7279\u6b8a\u7684\u5904\u7406\n\n```js\nconst eventUtil = {\n // \u6dfb\u52a0\u4e00\u4e2a\u4e8b\u4ef6\n addHandle = function (element, type, handle) {\n if (element.addEventListener) {\n element.addEventListener(type, handle, false) /*\u975eIE*/\n } else if (element.attachEvent) {\n element.attachEvent('on' + type, handle); /*IE*/\n } else {\n element[`on${type}`] = handle\n }\n },\n\n // \u7ed9\u5143\u7d20\u5220\u9664\u4e00\u4e2a\u4e8b\u4ef6\n removeHandle = function (element, type, handle) {\n if (element.removeEventListener) {\n element.removeEventListener(type, handle, false) /*\u975eIE*/\n } else if (element.detachEvent) {\n element.attachEvent('on' + type, handle); /*IE*/\n } else {\n element[`on${type}`] = null;\n }\n },\n\n // \u83b7\u53d6\u517c\u5bb9\u6240\u6709\u6d4f\u89c8\u5668\u7684\u4e00\u4e2a\u5bf9\u8c61\n getEvent: function (event) {\n return event ? event : window.event;\n },\n\n //\u83b7\u53d6\u4e8b\u4ef6\u7c7b\u578b\n getType: function (event) { //\u6b64\u9879\u4e0d\u5b58\u5728\u6d4f\u89c8\u5668\u517c\u5bb9\u95ee\u9898\n return event.type;\n },\n\n // \u4e8b\u4ef6\u6765\u81ea\u54ea\u4e2a\u5143\u7d20\n getTarget: function (event) {\n return event.target || event.srcElement;\n },\n\n // \u963b\u6b62\u4e8b\u4ef6\u9ed8\u8ba4\u884c\u4e3a\n preventDefault = function (event) {\n if (event.preventDefault) {\n event.preventDefault(); /*\u975eIE*/\n } else {\n event.returnValue = false; /*IE*/\n }\n },\n\n // \u963b\u6b62\u4e8b\u4ef6\u5192\u6ce1\u884c\u4e3a\n stopPropagation=function (event) {\n if (event.stopPropagation) {\n event.stopPropagation(); /*\u975eIE*/\n } else {\n event.cancelBubble = true; /*IE*/\n }\n }\n }\n```\n\n```html\n <ul id='ul'>\n <li>sayhello</li>\n <li>sayGodbay</li>\n </ul>\n```\n\n```js\n var ulWrap = document.getElementById('ul');\n EventUtil.addHandle(ulWrap, 'click',function(){\n event = EventUtil.getEvent(event);\n console.log('\u5f53\u524d\u9009\u4e2d\u7684\u5143\u7d20\u662f', event.target.innerText)\n })\n\n```\n\n### \u516d\u3001\u4e8b\u4ef6\u59d4\u6258\n\n\u9488\u5bf9\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u505a\u7684\u4e00\u4e9b\u4f18\u5316\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u4f7f\u7528\u4e8b\u4ef6\u59d4\u6258\n\n```html\n<ul id='myLinks'>\n <li id='gosomeWhere'>go someWhere</li>\n <li id='dosomething'>do something</li>\n <li id='sayhi'>Say hi</li>\n </ul>\n\n```\n\n\u5305\u542b3\u4e2a\u88ab\u5355\u51fb\u540e\u4f1a\u6267\u884c\u64cd\u4f5c\u7684\u5217\u8868\u9879\uff0c\u6309\u7167\u4f20\u7edf\u7684\u505a\u6cd5\uff0c\u9700\u8981\u6309\u7167\u4ee5\u4e0b\u7684\u65b9\u5f0f\u8fdb\u884c\n\n```js\n // \u4f20\u7edf\u7684\u65b9\u6cd5\n var item1 = document.getElementById('gosomeWhere')\n var item2 = document.getElementById('dosomething')\n var item3 = document.getElementById('sayhi')\n\n EventUtil.addHandle(item1, 'click', function () {\n location.href = 'https://www.baidu.com/';\n });\n\n EventUtil.addHandle(item2, 'click', function () {\n document.title = 'i changeed the document title'\n });\n\n EventUtil.addHandle(item3, 'click', function () {\n console.log('hi')\n });\n\n// \u4f7f\u7528\u4e8b\u4ef6\u59d4\u6258\uff0c\u53ea\u9700\u5728DOM\u6811\u4e2d\u5c3d\u91cf\u5676\u7684\u5c42\u6b21\u4e0a\u6dfb\u52a0\u4e00\u4e2a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\n var list = document.getElementById('myLinks');\n EventUtil.addHandle(list, 'click', function (event) {\n debugger;\n event = EventUtil.getEvent(event);\n const target = EventUtil.getTarget(event);\n switch (target.id) {\n case 'gosomeWhere':\n location.href = 'https://www.baidu.com/';\n break;\n case 'dosomething':\n document.title = 'i changeed the document title';\n break;\n case 'sayhi':\n console.log('hi');\n break;\n }\n });\n\n\n```\n\n\u4f7f\u7528\u4e8b\u4ef6\u59d4\u6258\u53ea\u53d6\u5f97\u4e86\u4e00\u4e2aDOM\u5143\u7d20\uff0c\u6dfb\u52a0\u4e86\u4e00\u4e2a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u51cf\u5c11\u4e86\u5185\u5b58\u7684\u5360\u7528\uff0c\u7528\u5230\u9f20\u6807\u4e8b\u4ef6\u548c\u952e\u76d8\u4e8b\u4ef6\u90fd\u53ef\u4ee5\u4f7f\u7528\u4e8b\u4ef6\u59d4\u6258\uff0c\u5728\u9700\u8981\u7684\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u79fb\u9664\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\n", "answer": "", "biz_type": 1, "qid": 236, "subject": "", "title": "\u63cf\u8ff0 DOM \u4e8b\u4ef6\u6355\u83b7\u7684\u5177\u4f53\u6d41\u7a0b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 10, "qid": 188, "qtype": "short_answer", "short_answer": {"analysis": "### Promise \u94fe\u5f0f\u8c03\u7528\n\n#### 1.\u94fe\u5f0f\u8c03\u7528\u6d41\u7a0b\uff1a\n\n- `promise1 = new Promise(excutor = (resolve, reject) => { ... })` \u4e2d\u7684 excutor \u662f\u7acb\u5373\u6267\u884c\u7684\uff0c\u4f46\u6700\u540e\u6267\u884c resolve \u53ef\u80fd\u662f\u5728\u5f02\u6b65\u64cd\u4f5c\u4e2d\n- `promise1.then` \u4f1a\u7ed9 promise1 \u6dfb\u52a0\u56de\u8c03\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 promise2\uff0c\u8fd9\u4e2a\u65b0\u7684 promise2 \u7684\u51b3\u8bae\u4f9d\u9760\u4e4b\u524d\u56de\u8c03\u4e2d\u7684 resolvePromise \u65b9\u6cd5\n- promise1 \u51b3\u8bae\u540e\u4f1a\u6267\u884c\u56de\u8c03\uff0c\u9996\u5148\u6267\u884c then \u4e2d\u4f20\u5165\u7684 `onFulfilled(promise1.value)`\uff0c\u8d4b\u503c\u7ed9\u53d8\u91cf x\uff0c\u518d\u6267\u884c `resolvePromise(promise2, x, promise2Resolve, promise2Reject)`\n- \u5982\u679c x \u662f\u4e2a\u5df2\u51b3\u8bae\u7684 Promise \u6216\u8005\u666e\u901a\u7684\u6570\u636e\u7c7b\u578b\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5 `promise2Resolve(x)` \u51b3\u8bae promise2\n- \u5982\u679c x \u662f\u4e2a pending \u72b6\u6001\u7684 promise \u6216\u8005 thenable \u5bf9\u8c61\uff0c\u90a3\u4e48\u6267\u884c `x.then` \uff0c\u5c06 resolvePromise \u653e\u5165 x \u7684\u6210\u529f\u56de\u8c03\u961f\u5217\uff0c\u7b49\u5f85 x \u51b3\u8bae\u540e\u5c06 `x.value` \u6210\u529f\u8d4b\u503c\uff0c\u7136\u540e\u6267\u884c `resolvePromise(promise2, x.value, promise2Resolve, promise2Reject)`\n- \u5728\u6b64\u671f\u95f4\u5982\u679c\u6267\u884c\u4e86 `promise2.then` \u5c31\u65b0\u5efa\u4e00\u4e2a promise3 \u5e76\u8fd4\u56de \uff0c\u5c06\u65b0\u4f20\u5165\u7684 `onFulfilled(promise2.value)` \u548c\u9488\u5bf9 promise3 \u7684 resolvePromise \u4f20\u5165 promise2 \u7684\u6210\u529f\u56de\u8c03\u961f\u5217\u4e2d\uff0c\u7b49\u5f85 promise2 \u7684\u51b3\u8bae\n- promise3.then \u540c\u4e0a\uff0c\u5c31\u6b64\u5b9e\u73b0\u4e86\u94fe\u5f0f\u8c03\u7528\n\n#### 2.\u94fe\u5f0f\u8c03\u7528\u987a\u5e8f\uff1a\n\n- `promise1 => promise2 => promise3`\uff0c\u56e0\u4e3a promise2 \u7684\u8981\u5728 promise1 \u7684\u6210\u529f\u56de\u8c03\u91cc\u6267\u884c\n\n#### 3.\u94fe\u5f0f\u8c03\u7528\u900f\u4f20\n\n\u5982\u679cpromise1.then \u4f20\u5165\u7684onFulfilled \u4e0d\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u6b64\u65f6onfulfilled \u4f1a\u88ab\u6539\u5199\u6210 val => val\n\n#### 4.promise\u4ee3\u7801\u5b9e\u73b0\uff1a\n\n```js\n // Promise \u4e09\u79cd\u72b6\u6001\n const PENDING = \"Pending\";\n const FULFILLED = \"Fulfilled\";\n const REJECTED = \"Rejected\";\n // promise \u5904\u7406\u8fc7\u7a0b\n function promiseResolutionProcedure(promise2, x, resolve, reject) {\n // \u5224\u65ad\u5faa\u73af\u5f15\u7528\n if (promise2 === x) {\n throw new Error(\"\u5faa\u73af\u5f15\u7528 promise\");\n }\n // \u5904\u7406 promise \u5bf9\u8c61\n if (x instanceof MyPromise) {\n if (x.state === PENDING) {\n x.then((y) => {\n promiseResolutionProcedure(promise2, y, resolve, reject);\n }, reject);\n } else {\n x.state === FULFILLED && resolve(x.value);\n x.state === REJECTED && reject(x.value);\n }\n }\n // \u5224\u65ad thenable \u5bf9\u8c61\n if ((typeof x === \"object\" || typeof x === \"function\") && x !== null) {\n if (typeof x.then === \"function\") {\n x.then((y) => {\n promiseResolutionProcedure(promise2, y, resolve, reject);\n }, reject);\n } else {\n resolve(x);\n }\n } else {\n resolve(x);\n }\n }\n class MyPromise {\n static all(promiseArray) {\n return new MyPromise((resolve, reject) => {\n let resultArray = [];\n let successTimes = 0;\n\n function processResult(index, data) {\n resultArray[index] = data;\n successTimes++;\n if (successTimes === promiseArray.length) {\n // \u5904\u7406\u6210\u529f\n resolve(resultArray);\n }\n }\n\n for (let i = 0; i < promiseArray.length; i++) {\n promiseArray[i].then(\n (data) => {\n processResult(i, data);\n },\n (err) => {\n // \u5904\u7406\u5931\u8d25\n reject(err);\n }\n );\n }\n });\n }\n constructor(fn) {\n this.state = PENDING;\n this.value = undefined;\n this.resolveCallbacks = [];\n this.rejectCallbacks = [];\n const resolve = (val) => {\n if (\n (typeof val === \"object\" || typeof val === \"function\") &&\n val.then\n ) {\n promiseResolutionProcedure(this, val, resolve, reject);\n return;\n }\n setTimeout(() => {\n if (this.state === PENDING) {\n this.state = FULFILLED;\n this.value = val;\n // \u6267\u884c\u6240\u6709\u7684 then \u65b9\u6cd5\n this.resolveCallbacks.map((fn) => fn());\n }\n }, 0);\n };\n const reject = (val) => {\n if (\n (typeof val === \"object\" || typeof val === \"function\") &&\n val.then\n ) {\n promiseResolutionProcedure(this, val, resolve, reject);\n return;\n }\n setTimeout(() => {\n if (this.state === PENDING) {\n this.value = val;\n this.state = REJECTED;\n // \u6267\u884c\u6240\u6709\u7684 then \u65b9\u6cd5\n this.rejectCallbacks.map((fn) => fn());\n }\n }, 0);\n };\n fn(resolve, reject);\n }\n then(\n onFulfilled = (val) => val,\n onRejected = (err) => {\n throw new Error(err);\n }\n ) {\n let promise2 = null;\n // \u5904\u7406\u5df2\u7ecf\u5b8c\u6210\u7684 promise\n if (this.state === FULFILLED) {\n promise2 = new MyPromise((resolve, reject) => {\n const x = onFulfilled(this.value);\n promiseResolutionProcedure(promise2, x, resolve, reject);\n });\n }\n\n // \u5904\u7406\u5df2\u7ecf\u5b8c\u6210\u7684 promise\n if (this.state === REJECTED) {\n promise2 = new MyPromise((resolve, reject) => {\n const x = onRejected(this.value);\n promiseResolutionProcedure(promise2, x, resolve, reject);\n });\n }\n\n // \u5904\u7406\u5c1a\u672a\u5b8c\u6210\u7684 promise\n if (this.state === PENDING) {\n promise2 = new MyPromise((resolve, reject) => {\n this.resolveCallbacks.push(() => {\n const x = onFulfilled(this.value);\n promiseResolutionProcedure(promise2, x, resolve, reject);\n });\n\n this.rejectCallbacks.push(() => {\n const x = onRejected(this.value);\n promiseResolutionProcedure(promise2, x, resolve, reject);\n });\n });\n }\n return promise2;\n }\n }\n```", "answer": "", "biz_type": 1, "qid": 188, "subject": "", "title": "Promise \u94fe\u5f0f\u8c03\u7528\u5982\u4f55\u5b9e\u73b0"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 301, "qtype": "short_answer", "short_answer": {"analysis": "### Generator\n\n#### 1.\u6982\u5ff5\n\n**Generator\uff1a**\u6307\u751f\u6210\u5668\u51fd\u6570\uff0c\u58f0\u660e\u65b9\u5f0f\u4e3a `function*`\u00a0\uff0c\u8fd4\u56de\u4e00\u4e2aGenerator\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u90e8\u7f72\u4e86Iterator\u63a5\u53e3\uff0c\u53ef\u904d\u5386\u3002\n\n- Generator\u662f\u4e00\u79cd \u5f02\u6b65\u89e3\u51b3\u65b9\u6848\uff0cGenerator\u53ef\u4ee5\u7406\u89e3\u6210\u4e00\u4e2a\u72b6\u6001\u673a\uff0c\u5c01\u88c5\u4e86\u591a\u4e2a\u5185\u90e8\u72b6\u6001\u3002\n- Generator\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u904d\u5386\u5668\u5bf9\u8c61\uff0c\u9664\u4e86\u662f\u4e2a\u72b6\u6001\u673a\uff0c\u8fd8\u662f\u4e2a\u904d\u5386\u5668\u5bf9\u8c61\u751f\u6210\u51fd\u6570\u3002\u8fd4\u56de\u7684\u904d\u5386\u5668\u5bf9\u8c61\uff0c\u80fd\u4f9d\u6b21\u904d\u5386\u51fd\u6570\u5185\u90e8\u6bcf\u4e2a\u72b6\u6001\u3002\n- Generator\u51fd\u6570\u6709\u4e24\u4e2a\u7279\u5f81\uff1a\n - `function`\u00a0\u5173\u952e\u5b57\u548c\u51fd\u6570\u540d\u4e4b\u95f4\u6709\u4e00\u4e2a\u661f\u53f7\n - \u51fd\u6570\u5185\u90e8\u4f7f\u7528 `yield`\u00a0\u8868\u8fbe\u5f0f\uff0c\u5b9a\u4e49\u4e86\u4e0d\u540c\u7684\u5185\u90e8\u72b6\u6001\n\n\u57fa\u672c\u8bed\u6cd5\n\n```js\nfunction* helloWorldGenerator(){\n\tyield 'hell0';\n yield 'world';\n return 'ending';\n}\nvar hw = helloWorldGenerator();\nhw.next()\n// { value: 'hello', done: false }\nhw.next()\n// { value: 'world', done: false }\nhw.next()\n// { value: 'ending', done: true }\nhw.next()\n// { value: undefined, done: true }\n```\n\n\u8c03\u7528Generator\u51fd\u6570\uff0c\u51fd\u6570\u5e76\u4e0d\u6267\u884c\uff0c\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u904d\u5386\u5668\u5bf9\u8c61\uff0c\u4e5f\u5c31\u662f\u4e00\u4e2a\u6307\u5411\u5185\u90e8\u72b6\u6001\u7684 \u6307\u9488\u5bf9\u8c61 \u3002\n\n\u5fc5\u987b\u8c03\u7528\u904d\u5386\u5668\u5bf9\u8c61\u7684 next \u65b9\u6cd5\uff0c\u4f7f\u5f97\u6307\u9488\u5411\u4e0b\u4e00\u4e2a\u72b6\u6001\u3002\n\n\u6bcf\u6b21\u8c03\u7528\u904d\u5386\u5668\u5bf9\u8c61\u7684 next \u65b9\u6cd5\uff0c\u5c31\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6709\u7740 value \u548c done \u4e24\u4e2a\u5c5e\u6027\u7684\u5bf9\u8c61\u3002\n\n- **value**\uff1a\u8868\u793a\u5f53\u524d\u5185\u90e8\u72b6\u6001\u7684\u503c\uff0c\u662f yield \u540e\u9762\u90a3\u4e2a\u8868\u8fbe\u5f0f\u7684\u503c\n- **done**\uff1a\u662f\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u8868\u793a\u662f\u5426\u904d\u5386\u7ed3\u675f\u3002\n\n#### 2.yield\n\n- `yield`\u00a0\u8868\u8fbe\u5f0f\u7528\u4e8e\u59d4\u6258\u7ed9\u53e6\u4e00\u4e2a generator \u6216 \u53ef\u8fed\u4ee3\u7684\u5bf9\u8c61\u3002\n- `yield`\u00a0\u8868\u8fbe\u5f0f\u8fed\u4ee3\u7d27\u8ddf\u5176\u540e\u7684\u5bf9\u8c61\u6216generator\uff0c\u8fd4\u56de\u6bcf\u6b21\u64cd\u4f5c\u7684\u503c\u3002\n- `yield`\u00a0\u8868\u8fbe\u5f0f\u672c\u8eab\u7684\u503c\u662f\u5f53\u8fed\u4ee3\u5668\u5173\u95ed\u65f6\u8fd4\u56de\u7684\u503c\uff08\u5373done\u4e3atrue\u65f6\uff09\n\n**\u4f5c\u7528**\uff1a\n\n\u7528\u6765\u6682\u505c\u548c\u6062\u590d\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\n\n**\u8bed\u6cd5\uff1a**\n\n```javascript\n[rv] = yield [expression];\n```\n\n- `expression`\u5b9a\u4e49\u901a\u8fc7\u8fed\u4ee3\u5668\u534f\u8bae\u4ece\u751f\u6210\u5668\u51fd\u6570\u8fd4\u56de\u7684\u503c\u3002\u5982\u679c\u7701\u7565\uff0c\u5219\u8fd4\u56de`undefined`\u3002\n- `rv`\u8fd4\u56de\u4f20\u9012\u7ed9\u751f\u6210\u5668\u7684`next()`\u65b9\u6cd5\u7684\u53ef\u9009\u503c\uff0c\u4ee5\u6062\u590d\u5176\u6267\u884c\u3002\n\n**\u793a\u4f8b**\n\n```js\nfunction* g4() {\n yield* [1, 2, 3];\n return \"foo\";\n}\n\nvar result;\n\nfunction* g5() {\n result = yield* g4();\n}\n\nvar iterator = g5();\n\nconsole.log(iterator.next()); // { value: 1, done: false }\nconsole.log(iterator.next()); // { value: 2, done: false }\nconsole.log(iterator.next()); // { value: 3, done: false }\nconsole.log(iterator.next()); // { value: undefined, done: true }, \n// \u6b64\u65f6 g4() \u8fd4\u56de\u4e86 { value: \"foo\", done: true }\nconsole.log(result); // \"foo\"\n```\n\n**\u63cf\u8ff0\uff1a**\n\n- `yield`\u5173\u952e\u5b57\u4f7f\u751f\u6210\u5668\u51fd\u6570\u6267\u884c\u6682\u505c\uff0c`yield`\u5173\u952e\u5b57\u540e\u9762\u7684\u8868\u8fbe\u5f0f\u7684\u503c\u8fd4\u56de\u7ed9\u751f\u6210\u5668\u7684\u8c03\u7528\u8005\u3002\u5b83\u53ef\u4ee5\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u57fa\u4e8e\u751f\u6210\u5668\u7684\u7248\u672c\u7684`return`\u5173\u952e\u5b57\u3002\n- `yield`\u5173\u952e\u5b57\u5b9e\u9645\u8fd4\u56de\u4e00\u4e2a`IteratorResult`\u5bf9\u8c61\uff0c\u5b83\u6709\u4e24\u4e2a\u5c5e\u6027\uff0c`value`\u548c`done`\u3002`value`\u5c5e\u6027\u662f\u5bf9`yield`\u8868\u8fbe\u5f0f\u6c42\u503c\u7684\u7ed3\u679c\uff0c\u800c`done`\u662f`false`\uff0c\u8868\u793a\u751f\u6210\u5668\u51fd\u6570\u5c1a\u672a\u5b8c\u5168\u5b8c\u6210\u3002\n- \u4e00\u65e6\u9047\u5230 `yield` \u8868\u8fbe\u5f0f\uff0c\u751f\u6210\u5668\u7684\u4ee3\u7801\u5c06\u88ab\u6682\u505c\u8fd0\u884c\uff0c\u76f4\u5230\u751f\u6210\u5668\u7684 `next()` \u65b9\u6cd5\u88ab\u8c03\u7528\u3002\u6bcf\u6b21\u8c03\u7528\u751f\u6210\u5668\u7684`next()`\u65b9\u6cd5\u65f6\uff0c\u751f\u6210\u5668\u90fd\u4f1a\u6062\u590d\u6267\u884c\uff0c\u76f4\u5230\u8fbe\u5230\u4ee5\u4e0b\u67d0\u4e2a\u503c\uff1a\n - `yield`\uff0c\u5bfc\u81f4\u751f\u6210\u5668\u518d\u6b21\u6682\u505c\u5e76\u8fd4\u56de\u751f\u6210\u5668\u7684\u65b0\u503c\u3002 \u4e0b\u4e00\u6b21\u8c03\u7528`next()`\u65f6\uff0c\u5728`yield`\u4e4b\u540e\u7d27\u63a5\u7740\u7684\u8bed\u53e5\u7ee7\u7eed\u6267\u884c\u3002\n - `throw`\u7528\u4e8e\u4ece\u751f\u6210\u5668\u4e2d\u629b\u51fa\u5f02\u5e38\u3002\u8fd9\u8ba9\u751f\u6210\u5668\u5b8c\u5168\u505c\u6b62\u6267\u884c\uff0c\u5e76\u5728\u8c03\u7528\u8005\u4e2d\u7ee7\u7eed\u6267\u884c\uff0c\u6b63\u5982\u901a\u5e38\u60c5\u51b5\u4e0b\u629b\u51fa\u5f02\u5e38\u4e00\u6837\u3002\n - \u5230\u8fbe\u751f\u6210\u5668\u51fd\u6570\u7684\u7ed3\u5c3e\uff1b\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u751f\u6210\u5668\u7684\u6267\u884c\u7ed3\u675f\uff0c\u5e76\u4e14`IteratorResult`\u7ed9\u8c03\u7528\u8005\u8fd4\u56de`undefined`\u5e76\u4e14`done`\u4e3a`true`\u3002\n - \u5230\u8fbe`return`\u8bed\u53e5\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u751f\u6210\u5668\u7684\u6267\u884c\u7ed3\u675f\uff0c\u5e76\u5c06`IteratorResult`\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\uff0c\u5176\u503c\u662f\u7531`return`\u8bed\u53e5\u6307\u5b9a\u7684\uff0c\u5e76\u4e14`done` \u4e3a`true`\u3002\n- \u5982\u679c\u5c06\u53c2\u6570\u4f20\u9012\u7ed9\u751f\u6210\u5668\u7684`next()`\u65b9\u6cd5\uff0c\u5219\u8be5\u503c\u5c06\u6210\u4e3a\u751f\u6210\u5668\u5f53\u524d`yield`\u64cd\u4f5c\u8fd4\u56de\u7684\u503c\u3002\n\n\u5728\u751f\u6210\u5668\u7684\u4ee3\u7801\u8def\u5f84\u4e2d\u7684`yield`\u8fd0\u7b97\u7b26\uff0c\u4ee5\u53ca\u901a\u8fc7\u5c06\u5176\u4f20\u9012\u7ed9`Generator.prototype.next()`\u6307\u5b9a\u65b0\u7684\u8d77\u59cb\u503c\u7684\u80fd\u529b\u4e4b\u95f4\uff0c\u751f\u6210\u5668\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u63a7\u5236\u529b\u3002\n\n#### 3.\u603b\u7ed3\n\n- yield \u662f Generator \u51fd\u6570\u5185\u90e8\u5173\u952e\u5b57\uff0c\u7528\u4e8e\u6682\u505c\u548c\u6062\u590d\u51fd\u6570\u6267\u884c\u3002\n- yield \u548c Generator \u51fd\u6570\u8fd4\u56de\u7684\u904d\u5386\u5668\u5bf9\u8c61\u7684 next \u65b9\u6cd5\u914d\u5408\uff0c\u80fd\u5b9e\u73b0\u5185\u90e8\u72b6\u6001\u904d\u5386\u8fc7\u7a0b\u3002\n- \u8c03\u7528 GeneratorObj.next() \u8fd4\u56de yield \u540e\u9762\u7684\u503c\u4ee5\u53ca\u662f\u5426\u6267\u884c\u7ed3\u675f\u7684\u6807\u5fd7\u3002\u5c06\u51fd\u6570\u6267\u884c\u6743\u4ea4\u8fd8\u7ed9\u5916\u90e8\u8c03\u7528\u3002\n- \u540c\u65f6 GeneratorObj.next()\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4f20\u53c2\u5c06\u5916\u90e8\u53d8\u91cf\uff0c\u4f20\u7ed9\u51fd\u6570\u5185\u90e8\u3002\n\n", "answer": "", "biz_type": 1, "qid": 301, "subject": "", "title": "gennerator yield \u7684\u4f5c\u7528"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 305, "qtype": "short_answer", "short_answer": {"analysis": "### CORS\n\nCORS\u5373Cross Origin Resource Sharing\uff08\u8de8\u6765\u6e90\u8d44\u6e90\u5171\u4eab\uff09\uff0c\u901a\u4fd7\u8bf4\u5c31\u662f\u6211\u4eec\u6240\u719f\u77e5\u7684\u8de8\u57df\u8bf7\u6c42\u3002\u4f17\u6240\u5468\u77e5\uff0c\u5728\u4ee5\u524d\uff0c\u8de8\u57df\u53ef\u4ee5\u91c7\u7528\u4ee3\u7406\u3001JSONP\u7b49\u65b9\u5f0f\uff0c\u800c\u5728Modern\u6d4f\u89c8\u5668\u9762\u524d\uff0c\u8fd9\u4e9b\u7ec8\u5c06\u6210\u4e3a\u8fc7\u53bb\u5f0f\uff0c\u56e0\u4e3a\u6709\u4e86CORS\u3002\n\nCORS\u5728\u6700\u521d\u63a5\u89e6\u7684\u65f6\u5019\u53ea\u5927\u6982\u4e86\u89e3\u5230\uff0c\u901a\u8fc7\u670d\u52a1\u5668\u7aef\u8bbe\u7f6e`Access-Control-Allow-Origin`\u54cd\u5e94\u5934\uff0c\u5373\u53ef\u4f7f\u6307\u5b9a\u6765\u6e90\u50cf\u8bbf\u95ee\u540c\u6e90\u63a5\u53e3\u4e00\u6837\u8bbf\u95ee\u8de8\u57df\u63a5\u53e3\uff0c\u6700\u8fd1\u5728\u4f7f\u7528CORS\u7684\u65f6\u5019\uff0c\u7531\u4e8e\u9700\u8981\u4f20\u8f93\u81ea\u5b9a\u4e49Header\u4fe1\u606f\uff0c\u53d1\u73b0\u539f\u6765CORS\u7684\u89c4\u8303\u5b9a\u4e49\u8fdc\u4e0d\u6b62\u8fd9\u4e9b\u3002\n\nCORS\u53ef\u4ee5\u5206\u6210\u4e24\u79cd\uff1a\n\n1\u3001**\u7b80\u5355\u8bf7\u6c42**\n2\u3001**\u590d\u6742\u8bf7\u6c42**\n\n#### 1.\u7b80\u5355\u8bf7\u6c42\uff1a\n\n**HTTP\u65b9\u6cd5\u662f\u4e0b\u5217\u4e4b\u4e00**\n\n- HEAD\n- GET\n- POST\n\n**HTTP\u5934\u4fe1\u606f\u4e0d\u8d85\u51fa\u4ee5\u4e0b\u51e0\u79cd\u5b57\u6bb5**\n\n- Accept\n- Accept-Language\n- Content-Language\n- Last-Event-ID\n- Content-Type\uff0c\u4f46\u4ec5\u80fd\u662f\u4e0b\u5217\u4e4b\u4e00\n- application/x-www-form-urlencoded\n- multipart/form-data\n- text/plain\n\n\u4efb\u4f55\u4e00\u4e2a\u4e0d\u6ee1\u8db3\u4e0a\u8ff0\u8981\u6c42\u7684\u8bf7\u6c42\uff0c\u5373\u88ab\u8ba4\u4e3a\u662f\u590d\u6742\u8bf7\u6c42\u3002\u4e00\u4e2a\u590d\u6742\u8bf7\u6c42\u4e0d\u4ec5\u6709\u5305\u542b\u901a\u4fe1\u5185\u5bb9\u7684\u8bf7\u6c42\uff0c\u540c\u65f6\u4e5f\u5305\u542b\u9884\u8bf7\u6c42\uff08preflight request\uff09\u3002\n\n\u7b80\u5355\u8bf7\u6c42\u7684\u53d1\u9001\u4ece\u4ee3\u7801\u4e0a\u6765\u770b\u548c\u666e\u901a\u7684XHR\u6ca1\u592a\u5927\u533a\u522b\uff0c\u4f46\u662fHTTP\u5934\u5f53\u4e2d\u8981\u6c42\u603b\u662f\u5305\u542b\u4e00\u4e2a\u57df\uff08Origin\uff09\u7684\u4fe1\u606f\u3002\u8be5\u57df\u5305\u542b\u534f\u8bae\u540d\u3001\u5730\u5740\u4ee5\u53ca\u4e00\u4e2a\u53ef\u9009\u7684\u7aef\u53e3\u3002\u4e0d\u8fc7\u8fd9\u4e00\u9879\u5b9e\u9645\u4e0a\u7531\u6d4f\u89c8\u5668\u4ee3\u4e3a\u53d1\u9001\uff0c\u5e76\u4e0d\u662f\u5f00\u53d1\u8005\u4ee3\u7801\u53ef\u4ee5\u89e6\u53ca\u5230\u7684\u3002\n\n**\u7b80\u5355\u8bf7\u6c42\u7684\u90e8\u5206\u54cd\u5e94\u5934\u53ca\u89e3\u91ca\u5982\u4e0b\uff1a**\n\n- Access-Control-Allow-Origin\uff08\u5fc5\u542b\uff09- \u4e0d\u53ef\u7701\u7565\uff0c\u5426\u5219\u8bf7\u6c42\u6309\u5931\u8d25\u5904\u7406\u3002\u8be5\u9879\u63a7\u5236\u6570\u636e\u7684\u53ef\u89c1\u8303\u56f4\uff0c\u5982\u679c\u5e0c\u671b\u6570\u636e\u5bf9\u4efb\u4f55\u4eba\u90fd\u53ef\u89c1\uff0c\u53ef\u4ee5\u586b\u5199\"*\"\u3002\n- Access-Control-Allow-Credentials\uff08\u53ef\u9009\uff09 \u2013 \u8be5\u9879\u6807\u5fd7\u7740\u8bf7\u6c42\u5f53\u4e2d\u662f\u5426\u5305\u542bcookies\u4fe1\u606f\uff0c\u53ea\u6709\u4e00\u4e2a\u53ef\u9009\u503c\uff1atrue\uff08\u5fc5\u4e3a\u5c0f\u5199\uff09\u3002\u5982\u679c\u4e0d\u5305\u542bcookies\uff0c\u8bf7\u7565\u53bb\u8be5\u9879\uff0c\u800c\u4e0d\u662f\u586b\u5199false\u3002\u8fd9\u4e00\u9879\u4e0eXmlHttpRequest2\u5bf9\u8c61\u5f53\u4e2d\u7684withCredentials\u5c5e\u6027\u5e94\u4fdd\u6301\u4e00\u81f4\uff0c\u5373withCredentials\u4e3atrue\u65f6\u8be5\u9879\u4e5f\u4e3atrue\uff1bwithCredentials\u4e3afalse\u65f6\uff0c\u7701\u7565\u8be5\u9879\u4e0d\u5199\u3002\u53cd\u4e4b\u5219\u5bfc\u81f4\u8bf7\u6c42\u5931\u8d25\u3002\n- Access-Control-Expose-Headers\uff08\u53ef\u9009\uff09 \u2013 \u8be5\u9879\u786e\u5b9aXmlHttpRequest2\u5bf9\u8c61\u5f53\u4e2dgetResponseHeader()\u65b9\u6cd5\u6240\u80fd\u83b7\u5f97\u7684\u989d\u5916\u4fe1\u606f\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0cgetResponseHeader()\u65b9\u6cd5\u53ea\u80fd\u83b7\u5f97\u5982\u4e0b\u7684\u4fe1\u606f\uff1a\n- Cache-Control\n- Content-Language\n- Content-Type\n- Expires\n- Last-Modified\n- Pragma\n- \u5f53\u4f60\u9700\u8981\u8bbf\u95ee\u989d\u5916\u7684\u4fe1\u606f\u65f6\uff0c\u5c31\u9700\u8981\u5728\u8fd9\u4e00\u9879\u5f53\u4e2d\u586b\u5199\u5e76\u4ee5\u9017\u53f7\u8fdb\u884c\u5206\u9694\n\n\u5982\u679c\u4ec5\u4ec5\u662f\u7b80\u5355\u8bf7\u6c42\uff0c\u90a3\u4e48\u5373\u4fbf\u4e0d\u7528CORS\u4e5f\u6ca1\u6709\u4ec0\u4e48\u5927\u4e0d\u4e86\uff0c\u4f46CORS\u7684\u590d\u6742\u8bf7\u6c42\u5c31\u4ee4CORS\u663e\u5f97\u66f4\u52a0\u6709\u7528\u4e86\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u4efb\u4f55\u4e0d\u6ee1\u8db3\u4e0a\u8ff0\u7b80\u5355\u8bf7\u6c42\u8981\u6c42\u7684\u8bf7\u6c42\uff0c\u90fd\u5c5e\u4e8e\u590d\u6742\u8bf7\u6c42\u3002\u6bd4\u5982\u8bf4\u4f60\u9700\u8981\u53d1\u9001PUT\u3001DELETE\u7b49HTTP\u52a8\u4f5c\uff0c\u6216\u8005\u53d1\u9001Content-Type: application/json\u7684\u5185\u5bb9\u3002\n\n#### 2.\u590d\u6742\u8bf7\u6c42\n\n\u590d\u6742\u8bf7\u6c42\u8868\u9762\u4e0a\u770b\u8d77\u6765\u548c\u7b80\u5355\u8bf7\u6c42\u4f7f\u7528\u4e0a\u5dee\u4e0d\u591a\uff0c\u4f46\u5b9e\u9645\u4e0a\u6d4f\u89c8\u5668\u53d1\u9001\u4e86\u4e0d\u6b62\u4e00\u4e2a\u8bf7\u6c42\u3002\u5176\u4e2d\u6700\u5148\u53d1\u9001\u7684\u662f\u4e00\u79cd\"\u9884\u8bf7\u6c42\"\uff0c\u6b64\u65f6\u4f5c\u4e3a\u670d\u52a1\u7aef\uff0c\u4e5f\u9700\u8981\u8fd4\u56de\"\u9884\u56de\u5e94\"\u4f5c\u4e3a\u54cd\u5e94\u3002\u9884\u8bf7\u6c42\u5b9e\u9645\u4e0a\u662f\u5bf9\u670d\u52a1\u7aef\u7684\u4e00\u79cd\u6743\u9650\u8bf7\u6c42\uff0c\u53ea\u6709\u5f53\u9884\u8bf7\u6c42\u6210\u529f\u8fd4\u56de\uff0c\u5b9e\u9645\u8bf7\u6c42\u624d\u5f00\u59cb\u6267\u884c\u3002\n\n\u9884\u8bf7\u6c42\u4ee5OPTIONS\u5f62\u5f0f\u53d1\u9001\uff0c\u5f53\u4e2d\u540c\u6837\u5305\u542b\u57df\uff0c\u5e76\u4e14\u8fd8\u5305\u542b\u4e86\u4e24\u9879CORS\u7279\u6709\u7684\u5185\u5bb9\uff1a\n\n- Access-Control-Request-Method \u2013 \u8be5\u9879\u5185\u5bb9\u662f\u5b9e\u9645\u8bf7\u6c42\u7684\u79cd\u7c7b\uff0c\u53ef\u4ee5\u662fGET\u3001POST\u4e4b\u7c7b\u7684\u7b80\u5355\u8bf7\u6c42\uff0c\u4e5f\u53ef\u4ee5\u662fPUT\u3001DELETE\u7b49\u7b49\u3002\n- Access-Control-Request-Headers \u2013 \u8be5\u9879\u662f\u4e00\u4e2a\u4ee5\u9017\u53f7\u5206\u9694\u7684\u5217\u8868\uff0c\u5f53\u4e2d\u662f\u590d\u6742\u8bf7\u6c42\u6240\u4f7f\u7528\u7684\u5934\u90e8\u3002\n\n\u663e\u800c\u6613\u89c1\uff0c\u8fd9\u4e2a\u9884\u8bf7\u6c42\u5b9e\u9645\u4e0a\u5c31\u662f\u5728\u4e3a\u4e4b\u540e\u7684\u5b9e\u9645\u8bf7\u6c42\u53d1\u9001\u4e00\u4e2a\u6743\u9650\u8bf7\u6c42\uff0c\u5728\u9884\u56de\u5e94\u8fd4\u56de\u7684\u5185\u5bb9\u5f53\u4e2d\uff0c\u670d\u52a1\u7aef\u5e94\u5f53\u5bf9\u8fd9\u4e24\u9879\u8fdb\u884c\u56de\u590d\uff0c\u4ee5\u8ba9\u6d4f\u89c8\u5668\u786e\u5b9a\u8bf7\u6c42\u662f\u5426\u80fd\u591f\u6210\u529f\u5b8c\u6210\u3002\n\n**\u590d\u6742\u8bf7\u6c42\u7684\u90e8\u5206\u54cd\u5e94\u5934\u53ca\u89e3\u91ca\u5982\u4e0b\uff1a**\n\n- Access-Control-Allow-Origin\uff08\u5fc5\u542b\uff09 \u2013 \u548c\u7b80\u5355\u8bf7\u6c42\u4e00\u6837\u7684\uff0c\u5fc5\u987b\u5305\u542b\u4e00\u4e2a\u57df\u3002\n- Access-Control-Allow-Methods\uff08\u5fc5\u542b\uff09 \u2013 \u8fd9\u662f\u5bf9\u9884\u8bf7\u6c42\u5f53\u4e2dAccess-Control-Request-Method\u7684\u56de\u590d\uff0c\u8fd9\u4e00\u56de\u590d\u5c06\u662f\u4e00\u4e2a\u4ee5\u9017\u53f7\u5206\u9694\u7684\u5217\u8868\u3002\u5c3d\u7ba1\u5ba2\u6237\u7aef\u6216\u8bb8\u53ea\u8bf7\u6c42\u67d0\u4e00\u65b9\u6cd5\uff0c\u4f46\u670d\u52a1\u7aef\u4ecd\u7136\u53ef\u4ee5\u8fd4\u56de\u6240\u6709\u5141\u8bb8\u7684\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u5ba2\u6237\u7aef\u5c06\u5176\u7f13\u5b58\u3002\n- Access-Control-Allow-Headers\uff08\u5f53\u9884\u8bf7\u6c42\u4e2d\u5305\u542bAccess-Control-Request-Headers\u65f6\u5fc5\u987b\u5305\u542b\uff09 \u2013 \u8fd9\u662f\u5bf9\u9884\u8bf7\u6c42\u5f53\u4e2dAccess-Control-Request-Headers\u7684\u56de\u590d\uff0c\u548c\u4e0a\u9762\u4e00\u6837\u662f\u4ee5\u9017\u53f7\u5206\u9694\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u8fd4\u56de\u6240\u6709\u652f\u6301\u7684\u5934\u90e8\u3002\u8fd9\u91cc\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\u6709\u9047\u5230\uff0c\u6240\u6709\u652f\u6301\u7684\u5934\u90e8\u4e00\u65f6\u53ef\u80fd\u4e0d\u80fd\u5b8c\u5168\u5199\u51fa\u6765\uff0c\u800c\u53c8\u4e0d\u60f3\u5728\u8fd9\u4e00\u5c42\u505a\u8fc7\u591a\u7684\u5224\u65ad\uff0c\u6ca1\u5173\u7cfb\uff0c\u4e8b\u5b9e\u4e0a\u901a\u8fc7request\u7684header\u53ef\u4ee5\u76f4\u63a5\u53d6\u5230Access-Control-Request-Headers\uff0c\u76f4\u63a5\u628a\u5bf9\u5e94\u7684value\u8bbe\u7f6e\u5230Access-Control-Allow-Headers\u5373\u53ef\u3002\n- Access-Control-Allow-Credentials\uff08\u53ef\u9009\uff09 \u2013 \u548c\u7b80\u5355\u8bf7\u6c42\u5f53\u4e2d\u4f5c\u7528\u76f8\u540c\u3002\n- Access-Control-Max-Age\uff08\u53ef\u9009\uff09 \u2013 \u4ee5\u79d2\u4e3a\u5355\u4f4d\u7684\u7f13\u5b58\u65f6\u95f4\u3002\u9884\u8bf7\u6c42\u7684\u7684\u53d1\u9001\u5e76\u975e\u514d\u8d39\u5348\u9910\uff0c\u5141\u8bb8\u65f6\u5e94\u5f53\u5c3d\u53ef\u80fd\u7f13\u5b58\u3002\n\n\u4e00\u65e6\u9884\u56de\u5e94\u5982\u671f\u800c\u81f3\uff0c\u6240\u8bf7\u6c42\u7684\u6743\u9650\u4e5f\u90fd\u5df2\u6ee1\u8db3\uff0c\u5219\u5b9e\u9645\u8bf7\u6c42\u5f00\u59cb\u53d1\u9001\u3002\n\n\u901a\u8fc7caniuse.com\u5f97\u77e5\uff0c\u76ee\u524d\u5927\u90e8\u5206Modern\u6d4f\u89c8\u5668\u5df2\u7ecf\u652f\u6301\u5b8c\u6574\u7684CORS\uff0c\u4f46IE\u76f4\u5230IE11\u624d\u5b8c\u7f8e\u652f\u6301\uff0c\u6240\u4ee5\u5bf9\u4e8ePC\u7f51\u7ad9\uff0c\u8fd8\u662f\u5efa\u8bae\u91c7\u7528\u5176\u4ed6\u89e3\u51b3\u65b9\u6848\uff0c\u5982\u679c\u4ec5\u4ec5\u662f\u79fb\u52a8\u7aef\u7f51\u7ad9\uff0c\u5927\u53ef\u653e\u5fc3\u4f7f\u7528\u3002", "answer": "", "biz_type": 1, "qid": 305, "subject": "", "title": "cros \u7684\u7b80\u5355\u8bf7\u6c42\u548c\u590d\u6742\u8bf7\u6c42\u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4", "\u9ad8\u5fb7"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 5, "qid": 189, "qtype": "short_answer", "short_answer": {"analysis": "### BigInt\n\nJavaScript \u6240\u6709\u6570\u5b57\u90fd\u4fdd\u5b58\u6210 64 \u4f4d\u6d6e\u70b9\u6570\uff0c\u8fd9\u7ed9\u6570\u503c\u7684\u8868\u793a\u5e26\u6765\u4e86\u4e24\u5927\u9650\u5236\u3002\u4e00\u662f\u6570\u503c\u7684\u7cbe\u5ea6\u53ea\u80fd\u5230 53 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\uff08\u76f8\u5f53\u4e8e 16 \u4e2a\u5341\u8fdb\u5236\u4f4d\uff09\uff0c\u5927\u4e8e\u8fd9\u4e2a\u8303\u56f4\u7684\u6574\u6570\uff0cJavaScript \u662f\u65e0\u6cd5\u7cbe\u786e\u8868\u793a\u7684\uff0c\u8fd9\u4f7f\u5f97 JavaScript \u4e0d\u9002\u5408\u5927\u89c4\u6a21\u7684\u7cbe\u786e\u8ba1\u7b97\u3002\u4e8c\u662f\u5927\u4e8e\u6216\u7b49\u4e8e2\u76841024\u6b21\u65b9\u7684\u6570\u503c\uff0cJavaScript \u65e0\u6cd5\u8868\u793a\uff0c\u4f1a\u8fd4\u56deInfinity\u3002\n\n```js\nMath.pow(2, 1024) // Infinity\n```\n\n#### 1\uff09\u4f7f\u7528\u573a\u666f\n\n\u5728\u5bf9\u5927\u6574\u6570\u6267\u884c\u6570\u5b66\u8fd0\u7b97\u65f6\uff0c\u4ee5\u4efb\u610f\u7cbe\u5ea6\u8868\u793a\u6574\u6570\u7684\u80fd\u529b\u5c24\u4e3a\u91cd\u8981, \u6bd4\u5982\u5e94\u7528\u4e8e\u79d1\u5b66\u548c\u91d1\u878d\u65b9\u9762\u7684\u8ba1\u7b97\u3002\n\n#### 2\uff09\u4f7f\u7528BigInt\n\n\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cJavaScript \u65b0\u589e\u4e86\u57fa\u672c\u6570\u636e\u7c7b\u578b BigInt \uff0c\u76ee\u7684\u5c31\u662f\u6bd4Number\u6570\u636e\u7c7b\u578b\u652f\u6301\u7684\u8303\u56f4\u66f4\u5927\u7684\u6574\u6570\u503c\u3002\n\n\u4f7f\u7528 JavaScript \u63d0\u4f9b\u7684BigInt\u5bf9\u8c61\uff0c \u53ef\u4ee5\u7528\u4f5c\u6784\u9020\u51fd\u6570\u751f\u6210 BigInt \u7c7b\u578b\u7684\u6570\u503c\u3002\n\n\u8f6c\u6362\u89c4\u5219\u57fa\u672c\u4e0e Number() \u4e00\u81f4\uff0c\u5c06\u5176\u4ed6\u7c7b\u578b\u7684\u503c\u8f6c\u4e3a BigInt\u3002\n\n```js\nBigInt(123) // 123n\nBigInt('123') // 123n\nBigInt(false) // 0n\nBigInt(true) // 1n\n```\n\nBigInt()\u6784\u9020\u51fd\u6570\u5fc5\u987b\u6709\u53c2\u6570\uff0c\u800c\u4e14\u53c2\u6570\u5fc5\u987b\u53ef\u4ee5\u6b63\u5e38\u8f6c\u4e3a\u6570\u503c\uff0c\u4e0b\u9762\u7684\u7528\u6cd5\u90fd\u4f1a\u62a5\u9519\u3002 \n\n```js\nnew BigInt() // TypeError\nBigInt(undefined) //TypeError\nBigInt(null) // TypeError\nBigInt('123n') // SyntaxError\nBigInt('abc') // SyntaxError\n```\n\n\u6216\u662f\u76f4\u63a5\u5728\u6574\u6570\u672b\u5c3e\u8ffd\u52a0n\u5373\u53ef\uff0c\u6bd4\u5982:\n\n```js\nconsole.log(9999999999999999n); // 9999999999999999n\nconsole.log(9999999999999999); // 10000000000000000\n```\n\n\n\u5728\u7c7b\u578b\u5224\u65ad\u4e0a\uff0c\u4e0d\u80fd\u4f7f\u7528\u4e25\u683c\u8fd0\u7b97\u7b26\u53bb\u6bd4\u8f83BigInt\u4e0e Number\u7c7b\u578b\uff0c\u5b83\u4eec\u7684\u7c7b\u578b\u4e0d\u540c\u56e0\u6b64\u4e3afalse, \u800c\u76f8\u7b49\u8fd0\u7b97\u7b26\u5219\u4f1a\u8fdb\u884c\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 , \u56e0\u6b64\u4e3atrue\n\n```js\nconsole.log(100n === 100); // false\nconsole.log(100n == 100); // true \n```\n\nBigInt \u65e0\u6cd5\u4f7f\u7528`+`\u4e00\u5143\u8fd0\u7b97\u7b26\uff0c \u4e5f\u65e0\u6cd5\u548c Number \u4e00\u540c\u8ba1\u7b97\n\n```js\n+10n // Uncaught TypeError: Cannot convert a BigInt value to a number\n20n / 10 // Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions\n```\n\n\u5f53\u4f7f\u7528 BigInt \u8fdb\u884c\u8ba1\u7b97\u65f6\uff0c\u7ed3\u679c\u540c\u6837\u4f1a\u8fd4\u56deBigInt\u503c\u3002 \u5e76\u4e14\u9664\u6cd5(/)\u8fd0\u7b97\u7b26\u7684\u7ed3\u679c\u4f1a\u81ea\u52a8\u5411\u4e0b\u820d\u5165\u5230\u6700\u63a5\u8fd1\u7684\u6574\u6570\u3002\n\n```js\n25n / 10n // 10n\n```\n\n#### 3\uff09\u603b\u7ed3\nBigInt \u662f\u4e00\u79cd\u65b0\u7684\u57fa\u672c\u7c7b\u578b\uff0c\u7528\u4e8e\u5f53\u6574\u6570\u503c\u5927\u4e8e Number \u6570\u636e\u7c7b\u578b\u7684\u8303\u56f4\u65f6\u3002\u4f7f\u7528 BigInt \u907f\u514d\u6574\u6570\u6ea2\u51fa\uff0c\u4fdd\u8bc1\u8ba1\u7b97\u5b89\u5168\u3002\n\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u8981\u907f\u514d BigInt \u4e0e Number \u548c `+`\u4e00\u5143\u8fd0\u7b97\u7b26\u540c\u65f6\u4f7f\u7528\u3002", "answer": "", "biz_type": 1, "qid": 189, "subject": "", "title": "\u8bf4\u4e00\u4e0b\u5bf9`BigInt`\u7684\u7406\u89e3\uff0c\u5728\u4ec0\u4e48\u573a\u666f\u4e0b\u4f1a\u4f7f\u7528"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 152, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 152, "subject": "", "title": "\u6539\u53d8 this \u6307\u5411\u7684\u65b9\u5f0f\u90fd\u6709\u54ea\u4e9b\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fae\u8f6f"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 8, "qid": 165, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 165, "subject": "", "title": "\u8282\u6d41"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6613\u8f66"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 169, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 169, "subject": "", "title": "\u5982\u4f55\u5b9e\u73b0 5 \u79d2\u81ea\u52a8\u5237\u65b0\u4e00\u6b21\u9875\u9762(\u5177\u4f53\u90fd\u6709\u4ec0\u4e48\u65b9\u6cd5 reload \u4e4b\u7c7b\u7684)"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6613\u8f66", "\u8109\u8109", "\u864e\u6251", "\u559c\u9a6c\u62c9\u96c5", "\u767e\u5206\u70b9", "\u6d77\u98ce\u6559\u80b2", "58"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 170, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 170, "subject": "", "title": "\u90fd\u4e86\u89e3\u54ea\u4e9b ES6\u3001ES7 \u7684\u65b0\u7279\u6027\uff0c\u7bad\u5934\u51fd\u6570\u53ef\u4ee5\u88ab new \u5417"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6613\u8f66", "\u8109\u8109", "\u5fae\u533b", "\u6d77\u5eb7\u5a01\u89c6"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 171, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 171, "subject": "", "title": "\u8bf4\u4e00\u4e0b JavaScript \u7ee7\u627f\u90fd\u6709\u54ea\u4e9b\u65b9\u6cd5"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u65b0\u4e1c\u65b9"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 175, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 175, "subject": "```js\nvar arry = [];\nvar obj = {};\n// arry,obj \u8f6c\u6210\u5b57\u7b26\u4e32\u7684\u7ed3\u679c\u662f\u4ec0\u4e48\uff1f\n```\n", "title": "\u6570\u7ec4\u548c\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u65b0\u4e1c\u65b9"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 176, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 176, "subject": "```js\nvar a = {\n name: \"A\",\n fn() {\n console.log(this.name);\n },\n};\na.fn();\na.fn.call({ name: \"B\" });\nvar fn1 = a.fn;\nfn1();\n// \u5199\u51fa\u6253\u5370\u7ed3\u679c\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee5\u4e0b\u4ee3\u7801\u7684\u6253\u5370\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u65b0\u4e1c\u65b9"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 177, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 177, "subject": "```js\nlet int = 1;\nsetTimeout(function () {\n console.log(int);\n int = 2;\n new Promise((resolve, reject) => {\n resolve();\n }).then(function () {\n console.log(int);\n int = 7;\n });\n console.log(int);\n});\nint = 3;\nconsole.log(int);\nnew Promise((resolve, reject) => {\n console.log(int);\n return resolve((int = 4));\n}).then(function (res) {\n console.log(int);\n int = 5;\n setTimeout(function () {\n console.log(int);\n int = 8;\n });\n return false;\n});\nconsole.log(int);\n// \u5199\u51fa\u6253\u5370\u7ed3\u679c\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee5\u4e0b\u4ee3\u7801\u7684\u6253\u5370\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 10, "qid": 182, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 182, "subject": "", "title": "\u8bf7\u7ed9\u51fa\u8bc6\u522b Email \u7684\u6b63\u5219\u8868\u8fbe\u5f0f"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 6, "qid": 183, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 183, "subject": "", "title": "\u8bbe\u8ba1 AutoComplete \u7ec4\u4ef6(\u53c8\u53eb\u641c\u7d22\u7ec4\u4ef6\u3001\u81ea\u52a8\u8865\u5168\u7ec4\u4ef6\u7b49)\u65f6\uff0c\u9700\u8981\u8003\u8651\u4ec0\u4e48\u95ee\u9898\uff1f"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 5, "qid": 190, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 190, "subject": "", "title": "null \u662f\u4e0d\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5982\u679c\u662f\uff0c\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f null\uff0c\u4e0d\u4f7f\u7528 JavaScript \u63d0\u4f9b\u7684 api \u5982\u4f55\u8fdb\u884c\u5224\u65ad"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 204, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 204, "subject": "```js\nvar a = { x: 1 };\nvar b = a;\na.x = a = { n: 1 };\nconsole.log(a); // ?\nconsole.log(b); // ?\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee5\u4e0b\u4ee3\u7801\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 205, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 205, "subject": "```js\nFunction.prototype.a = () = >{alert(1)}\nObject.prototype.b = () = >{alert(2)}\nfunction A(){};\nconst a = new A();\na.a();\na.b();\n// \u5199\u51fa\u6267\u884c\u7ed3\u679c\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee5\u4e0b\u4ee3\u7801\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 206, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 206, "subject": "```js\nlet a = 0;\nconsole.log(a);\nconsole.log(b);\nlet b = 0;\nconsole.log(c);\nfunction c() {}\n// \u5199\u51fa\u6267\u884c\u7ed3\u679c\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee5\u4e0b\u4ee3\u7801\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 207, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 207, "subject": "```js\nvar x = 10;\nfunction a(y) {\n var x = 20;\n return b(y);\n}\nfunction b(y) {\n return x + y;\n}\na(20);\n// \u5199\u51fa\u6267\u884c\u7ed3\u679c\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee5\u4e0b\u4ee3\u7801\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 209, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 209, "subject": "```js\n[1, 2, 3, 4, 5].map(parselnt);\n// \u5199\u51fa\u6267\u884c\u7ed3\u679c\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee5\u4e0b\u4ee3\u7801\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 210, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 210, "subject": "```js\ntypeof typeof typeof [];\n// \u5199\u51fa\u6267\u884c\u7ed3\u679c\n```\n", "title": "\u8bf7\u5199\u51fa\u4ee5\u4e0b\u4ee3\u7801\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 8, "qid": 215, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 215, "subject": "", "title": "\u5b9e\u73b0\u4e00\u4e2a\u51fd\u6570\u67ef\u91cc\u5316"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u81ea\u5982"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 8, "qid": 239, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 239, "subject": "", "title": "\u8bf7\u5b9e\u73b0`$on,$emit`"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u81ea\u5982"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 9, "qid": 241, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 241, "subject": "", "title": "\u624b\u5199\u5b9e\u73b0 sleep \u51fd\u6570"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u81ea\u5982"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 244, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 244, "subject": "", "title": "\u8bf7\u5199\u51fa\u539f\u751f js \u5982\u4f55\u8bbe\u7f6e\u5143\u7d20\u9ad8\u5ea6"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 8, "qid": 251, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 251, "subject": "", "title": "\u6362\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 8, "qid": 253, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 253, "subject": "", "title": "\u8f93\u5165\u4e00\u4e2a\u65e5\u671f \u8fd4\u56de\u51e0\u79d2\u524d\u3001\u51e0\u5c0f\u65f6\u524d\u3001\u51e0\u5929\u524d\u3001\u51e0\u6708\u524d"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 8, "qid": 254, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 254, "subject": "", "title": "\u5c06 153812.7 \u8f6c\u5316\u4e3a 153,812.7"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 276, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 276, "subject": "", "title": "valueOf \u4e0e toString \u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u83dc\u9e1f\u7f51\u7edc"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 279, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 279, "subject": "", "title": "\u600e\u4e48\u5224\u65ad\u662f\u4e00\u4e2a\u7a7a\u5bf9\u8c61"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 280, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 280, "subject": "```js\nsetTimeout(() => {\n console.log(0);\n}, 0);\nnew Promise((res) => setTimeout(res, 0)).then(() => {\n console.log(1);\n setTimeout(() => {\n console.log(2);\n }, 0);\n new Promise((r = r())).then(() => {\n console.log(3);\n });\n});\nsetTimeout(() => {\n console.log(4);\n}, 0);\nnew Promise((res) => res()).then(() => {\n console.log(5);\n});\n```\n", "title": "\u8bf7\u5199\u51fa\u4e0b\u9762\u4ee3\u7801\u7684\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u817e\u8baf\u5e94\u7528\u5b9d"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 287, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 287, "subject": "", "title": "setTimeout \u4e0e setInterval \u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6316\u8d22"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 289, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 289, "subject": "", "title": "\u9879\u76ee\u4e2d\u5982\u4f55\u5e94\u7528\u6570\u636e\u7ed3\u6784"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5bfa\u5e93"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 293, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 293, "subject": "", "title": "\u95ed\u5305\u7684\u6838\u5fc3\u662f\u4ec0\u4e48"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u817e\u8baf\u5e94\u7528\u5b9d", "\u5b89\u5c45\u5ba2"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 298, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 298, "subject": "", "title": "\u4ecb\u7ecd\u4e8b\u4ef6\u5192\u6ce1\u3001\u4e8b\u4ef6\u4ee3\u7406\u3001\u4e8b\u4ef6\u6355\u83b7\uff0c\u4ee5\u53ca\u5b83\u4eec\u7684\u5173\u7cfb"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fae\u533b"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 302, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 302, "subject": "", "title": "promise \u7684\u72b6\u6001\u6709\u54ea\u4e9b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u864e\u6251"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 264, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u9519\u8bef\u6355\u83b7\u65b9\u5f0f\n\n#### 1.try/catch\n\n\u4e00\u822c\u60c5\u51b5\u4e0b async/await \u5728\u9519\u8bef\u5904\u7406\u65b9\u9762\uff0c\u4e3b\u8981\u4f7f\u7528 try/catch\uff0c\u50cf\u8fd9\u6837\n\n```js\nconst fetchData = () => {\n return new Promise((resolve, reject) => {\n setTimeout(() => {\n resolve('fetch data is me')\n }, 1000)\n })\n}\n\n(async () => {\n try {\n const data = await fetchData()\n console.log('data is ->', data)\n } catch(err) {\n console.log('err is ->', err)\n }\n})()\n\n```\n\n\u8fd9\u4e48\u770b\u5012\u662f\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\uff0c\u4f46\u662f\u5982\u679c\u6709\u591a\u4e2a\u5f02\u6b65\u64cd\u4f5c\u5462\uff1f\u90a3\u5c31\u9700\u8981\u4e9b\u591a\u4e2a `try catch`\u3002\u6240\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u95f4\u63a5\u6027\u5c31\u5f88\u5dee\u3002\n\n#### 1.2 \u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u5f0f\n\n\n\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u5f0f\uff0casync/await \u672c\u8d28\u5c31\u662f promise \u7684\u8bed\u6cd5\u7cd6\uff0c\u65e2\u7136\u662f promise \u90a3\u4e48\u5c31\u53ef\u4ee5\u4f7f\u7528 then \u51fd\u6570\u4e86\n\n\n1. async\u51fd\u6570\u672c\u8eab\u4f1a\u8fd4\u56de\u4e00\u4e2apromise,\u53ef\u4ee5\u901a\u8fc7catch\u83b7\u53d6\n2. async\u5185\u90e8\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7try...catch\u6765\u83b7\u53d6\uff0c\u539f\u672c\u5c31\u662fgenerator\u8bed\u6cd5\u7cd6\n3. \u57fa\u4e8e\u8fd9\u4e2a\u673a\u5236\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u9519\u8bef\u6355\u6349\n\n```js\nexport default function errorHanlder(promise) {\n return promise.then(data => {\n return [null, data];\n })\n .catch(err => [err]);\n}\n```\n\n\n", "answer": "", "biz_type": 1, "qid": 264, "subject": "", "title": "async\u3001await \u5982\u4f55\u8fdb\u884c\u9519\u8bef\u6355\u83b7"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 187, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u6267\u884c\u7ed3\u679c\n\n```js\n{a: 3}\n{a: 2}\n```\n\n### \u89e3\u6790\n\njs \u4f20\u9012\u503c\u7684\u65b9\u5f0f\u662f\u6309\u503c\u4f20\u9012\uff0c\u5982\u679c\u4f20\u9012\u7684\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u5219\u4f20\u9012\u7684\u662f*\u503c\u7684\u526f\u672c*\uff0c\u5982\u679c\u4f20\u9012\u7684\u53c2\u6570\u662f\u5f15\u7528\u7c7b\u578b\uff0c\u5219\u4f20\u9012\u7684\u503c\u662f\u6570\u636e\u7684*\u5f15\u7528\u5730\u5740\u526f\u672c* \n\u6240\u4ee5\uff0c\u5f53\u4f20\u9012\u7684\u503c\u4e3a\u5f15\u7528\u7c7b\u578b\u65f6\uff0c\u4f20\u9012\u7684\u662f\u5f15\u7528\u5730\u5740\u526f\u672c\uff0c\u901a\u8fc7\u5f15\u7528\u7c7b\u578b\u7684\u5730\u5740\u526f\u672c\uff0c\u6240\u6307\u5411\u7684\u6570\u636e\u5730\u5740\u90fd\u662f\u540c\u4e00\u4e2a\u5185\u5b58\u5806\uff0c\u6240\u4ee5\u6539\u53d8\u5f15\u7528\u5730\u5740\u526f\u672c\u7684\u6570\u636e\uff0c\u539f\u6765\u7684\u5f15\u7528\u6570\u636e\u4e5f\u4f1a\u53d1\u751f\u6539\u53d8\uff0c\u4f46\u662f\uff0c\u5728\u9898\u4e2d\uff0c\u7ed9 obj \u91cd\u65b0\u8d4b\u503c\u4e86\u65b0\u7684\u5bf9\u8c61\uff0c\u5373\u6539\u53d8\u4e86\u5f15\u7528\u5730\u5740\u526f\u672c\uff0c\u6240\u4ee5\u6700\u7ec8\u770b\u5230\u4e0a\u9762\u8fd9\u6837\u7684\u7ed3\u679c\n\n\u770b\u4ee5\u4e0b\u7684\u4e24\u4e2a\u4f8b\u5b50\uff0c\u7406\u89e3\u4ee5\u4e0a\u89e3\u91ca\n\n```js\n function a(obj){\n obj = 123;\n return obj;\n }\n const obj = {\n a: 1\n };\n console.log(a(obj)); // 123\n console.log(obj); // {a:1} \u539f\u6765\u5f15\u7528\u6570\u636e\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\n```\n\n```js\n function a(obj) {\n obj.a = 123;\n return obj;\n }\n const obj = {\n a: 1\n };\n console.log(a(obj)); // {a:123}\n console.log(obj); // {a:123} \u539f\u6765\u5f15\u7528\u6570\u636e\u53d7\u5230\u5f71\u54cd\n console.log(a(obj == obj)); // true \u6307\u5411\u540c\u4e00\u5730\u5740\n```\n\n```js\n function a(obj) {\n obj.a = 123;\n obj = 'change address'\n return obj;\n }\n const obj = {\n a: 1\n };\n console.log(a(obj)); // {a:change address} \u6539\u53d8\u4e86\u5730\u5740\u526f\u672c\n console.log(obj); // {a:123} \u539f\u6765\u5f15\u7528\u6570\u636e\u53d7\u5230\u5f71\u54cd\n console.log(a(obj) == obj); // false \u6307\u5411\u4e0d\u540c\u5730\u5740\n```", "answer": "", "biz_type": 1, "qid": 187, "subject": "```js\nfunction a(obj) {\n obj.a = 2;\n obj = { a: 3 };\n return obj;\n}\nconst obj = { a: 1 };\na(obj);\nconsole.log(obj);\n```\n", "title": "\u8bf7\u5199\u51fa\u4e00\u4e0b\u4ee3\u7801\u7684\u6253\u5370\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 281, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u6267\u884c\u7ed3\u679c\n\n1. getName();\u7531\u4e8e\u8fd9\u91cc\u53d8\u91cf getName \u63d0\u5347\u4e3a undefined\uff0c\u6240\u4ee5\u62a5\u9519\n2. \u5ffd\u7565\u7b2c\u4e00\u6b65\uff1bgetName \u6b64\u65f6\u8d4b\u503c\u6210\u4e86\u51fd\u6570\u6240\u4ee5\u6253\u5370 4\n3. Foo.getName()\u662f Foo \u7684\u9759\u6001\u65b9\u6cd5\u8c03\u7528\u6240\u4ee5 2\n4. new Foo()\u4f18\u5148\u7ea7\u5927\u4e8e.\u8fd0\u7b97\uff0c\u6240\u4ee5 getName \u662f\u5f53\u524d\u5b9e\u4f8b\u7684\u8c03\u7528\uff0c\u5219\u67e5\u5230\u5230\u539f\u578b\u7684\u65b9\u6cd5 getName \u6253\u5370 3\n\n```js\n\u62a5\u9519\n4\n2\n3\n```\n", "answer": "", "biz_type": 1, "qid": 281, "subject": "```js\nfunction Foo() {\n getName = function () {\n alert(1);\n };\n return this;\n}\ngetName();\nFoo.getName = function () {\n alert(2);\n};\nFoo.prototype.getName = function () {\n alert(3);\n};\ngetName = function () {\n alert(4);\n};\n\n// \u8bf7\u5199\u51fa\u4e0b\u9762\u7684\u8f93\u51fa\u7ed3\u679c\ngetName90;\nFoo.getName();\nnew Foo().getName();\n```\n", "title": "\u8bf7\u5199\u51fa\u4e0b\u9762\u4ee3\u7801\u7684\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u65b0\u4e1c\u65b9"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 8, "qid": 179, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n\u89e3\u51b3\u6b64\u9898\u7684\u5173\u952e\u5c31\u662f\u77e5\u9053\u5728for\u5faa\u73af\u7684\u65f6\u5019\uff0c\u5982\u4f55\u8bb0\u4f4f\u6211\u662f\u8c01\u3002\n\n\u89e3\u51b3\u65b9\u6848\u6e05\u6670\u5f88\u591a\uff1a \u5757\u7ea7\u4f5c\u7528\u57df\u6216\u8005\u95ed\u5305\n\n#### \u65b9\u6848\u4e00\n\nlet \u548cconst\u58f0\u660e\u7684\u53d8\u91cf\u9047\u5230{\u5757}\u90fd\u4f1a\u5f62\u6210\u4e00\u4e2a\u5757\uff0c\u6b64\u65f6i\u662f\u4e2a\u53d8\u91cf\uff0c\u4f7f\u7528let\n\n```js\nfor (let i = 0; i < 10; i++) {\n arrys.push(function () {\n return i;\n });\n}\n```\n\n#### \u65b9\u6848\u4e8c\n\n\u81ea\u6267\u884c\u51fd\u6570\u5f62\u6210\u95ed\u5305\n\n```js\nfor (var i = 0; i < 10; i++) {\n (function(j){\n arrys.push(function () {\n return j;\n });\n })(i);\n}\n```\n\n#### \u65b9\u6848\u4e09\n\n\u5185\u90e8\u51fd\u6570\u81ea\u6267\u884c\u5f62\u6210\u95ed\u5305\n\n```js\nfor (var i = 0; i < 10; i++) {\n arrys.push((function (j) {\n return _ => j;\n })(i));\n}\n```\n", "answer": "", "biz_type": 1, "qid": 179, "subject": "```js\nvar arrys = [];\nfor (var i = 0; i < 10; i++) {\n arrys.push(function () {\n return i;\n });\n}\narrys.forEach(function (fn) {\n console.log(fn());\n}); //\u672c\u2f8f\u4e0d\u80fd\u4fee\u6539\n```\n", "title": "\u4fee\u6539\u4ee5\u4e0b\u4ee3\u7801\uff0c\u4f7f\u5f97\u6700\u540e\u2f00\u2f8f\u4ee3\u7801\u80fd\u591f\u8f93\u51fa\u6570\u5b57 0-9\uff08\u6700\u597d\u80fd\u7ed9\u591a\u79cd\u7b54\u6848\uff09"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 283, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n```js\nArray.from({length:n}, () => Math.random()*10)\n\n// \u751f\u6210 \u957f\u5ea6\u4e3a10 \u7684\u968f\u673a\u6570\u7ec4\nArray.from({length:10}, () => Math.random()*10)\n```", "answer": "", "biz_type": 1, "qid": 283, "subject": "", "title": "\u8bf7\u53ea\u7528\u6570\u7ec4\u65b9\u6cd5\u548c Math.random()\u5728\u4e00\u6761\u8bed\u53e5\u7684\u60c5\u51b5\u4e0b\uff0c\u5b9e\u73b0\u751f\u6210\u7ed9\u5b9a\u4f4d\u6570\u7684\u968f\u673a\u6570\u7ec4\uff0c\u4f8b\u5982\u751f\u6210 10 \u4f4d\u968f\u673a\u6570\u7ec4[1.1,102.1,2,3,8,4,90,123,11,123],\u6570\u7ec4\u5185\u6570\u5b57\u968f\u673a\u751f\u6210\u3002"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 8, "qid": 284, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n- \u5b9e\u73b0\u4e00\n\n```js\nlet setter = function (conten, key, value) {\n let argArr = key.split('.');\n let i = argArr.shift();\n if (argArr.length==0){\n conten[i]=value;\n }else{\n conten[i]=setter(conten[i],argArr.join('.'),value);\n }\n return conten;\n};\nlet n = {\n a: {\n b: {\n c: { d: 1 },\n bx: { y: 1 },\n },\n ax: { y: 1 },\n },\n};\n// \u4fee\u6539\u503c\nsetter(n, \"a.b.c.d\", 3);\nconsole.log(n.a.b.c.d); //3\nsetter(n, \"a.b.bx\", 1);\nconsole.log(n.a.b.bx); //1\n```\n\n- \u5b9e\u73b0\u4e8c\n\n```js\nlet setter = function (content, key, value) {\n try {\n let keyArr = key.split(\".\");\n let { obj, k } = keyArr.reduce((content, k, i) => {\n if (i !== keyArr.length - 1) {\n return content[k];\n } else {\n return { obj: content, k };\n }\n }, content);\n obj[k] = value;\n } catch (e) {\n console.warn(\"\u8f93\u5165key\u6709\u8bef\");\n }\n};\nlet n = {\n a: {\n b: {\n c: { d: 1 },\n bx: { y: 1 },\n },\n ax: { y: 1 },\n },\n};\n// \u4fee\u6539\u503c\nsetter(n, \"a.b.c.d\", 3);\nconsole.log(n.a.b.c.d); //3\nsetter(n, \"a.b.bx\", 1);\nconsole.log(n.a.b.bx); //1\n```\n\n- \u5b9e\u73b0\u4e09\n\n```js\nlet setter = function(content,key,value){\n\tvar proto = key.split('.');\n\tvar len = proto.length;\n\tproto.reduce(function(obj,val,index){\n\t\tif(index === len-1){\n\t\t\treturn obj[val] = value;\n\t\t}\n\t\tif (typeof obj[val] != 'object') obj[val] = {};\n\t\treturn obj[val];\n\t},content)\n}\nlet n = {\n a: {\n b: {\n c: { d: 1 },\n bx: { y: 1 },\n },\n ax: { y: 1 },\n },\n};\n// \u4fee\u6539\u503c\nsetter(n, \"a.b.c.d\", 3);\nconsole.log(n.a.b.c.d); //3\nsetter(n, \"a.b.bx\", 1);\nconsole.log(n.a.b.bx); //1\n```", "answer": "", "biz_type": 1, "qid": 284, "subject": "```js\nlet setter = function (conten, key, value) {\n // your code\n};\nlet n = {\n a: {\n b: {\n c: { d: 1 },\n bx: { y: 1 },\n },\n ax: { y: 1 },\n },\n};\n// \u4fee\u6539\u503c\nsetter(n, \"a.b.c.d\", 3);\nconsole.log(n.a.b.c.d); //3\nsetter(n, \"a.b.bx\", 1);\nconsole.log(n.b.bx); //1\n```\n", "title": "\u5b9e\u73b0\u4e00\u4e2a setter \u65b9\u6cd5"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u65b0\u4e1c\u65b9"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 8, "qid": 178, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n#### \u65b9\u6cd5\u4e00\n\n```js\nlet _name = 'sfd'\nA = {\n\tget name() {\n\t\treturn _name\n\t},\n set name(name) {\n _name = name\n this.getName()\n },\n getName: function() {\n console.log(this.name)\n }\n}\n```\n\n#### \u65b9\u6cd5\u4e8c\n\n```js\nconst A = {\n getName: function() {\n console.log(this.name)\n }\n}\nlet _name = 'sfd'\nObject.defineProperty(A, 'name', {\n enumerable: true,\n configurable: true,\n get() {\n return _name\n },\n set(name) {\n _name = name\n this.getName()\n }\n})\n```\n\n#### \u65b9\u6cd5\u4e09\n\n```js\nconst _A = {\n name: 'sfd',\n\tgetName: function() {\n\t\tconsole.log(this.name)\n\t}\n}\nconst A = new Proxy(_A, {\n get(target, key, receiver) {\n return Reflect.get(target, key, receiver) \n },\n set(target, key, value, receiver) {\n const res = Reflect.set(target, key, value, receiver)\n target.getName()\n return res\n }\n})\n```", "answer": "", "biz_type": 1, "qid": 178, "subject": "```js\n/*\n\t\u5df2\u77e5\u5bf9\u8c61A = {name: 'sfd', getName: function(){console.log(this.name)}},\n\t\u73b0\u8981\u6c42\u2f64\u4e0d\u540c\u2f45\u5f0f\u5bf9A\u8fdb\u2f8f\u6539\u9020\u5b9e\u73b0A.name\u53d1\u2f63\u53d8\u5316\u65f6\u2f74\u5373\u6267\u2f8fA.getName\n*/\n```\n", "title": "\u8981\u6c42\u2f64\u4e0d\u540c\u2f45\u5f0f\u5bf9 A \u8fdb\u2f8f\u6539\u9020\u5b9e\u73b0 A.name \u53d1\u2f63\u53d8\u5316\u65f6\u2f74\u5373\u6267\u2f8f A.getName"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4", "\u9ad8\u5fb7"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 5, "qid": 192, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4e00\u3001\u7406\u89e3\n\n\u5806\u548c\u6808\u5176\u5b9e\u662f\u4e24\u79cd\u6570\u636e\u7ed3\u6784\u3002\u5806\u6808\u90fd\u662f\u4e00\u79cd\u6570\u636e\u9879\u6309\u5e8f\u6392\u5217\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ea\u80fd\u5728\u4e00\u7aef(\u79f0\u4e3a\u6808\u9876(top))\u5bf9\u6570\u636e\u9879\u8fdb\u884c\u63d2\u5165\u548c\u5220\u9664\u3002\u5806\u6808\u662f\u4e2a\u7279\u6b8a\u7684\u5b58\u50a8\u533a\uff0c\u4e3b\u8981\u529f\u80fd\u662f\u6682\u65f6\u5b58\u653e\u6570\u636e\u548c\u5730\u5740\u3002\n\n\n#### 1.\u5806\n\n- \u5806\u5185\u5b58\u7684\u5b58\u50a8\u7684\u503c\u7684\u5927\u5c0f\u4e0d\u5b9a\uff0c\u662f\u7531\u7a0b\u5e8f\u5458\u81ea\u5df1\u7533\u8bf7\u5e76\u6307\u660e\u5927\u5c0f\u3002\u56e0\u4e3a\u5806\u5185\u5b58\u662f new \u5206\u914d\u7684\u5185\u5b58\uff0c\u6240\u4ee5\u8fd0\u884c\u6548\u7387\u4f1a\u8f83\u4f4e\u3002\n- \u5806\u5185\u5b58\u662f\u5411\u9ad8\u5730\u5740\u6269\u5c55\u7684\u6570\u636e\u7ed3\u6784\uff0c\u662f\u4e0d\u8fde\u7eed\u7684\u5185\u5b58\u533a\u57df\uff0c\u7cfb\u7edf\u4e5f\u662f\u7528\u94fe\u8868\u6765\u5b58\u50a8\u7a7a\u95f2\u7684\u5185\u5b58\u5730\u5740\uff0c\u6240\u4ee5\u662f\u4e0d\u8fde\u7eed\u7684\u3002\u56e0\u4e3a\u662f\u8bb0\u5f55\u7684\u5185\u5b58\u5730\u5740\uff0c\u6240\u4ee5\u83b7\u53d6\u662f\u901a\u8fc7\u5f15\u7528\uff0c\u5b58\u50a8\u7684\u662f\u5bf9\u8c61\u5c45\u591a\u3002\n- \u5806\u5185\u5b58\u7684\u56de\u6536\u662f\u4eba\u4e3a\u63a7\u5236\u7684\uff0c\u5f53\u7a0b\u5e8f\u7ed3\u675f\u540e\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u56de\u6536\u3002\n\n#### 2.\u6808\n\n- \u6808\u5185\u5b58\u7684\u5b58\u50a8\u5927\u5c0f\u662f\u56fa\u5b9a\u7684\uff0c\u7533\u8bf7\u65f6\u7531\u7cfb\u7edf\u81ea\u52a8\u5206\u914d\u5185\u5b58\u7a7a\u95f4\uff0c\u8fd0\u884c\u7684\u6548\u7387\u6bd4\u8f83\u5feb\uff0c\u4f46\u662f\u56e0\u4e3a\u5b58\u50a8\u7684\u5927\u5c0f\u56fa\u5b9a\uff0c\u6240\u4ee5\u5bb9\u6613\u5b58\u50a8\u7684\u5927\u5c0f\u8d85\u8fc7\u5b58\u50a8\u7684\u5927\u5c0f\uff0c\u5bfc\u81f4\u6ea2\u6808\u3002\n- \u6808\u5185\u5b58\u5b58\u50a8\u7684\u662f\u57fa\u7840\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u4e14\u662f\u6309\u503c\u8bbf\u95ee\uff0c\u56e0\u4e3a\u6808\u662f\u4e00\u5757\u8fde\u7eed\u7684\u5185\u5b58\u533a\u57df\uff0c\u4ee5**\u540e\u8fdb\u5148\u51fa**\u7684\u539f\u5219\u5b58\u50a8\u8c03\u7528\u7684\uff0c\u6240\u4ee5\u662f\u8fde\u7eed\u5b58\u50a8\u7684\u3002\n- \u6808\u7684\u56de\u6536\u662f\u7cfb\u7edf\u63a7\u5236\u5b9e\u73b0\u7684\u3002", "answer": "", "biz_type": 1, "qid": 192, "subject": "", "title": "\u8bf4\u4e00\u4e0b\u5bf9\u4e8e\u5806\u6808\u7684\u7406\u89e3"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 156, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4e00\u3001\u533a\u522b\n\n`module.exports`\u548c`exports`\u662fnode \u652f\u6301\u7684\u5bfc\u51fa\u65b9\u5f0f\uff0c\u4e5f\u5c31\u662f commonjs \u5bfc\u51fa\u89c4\u8303\n\n`export`\u548c`export default`\u662f es6 \u652f\u6301\u7684\u5bfc\u51fa\u65b9\u5f0f\n\n#### 1.`module.exports`\u548c`exports`\u7684\u533a\u522b\n\n**exports**\n\nexports \u5bfc\u51fa\u7684\u662f module.exports \u7684\u5f15\u7528\uff0c\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u8bf4\uff0c\u4ee3\u7801\u5982\u4e0b\n\n```js\nconsole.log(exports) //{}\nconsole.log(module.exports) //{}\n// a.js\nconst a = 25;\nexports.a = a\n// b.js\nconst test = require('./a')\nconsole.log(test)\n// {a:25}\n```\n\n\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0cexports.a \u8fd9\u6837\u7684\u5bfc\u51fa\u7684\u65b9\u5f0f\uff0c\u5176\u5b9e\u4e5f\u662f\u5c06 module.exports \u5bfc\u51fa\u53bb\u4e86\uff0c\u53ea\u662f\u5f80\u5916\u7684\u5c06 a \u6302\u8f7d\u5230\u4e86 module.exports \u7684\u5185\u5b58\u5730\u5740\u4e0a \n\n\u4e3a\u4ec0\u4e48\u8bf4\u4e00\u5b9a\u662f module.exports \u7684\u5185\u5b58\u5730\u5740\u5462\uff0c\u518d\u770b\u4e0b\u9762\u7684\u4ee3\u7801\n\n```js\n// a.js\nconst a = 25;\nexports = a\n// b.js\nconst test = require('./a')\nconsole.log(test)\n// {}\n```\n\n\u53ef\u4ee5\u770b\u5230\uff0c\u5982\u679c\u76f4\u63a5 exports \u7684\u8bdd\uff0c\u8fd4\u56de\u662f\u4e00\u4efd\u65b0\u5185\u5b58\u5730\u5740\n\n**module.exports**\n\n\u533a\u522b\u5176\u5b9e\u5728\u4e0a\u9762\u5df2\u7ecf\u8bf4\u5b8c\u4e86\uff0c\u770b\u4e0b\u9762\u4ee3\u7801\n\n```js\n// a.js\nconst a = 25;\n\nmodule.exports = a\n// b.js\nconst test = require('./a')\nconsole.log(test)\n// 25\n```\n\n#### 2.`export`\u548c`export default`\u7684\u533a\u522b\n\n\n1\u3001`export default` \u53ea\u80fd\u5bfc\u51fa\u4e00\u4e2a\u53d8\u91cf\uff0c\u800c export \u53ef\u4ee5\u5bfc\u51fa\u591a\u4e2a\n\n2\u3001`export` \u5bfc\u51fa\u7684\u53d8\u91cf\u53ef\u4ee5\u4fee\u6539\uff0c`export default` \u5219\u4e0d\u884c\n\n```js\n// a.js\nlet e1='export 1';\nlet e2='export 2';\nexport {e2};\nexport default e1;\ne1='export 1 modified';\ne2='export 2 modified';\n// b.js\nimport e1, {e2} from \"./b\";\nconsole.log(e1); //export 1\nconsole.log(e2); // export 2 modified\n```\n\n3\u3001\u8bed\u6cd5\u5dee\u5f02\uff0cexport var/let/const xxx = xxx;\u662f\u5408\u6cd5\u8bed\u53e5\uff0c\u4f46\u662f export default \u5219\u4e0d\u53ef\u4ee5\n\n\n", "answer": "", "biz_type": 1, "qid": 156, "subject": "", "title": "\u8bf4\u4e00\u4e0b`module.exports`\u548c`exports`\u7684\u533a\u522b\uff0c`export`\u548c`export default`\u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613", "\u864e\u6251", "\u6caa\u6c5f"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 263, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4e00\u3001\u533a\u522b\n\n#### 1.\u5b9a\u4e49\n\n* `Promise`\u662f\u5bf9\u8c61\uff0c\u7528\u4e8e\u8868\u793a\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u7684\u6700\u7ec8\u5b8c\u6210 (\u6216\u5931\u8d25), \u53ca\u5176\u7ed3\u679c\u503c\u3002\n* async function \u662f\u58f0\u660e\u8bed\u53e5\uff0c\u7528\u6765\u5b9a\u4e49\u4e00\u4e2a\u8fd4\u56de `AsyncFunction` \u5bf9\u8c61\u7684\u5f02\u6b65\u51fd\u6570\uff0c\u5b83\u4f1a\u901a\u8fc7\u4e00\u4e2a\u9690\u5f0f\u7684 Promise \u8fd4\u56de\u5176\u7ed3\u679c\u3002\n* await \u662f\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u6682\u505c\u5f53\u524d\u5f02\u6b65\u51fd\u6570\u7684\u6267\u884c\uff0c\u7b49\u5f85 Promise \u5904\u7406\u5b8c\u6210\uff0c\u53ea\u5728\u5f02\u6b65\u51fd\u6570\u5185\u6709\u6548\u3002\n\n#### 2.\u9519\u8bef\u7684\u6355\u83b7\n\n* \u4f7f\u7528 async/await \u8bed\u6cd5\u80fd\u8ba9\u5f00\u53d1\u8005\u5728\u5f02\u6b65\u4ee3\u7801\u4e2d\u4e5f\u6309\u7167\u7c7b\u4f3c\u540c\u6b65\u4ee3\u7801\u4e2d\u4e00\u6837\u7684\u65b9\u5f0f\u4f7f\u7528 try-catch \u8bed\u53e5\u3002\n* Promise \u7684\u9519\u8bef\u65e0\u6cd5\u5728\u5916\u90e8\u88ab\u6355\u6349\u5230\uff0c\u53ea\u80fd\u5728\u5185\u90e8\u8fdb\u884c\u9884\u5224\u5904\u7406\n\n#### 3.\u4e2d\u65ad\n\n* Promise \u662f\u4e00\u4e2a\u72b6\u6001\u673a\uff0c\u672c\u8eab\u662f\u65e0\u6cd5\u5b8c\u5168\u4e2d\u6b62\u7684\u3002\n* async function \u4f7f\u7528\u8bed\u4e49\u5316\u7684 await \u4e2d\u65ad\u7a0b\u5e8f\u3002 \n\n### \u4e8c\u3001\u5173\u7cfb\n\n`async`/`await`\u7684\u76ee\u7684\u662f\u7b80\u5316\u4f7f\u7528\u591a\u4e2a promise \u65f6\u7684\u540c\u6b65\u884c\u4e3a\uff0c\u5e76\u5bf9\u4e00\u7ec4 `Promises`\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\u3002\u6b63\u5982`Promises`\u7c7b\u4f3c\u4e8e\u7ed3\u6784\u5316\u56de\u8c03\uff0c`async`/`await`\u66f4\u50cf\u7ed3\u5408\u4e86generators\u548c promises\u3002\n\n### \u4e09\u3001\u4f7f\u7528\u573a\u666f\n\n\u4e00\u822c\u5728\u5f02\u6b65\u5904\u7406\u65f6\u4f7f\u7528\uff0c\u800c\u901a\u5e38\u7684\uff0c\u7531\u4e8e\u5b83\u4eec\u4e4b\u95f4\u7d27\u5bc6\u7684\u5173\u7cfb\uff0c\u90fd\u662f\u642d\u914d\u4f7f\u7528\u7684\u3002\n\n1. Promise \u63d0\u4f9b\u7684\u5de5\u5177\u51fd\u6570\u5bf9\u5e94\u7684\u573a\u666f\uff0c\u5982 Promise.all \u5e76\u884c\u6267\u884c\u4e00\u7ec4 Promise\n2. async/await \u907f\u514d\u4e86\u7e41\u6742\u7684 Promise \u94fe\u5f0f\u8c03\u7528\uff0c\u4e14\u66f4\u52a0\u8bed\u4e49\u5316\uff0c\u63a8\u8350\u4f7f\u7528 async/await ", "answer": "", "biz_type": 1, "qid": 263, "subject": "", "title": "promise \u8ddf async await \u7684\u533a\u522b\uff0c\u4f7f\u7528\u573a\u666f \u2028"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 300, "qtype": "short_answer", "short_answer": {"analysis": "\n### map & for\n\n- map \u4f1a\u5148\u628a\u6267\u884c\u540c\u6b65\u64cd\u4f5c\u6267\u884c\u5b8c\uff0c\u5c31\u8fd4\u56de\uff0c\u4e4b\u540e\u518d\u4e00\u6b21\u4e00\u6b21\u7684\u6267\u884c\u5f02\u6b65\u4efb\u52a1\n- for \u662f\u7b49\u5f85\u5f02\u6b65\u8fd4\u56de\u7ed3\u679c\u540e\u518d\u8fdb\u5165\u4e0b\u4e00\u6b21\u5faa\u73af\n\n#### map\n\n```js\nconst arr = [1, 2, 3, 4, 5];\nfunction getData() {\n return new Promise((resolve, reject) => {\n setTimeout(() => {\n resolve(\"data\");\n }, 1000);\n });\n}\n\n(async () => {\n const result = arr.map(async () => {\n console.log(\"start\");\n const data = await getData();\n console.log(data);\n return data;\n });\n console.log(result);\n})();\n\n// 5 start -> \u904d\u5386\u6bcf\u4e00\u9879\u5f00\u59cb\n// (5)\u00a0[Promise, Promise, Promise, Promise, Promise] -> \u8fd4\u56de\u7684\u7ed3\u679c\n// 5 data -> \u904d\u5386\u6bcf\u4e00\u9879\u5f02\u6b65\u6267\u884c\u8fd4\u56de\u7684\u7ed3\u679c\n```\n\n#### \u5206\u6790\n\nmap \u51fd\u6570\u7684\u539f\u7406\u662f\uff1a\n\n1. \u5faa\u73af\u6570\u7ec4\uff0c\u628a\u6570\u7ec4\u6bcf\u4e00\u9879\u7684\u503c\uff0c\u4f20\u7ed9\u56de\u8c03\u51fd\u6570\n2. \u5c06\u56de\u8c03\u51fd\u6570\u5904\u7406\u540e\u7684\u7ed3\u679c push \u5230\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\n3. \u8fd4\u56de\u65b0\u6570\u7ec4\n\nmap \u51fd\u6570\u51fd\u6570\u662f\u540c\u6b65\u6267\u884c\u7684\uff0c\u5faa\u73af\u6bcf\u4e00\u9879\u65f6\uff0c\u5230\u7ed9\u65b0\u6570\u7ec4\u503c\u90fd\u662f\u540c\u6b65\u64cd\u4f5c\u3002\n\n\u4ee3\u7801\u6267\u884c\u7ed3\u679c\uff1a\n\nmap \u4e0d\u4f1a\u7b49\u5230\u56de\u8c03\u51fd\u6570\u7684\u5f02\u6b65\u51fd\u6570\u8fd4\u56de\u7ed3\u679c\uff0c\u5c31\u4f1a\u8fdb\u5165\u4e0b\u4e00\u6b21\u5faa\u73af\u3002\n\n\u6267\u884c\u5b8c\u540c\u6b65\u64cd\u4f5c\u4e4b\u540e\uff0c\u5c31\u4f1a\u8fd4\u56de\u7ed3\u679c\uff0c\u6240\u4ee5 map \u8fd4\u56de\u7684\u503c\u90fd\u662f Promise\n\n#### \u89e3\u51b3\u95ee\u9898\n\n- \u4f7f\u7528 for\u3001for..of \u4ee3\u66ff\n\n\u7b80\u5355\u5b9e\u73b0\u4e00\u4e2a\n\n```js\n// \u83b7\u53d6\u6570\u636e\u63a5\u53e3\nfunction getData() {\n return new Promise((resolve, reject) => {\n setTimeout(() => {\n resolve(\"data\");\n }, 1000);\n });\n}\n// \u5f02\u6b65\u7684map\nasync function selfMap(arr, fn) {\n let result = [];\n for (let i = 0, len = arr.length; i < len; i++) {\n const item = await fn(arr[i], i);\n result.push(item);\n }\n return result;\n}\n// \u8c03\u7528\n(async () => {\n const res = await selfMap([1, 2, 3, 4, 5], async (item, i) => {\n const data = await getData();\n return `${item}_${data}`;\n });\n console.log(res, \"res\");\n})();\n// [\"1_data\", \"2_data\", \"3_data\", \"4_data\", \"5_data\"] \"res\"\n```\n\n#### for\n\n```js\nfunction getData() {\n return new Promise((resolve, reject) => {\n setTimeout(() => {\n resolve(\"data\");\n }, 1000);\n });\n}\n\n(async () => {\n for (let i = 0, len = arr.length; i < len; i++) {\n console.log(i);\n const data = await getData();\n console.log(data);\n }\n})();\n\n// 0\n// data\n// 1\n// data\n// 2\n// data\n// 3\n// data\n// 4\n// data\n```", "answer": "", "biz_type": 1, "qid": 300, "subject": "", "title": "\u5728 map \u4e2d\u548c for \u4e2d\u8c03\u7528\u5f02\u6b65\u51fd\u6570\u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u81ea\u5982"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 8, "qid": 250, "qtype": "short_answer", "short_answer": {"analysis": "\n### JS \u81ea\u5b9a\u4e49\u4e8b\u4ef6\n\nJavascript\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u7c7b\u4f3c\u8bbe\u8ba1\u7684\u89c2\u5bdf\u8005\u6a21\u5f0f\uff0c\u901a\u8fc7\u72b6\u6001\u7684\u53d8\u66f4\u6765\u76d1\u542c\u884c\u4e3a\uff0c\u4e3b\u8981\u529f\u80fd\u89e3\u8026\uff0c\u6613\u4e8e\u6269\u5c55\u3002\u591a\u7528\u4e8e\u7ec4\u4ef6\u3001\u6a21\u5757\u95f4\u7684\u4ea4\u4e92\u3002\n\n\u539f\u578b\u6a21\u5f0f\u4e0b\u7684\u81ea\u5b9a\u4e49\u4e8b\u4ef6\n\n```js\nvar EventTarget = function() {\n this._listener = {};\n};\n\nEventTarget.prototype = {\n constructor: this,\n addEvent: function(type, fn) {\n if (typeof type === \"string\" && typeof fn === \"function\") {\n if (typeof this._listener[type] === \"undefined\") {\n this._listener[type] = [fn];\n } else {\n this._listener[type].push(fn);\n }\n }\n return this;\n },\n addEvents: function(obj) {\n obj = typeof obj === \"object\"? obj : {};\n var type;\n for (type in obj) {\n if ( type && typeof obj[type] === \"function\") {\n this.addEvent(type, obj[type]);\n }\n }\n return this;\n },\n fireEvent: function(type) {\n if (type && this._listener[type]) {\n //event\u53c2\u6570\u8bbe\u7f6e\n var events = {\n type: type,\n target: this\n };\n\n for (var length = this._listener[type].length, start=0; start<length; start+=1) {\n //\u6539\u53d8this\u6307\u5411\n this._listener[type][start].call(this, events);\n }\n }\n return this;\n },\n fireEvents: function(array) {\n if (array instanceof Array) {\n for (var i=0, length = array.length; i<length; i+=1) {\n this.fireEvent(array[i]);\n }\n }\n return this;\n },\n removeEvent: function(type, key) {\n var listeners = this._listener[type];\n if (listeners instanceof Array) {\n if (typeof key === \"function\") {\n for (var i=0, length=listeners.length; i<length; i+=1){\n if (listeners[i] === key){\n listeners.splice(i, 1);\n break;\n }\n }\n } else if (key instanceof Array) {\n for (var lis=0, lenkey = key.length; lis<lenkey; lis+=1) {\n this.removeEvent(type, key[lenkey]);\n }\n } else {\n delete this._listener[type];\n }\n }\n return this;\n },\n removeEvents: function(params) {\n if (params instanceof Array) {\n for (var i=0, length = params.length; i<length; i+=1) {\n this.removeEvent(params[i]);\n }\n } else if (typeof params === \"object\") {\n for (var type in params) {\n this.removeEvent(type, params[type]);\n }\n }\n return this;\n }\n};\n```", "answer": "", "biz_type": 1, "qid": 250, "subject": "", "title": "\u7528\u539f\u751f js \u5b9e\u73b0\u81ea\u5b9a\u4e49\u4e8b\u4ef6"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 4, "qid": 303, "qtype": "short_answer", "short_answer": {"analysis": "\n### ES6\u4e2d\u53ef\u7528\u7684\u5f02\u6b65\u65b9\u6cd5\n\n#### 1.Generator + Promise + \u6267\u884c\u5668\n\n```js\nconst fs = require('fs')\n \n// Promise \u7248\u7684readFile\nconst readFile = function (fileName) {\n return new Promise(function(resolve, reject) {\n fs.readFile(fileName, function(err, data){\n if (err) return reject(error);\n resolve(data);\n })\n })\n}\n \nconst gen = function * () {\n let f1 = yield readFile('a.txt');\n let f2 = yield readFile('b.txt');\n \n console.log('F1--->', f1.toString());\n console.log('F2--->', f2.toString());\n}\n \n \n// \u57fa\u4e8e Generator \u548c Promise \u7684\u81ea\u52a8\u6267\u884c\u5668\nfunction run(gen) {\n \n let g = gen();\n \n function next(data) {\n \n let result = g.next(data);\n \n if (result.done) return result.value;\n \n result.value.then(function(data) {\n next(data);\n });\n }\n next();\n}\n \nrun(gen);\n```\n\n#### 2.Generator + Thunk\u51fd\u6570 + \u6267\u884c\u5668\n\n```js\nconst fs = require('fs')\n \n// \u628a\u4e00\u4e2a\u5355\u4e00\u6267\u884c\u7684\u51fd\u6570 \uff0c\u53d8\u6210\u9700\u8981\u518d\u6b21\u8c03\u7528\u7684\u51fd\u6570\uff0c\u56fa\u5b9a\u4e00\u90e8\u5206\u53c2\u6570\nfunction thunkify(fn, obj = {}) {\n return function () {\n let args = Array.from(arguments);\n return function (m) {\n args.push(m)\n return fn.apply(obj, args)\n }\n }\n}\n \nconst readFile = thunkify(fs.readFile, fs);\n \nconst gen = function* () {\n let f1 = yield readFile('a.txt');\n let f2 = yield readFile('b.txt');\n \n console.log('F1-->', f1.toString());\n console.log('F2-->', f2.toString());\n}\n \n \n// \u57fa\u4e8e Generator \u548c Thunk\u51fd\u6570\u7684\u81ea\u52a8\u6267\u884c\u5668\nfunction run(fn) {\n let gen = fn();\n \n function next(err, data) {\n let result = gen.next(data);\n if (result.done) return 1;\n result.value(next);\n }\n \n next();\n \n}\n \nrun(gen);\n```\n\n#### 3.\u57fa\u4e8e async \u51fd\u6570 \u548c await \u7684\u5f02\u6b65\u5904\u7406\u65b9\u5f0f\n\n```js\nconst fs = require('fs')\n \n// Promise \u7248\u7684readFile\nconst readFile = function (fileName) {\n return new Promise(function(resolve, reject) {\n fs.readFile(fileName, function(err, data){\n if (err) return reject(err);\n resolve(data);\n })\n })\n}\n \nconst asyncReadFile = async function () {\n const f1 = await readFile('a.txt');\n const f2 = await readFile('b.txt');\n console.log(f1.toString());\n console.log(f2.toString());\n};\n \nasyncReadFile();\n```", "answer": "", "biz_type": 1, "qid": 303, "subject": "", "title": "\u5728 ES6 \u4e2d\u6709\u54ea\u4e9b\u89e3\u51b3\u5f02\u6b65\u7684\u65b9\u6cd5"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u81ea\u5982", "\u817e\u8baf\u5e94\u7528\u5b9d", "\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 9, "qid": 240, "qtype": "short_answer", "short_answer": {"analysis": "\n### bind \n\n#### 1.1 \u6982\u5ff5\n\nbind() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u5728 bind() \u88ab\u8c03\u7528\u65f6\uff0c\u8fd9\u4e2a\u65b0\u51fd\u6570\u7684 this \u88ab\u6307\u5b9a\u4e3a bind() \u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u800c\u5176\u4f59\u53c2\u6570\u5c06\u4f5c\u4e3a\u65b0\u51fd\u6570\u7684\u53c2\u6570\uff0c\u4f9b\u8c03\u7528\u65f6\u4f7f\u7528\u3002\n\n\u901a\u4fd7\u4e00\u70b9\uff0cbind\u4e0eapply/call\u4e00\u6837\u90fd\u80fd\u6539\u53d8\u51fd\u6570this\u6307\u5411\uff0c\u4f46bind\u5e76\u4e0d\u4f1a\u7acb\u5373\u6267\u884c\u51fd\u6570\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u7ed1\u5b9a\u4e86this\u7684\u65b0\u51fd\u6570\uff0c\u4f60\u9700\u8981\u518d\u6b21\u8c03\u7528\u6b64\u51fd\u6570\u624d\u80fd\u8fbe\u5230\u6700\u7ec8\u6267\u884c\u3002\n\n#### 1.2 \u7279\u70b9\n\n- \u53ef\u4ee5\u4fee\u6539\u51fd\u6570this\u6307\u5411\u3002\n- bind\u8fd4\u56de\u4e00\u4e2a\u7ed1\u5b9a\u4e86this\u7684\u65b0\u51fd\u6570\u00b7\n- \u652f\u6301\u51fd\u6570\u67ef\u91cc\u5316\n- \u65b0\u51fd\u6570\u7684this\u65e0\u6cd5\u518d\u88ab\u4fee\u6539\uff0c\u4f7f\u7528call\u3001apply\u4e5f\u4e0d\u884c\u3002\n\n#### 1.3 \u6a21\u62df\u5b9e\u73b0\n\n- \u4e0d\u51c6\u4f7f\u7528call\u3001apply\u3001bind \u53ef\u4ee5\u81ea\u5df1\u6a21\u62df\u4e00\u4e2aapply\n\n```js\nFunction.prototype.myapply = function (context, ...argus) {\n if (typeof this !== 'function') {\n throw new TypeError('not funciton')\n }\n const fn = this\n let result = null\n\n context = context || window\n argus = argus && argus[0] || []\n context.fn = fn\n result = context.fn(...argus)\n delete context.fn\n\n return result\n}\nFunction.prototype.mybind = function (context) {\n var me = this;\n var args = Array.prototype.slice.myapply(arguments, [1]);\n var F = function () {};\n F.prototype = this.prototype;\n var bound = function () {\n var innerArgs = Array.prototype.slice.mycall(arguments);\n var finalArgs = args.concat(innerArgs);\n return me.myapply(this instanceof F ? this : context || this, finalArgs);\n }\n bound.prototype = new F();\n return bound;\n}\n```", "answer": "", "biz_type": 1, "qid": 240, "subject": "", "title": "\u5b9e\u73b0 bind \u65b9\u6cd5\uff0c\u4e0d\u80fd\u4f7f\u7528 call\u3001apply\u3001bind"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 10, "qid": 214, "qtype": "short_answer", "short_answer": {"analysis": "\n\n### \u4ee3\u7801\u5b9e\u73b0\n\n- cache \u6784\u5efaMap\uff0c\u7528\u4f5c\u7f13\u5b58\u6570\u636e;\u628aURL\u4f5c\u4e3akey,\u7528\u4e8e\u5224\u65ad\u662f\u5426\u540c\u4e00\u4e2a\u8bf7\u6c42,\n- \u8bf7\u6c42\u7684\u66f4\u591a\u53c2\u6570\u53ef\u4f20\u5165option\uff0c\u5982\uff1aGET,data\u7b49\n- \u6bcf\u6b21\u8bf7\u6c42\u68c0\u67e5\u7f13\u5b58\uff0c\u6709\u5219\u8fd4\u56de\u7f13\u5b58\u6570\u636e\uff0c\u65e0\u5219\u53d1\u8d77\u8bf7\u6c42\n- \u8bf7\u6c42\u6210\u529f\u540e\uff0c\u4fdd\u5b58\u6570\u636e\u5230cache\u5e76\u8fd4\u56de\uff0c\u5931\u8d25\u5219\u5f39\u51fa\u63d0\u793a\n- \u7279\u6b8a\u60c5\u51b5\uff0c\u5982\u679c\u8bf7\u6c42\u5728pending\u72b6\u6001\uff0c\u5219\u8fd4\u56de\u8be5\u8bf7\u6c42\u7ee7\u7eed\u7b49\u5f85\n- \u4ee3\u7801\u4e2dajax\u8bf7\u6c42\u7528setTimeout()\u51fd\u6570\u4ee3\u66ff\uff0c\u53ef\u81ea\u884c\u5c01\u88c5request- \u51fd\u6570\u6216\u7528axios\u4ee3\u66ff\n\n```js\nconst request = (url,option)=>new Promise((res)=>{\n setTimeout(()=>{\n res({data:option})\n },2000)\n})\nconst cache = new Map();\nconst cacheRequest = (url,option) => {\n let key = `${url}:${option.method}`;\n if (cache.has(key)) {\n if(cache.get(key).status === 'pending'){\n return cache.get(key).myWait;\n }\n return Promise.resolve(cache.get(key).data)\n } else {\n // \u65e0\u7f13\u5b58\uff0c\u53d1\u8d77\u771f\u5b9e\u8bf7\u6c42\n let requestApi = request(url,option);\n cache.set(key, {status: 'pending',myWait: requestApi})\n return requestApi.then(res => {\n // console.log(cache)\n cache.set(key, {status: 'success',data:res})\n // console.log(cache)\n return Promise.resolve(res)\n }).catch(err => {\n cache.set(key, {status: 'fail',data:err})\n Promise.reject(err)\n })\n }\n}\n```\n\n\u8c03\u7528\n\n```js\ncacheRequest('url1')\n.then(res => console.log(res))\ncacheRequest('url1')\n.then(res => console.log(res))\n\nsetTimeout(()=>{\n cacheRequest('url1')\n .then(res => console.log(res))\n},4000)\n```", "answer": "", "biz_type": 1, "qid": 214, "subject": "", "title": "\u8bf7\u5b9e\u73b0\u4e00\u4e2a cacheRequest \u65b9\u6cd5\uff0c\u4fdd\u8bc1\u53d1\u51fa\u591a\u6b21\u540c\u4e00\u4e2a ajax \u8bf7\u6c42\u65f6\u90fd\u80fd\u62ff\u5230\u6570\u636e\uff0c\u800c\u5b9e\u9645\u4e0a\u53ea\u53d1\u51fa\u4e00\u6b21\u8bf7\u6c42"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fae\u8f6f"], "date": "Fri, 19 Jun 2020 16:20:41 GMT", "favorite_num": 8, "qid": 164, "qtype": "short_answer", "short_answer": {"analysis": "\n\n### \u4ee3\u7801\u5b9e\u73b0\n\n#### 1.\u57fa\u672c\u65b9\u6cd5\n\n 1. \u4ece\u4e2d\u6587\u6570\u5b57\u4e2d\u9010\u4e2a\u8bc6\u522b\u51fa\u6570\u5b57\u548c\u6743\u4f4d\u7684\u7ec4\u5408\n 2. \u7136\u540e\u6839\u636e\u6743\u4f4d\u548c\u6570\u5b57\u500d\u6570\u5bf9\u5e94\u5173\u7cfb \u8ba1\u7b97\u51fa\u6bcf\u4e2a\u6570\u5b57\u548c\u6743\u4f4d\u7ec4\u5408\u7684\u503c\n 3. \u6700\u540e\u6c42\u548c\u5f97\u51fa\u7ed3\u679c\n\n #### 2.\u7b97\u6cd5\u5b9e\u73b0\n\n1. \u5c06\u4e2d\u6587\u6570\u5b57\u8f6c\u6362\u6210\u963f\u62c9\u4f2f\u6570\u5b57\n2. \u5c06\u4e2d\u6587\u6743\u4f4d\u8f6c\u6362\u621010\u7684\u500d\u6570\n\n#### 3.\u4ee3\u7801\u5b9e\u73b0\u4e00\n\n```js\nfunction ChineseToNumber(chnString) {\n const CHN_CHAR_LENGTH = 1;\n\n const chnNumChar = [\"\u96f6\", \"\u4e00\", \"\u4e8c\", \"\u4e09\", \"\u56db\", \"\u4e94\", \"\u516d\", \"\u4e03\", \"\u516b\", \"\u4e5d\"];\n const chnUnitChar = [\"\", \"\u5341\", \"\u767e\", \"\u5343\"]\n const chnUnitSection = [\"\", \"\u4e07\", \"\u4ebf\", \"\u4e07\u4ebf\"]\n const chnValuePair = [\n [\"\u5341\", 10, false], // [0] name\u4e2d\u6587\u6743\u4f4d\u540d [1] 10\u7684\u500d\u6570\u503c value [2]secUnit \u662f\u5426\u662f\u8282\u6743\u4f4d\n [\"\u767e\", 100, false],\n [\"\u5343\", 1000, false],\n [\"\u4e07\", 10000, true],\n [\"\u4ebf\", 100000000, true],\n ]\n // \u4e3b\u8981\u7b97\u6cd5\n let rtn = 0;\n let section = 0;\n let number = 0;\n let secUnit = false;\n let pos = 0;\n // \u4e2d\u6587\u6570\u5b57\u8f6c\u6362\u6570\u5b57\uff0c\u5982\u679c\u8fd4\u56de-1\uff0c\u8868\u793a\u8fd9\u662f\u4e00\u4e2a\u6743\u4f4d\u5b57\u7b26\u3002\n function ChineseToValue(chnStr) {\n for (let val = 0; val < chnNumChar.length; val++) {\n if (chnStr == chnNumChar[val]) {\n return val;\n }\n }\n return -1;\n }\n // chnValuePair \u8868\u5f97\u5230\u6743\u4f4d\u5bf9\u5e94\u768410\u7684\u500d\u6570\u3002\n function ChineseToUnit(chnStr) {\n // console.log(chnStr)\n for (let unit = 0; unit < chnValuePair.length; unit++) {\n if (chnStr == chnValuePair[unit][0]) {\n secUnit = chnValuePair[unit][2];\n // console.log(secUnit)\n return chnValuePair[unit][1];\n }\n }\n\n return 1;\n }\n\n while (pos < chnString.length) {\n let num = ChineseToValue(chnString.substr(pos, CHN_CHAR_LENGTH));\n // console.log(\"num:\",num)\n if (num >= 0) // \u6570\u5b57\u8fd8\u662f\u5355\u4f4d\n {\n number = num;\n pos += CHN_CHAR_LENGTH;\n if (pos >= chnString.length) //\u5982\u679c\u662f\u6700\u540e\u4e00\u4f4d\u6570\u5b57 \u76f4\u63a5\u7ed3\u675f\n {\n section += number;\n rtn += section;\n break;\n }\n } else {\n let unit = ChineseToUnit(chnString.substr(pos, CHN_CHAR_LENGTH));\n // console.log(\"unit\",unit,secUnit)\n if (secUnit) //\u662f\u8282\u6743\u4f4d\u8bf4\u660e\u4e00\u4e2a\u8282\u5df2\u7ecf\u7ed3\u675f\n {\n section = (section + number) * unit;\n rtn += section;\n section = 0;\n } else {\n section += (number * unit);\n }\n number = 0;\n pos += CHN_CHAR_LENGTH;\n if (pos >= chnString.length) {\n rtn += section;\n break;\n }\n }\n }\n\n return rtn;\n}\n\nconst testPair = [\n [ 0,\"\u96f6\" ],\n [ 1,\"\u4e00\" ],\n [ 2,\"\u4e8c\" ],\n [ 3,\"\u4e09\" ],\n [ 4,\"\u56db\" ],\n [ 5,\"\u4e94\" ],\n [ 6,\"\u516d\" ],\n [ 7,\"\u4e03\" ],\n [ 8,\"\u516b\" ],\n [ 9,\"\u4e5d\" ],\n [ 10,\"\u4e00\u5341\" ],\n [ 11,\"\u4e00\u5341\u4e00\" ],\n [ 110,\"\u4e00\u767e\u4e00\u5341\" ],\n [ 111,\"\u4e00\u767e\u4e00\u5341\u4e00\" ],\n [ 100,\"\u4e00\u767e\" ],\n [ 102,\"\u4e00\u767e\u96f6\u4e8c\" ],\n [ 1020,\"\u4e00\u5343\u96f6\u4e8c\u5341\" ],\n [ 1001,\"\u4e00\u5343\u96f6\u4e00\" ],\n [ 1015,\"\u4e00\u5343\u96f6\u4e00\u5341\u4e94\" ],\n [ 1000,\"\u4e00\u5343\" ],\n [ 10000,\"\u4e00\u4e07\" ],\n [ 20010,\"\u4e8c\u4e07\u96f6\u4e00\u5341\" ],\n [ 20001,\"\u4e8c\u4e07\u96f6\u4e00\" ],\n [ 100000,\"\u4e00\u5341\u4e07\" ],\n [ 1000000,\"\u4e00\u767e\u4e07\" ],\n [ 10000000,\"\u4e00\u5343\u4e07\" ],\n [ 100000000,\"\u4e00\u4ebf\" ],\n [ 1000000000,\"\u4e00\u5341\u4ebf\" ],\n [ 1000001000,\"\u4e00\u5341\u4ebf\u96f6\u4e00\u5343\" ],\n [ 1000000100,\"\u4e00\u5341\u4ebf\u96f6\u4e00\u767e\" ],\n [ 200010,\"\u4e8c\u5341\u4e07\u96f6\u4e00\u5341\" ],\n [ 2000105,\"\u4e8c\u767e\u4e07\u96f6\u4e00\u767e\u96f6\u4e94\" ],\n [ 20001007,\"\u4e8c\u5343\u4e07\u4e00\u5343\u96f6\u4e03\" ],\n [ 2000100190,\"\u4e8c\u5341\u4ebf\u96f6\u4e00\u5341\u4e07\u96f6\u4e00\u767e\u4e5d\u5341\" ],\n [ 1040010000,\"\u4e00\u5341\u4ebf\u56db\u5343\u96f6\u4e00\u4e07\" ],\n [ 200012301,\"\u4e8c\u4ebf\u96f6\u4e00\u4e07\u4e8c\u5343\u4e09\u767e\u96f6\u4e00\" ],\n [ 2005010010,\"\u4e8c\u5341\u4ebf\u96f6\u4e94\u767e\u96f6\u4e00\u4e07\u96f6\u4e00\u5341\" ],\n [ 4009060200,\"\u56db\u5341\u4ebf\u96f6\u4e5d\u767e\u96f6\u516d\u4e07\u96f6\u4e8c\u767e\" ],\n [ 4294967295,\"\u56db\u5341\u4e8c\u4ebf\u4e5d\u5343\u56db\u767e\u4e5d\u5341\u516d\u4e07\u4e03\u5343\u4e8c\u767e\u4e5d\u5341\u4e94\" ]\n\n]\n\n// \u6d4b\u8bd5\u7528\u4f8b\nfunction testChineseToNumber()\n{\n for(let i = 0; i < testPair.length; i++)\n {\n let num = ChineseToNumber(testPair[i][1]);\n console.log(num == testPair[i][0]);\n }\n}\n\ntestChineseToNumber();\n// \u63a7\u5236\u53f0\u6253\u5370\u7ed3\u679c \uff1a39 true\n```\n\n#### 4.\u4ee3\u7801\u5b9e\u73b0\u4e8c\n\n```js\nfunction transform(str) {\n const numChar = {\n '\u96f6':0,\n '\u4e00':1,\n '\u4e8c':2,\n '\u4e09':3,\n '\u56db':4,\n '\u4e94':5,\n '\u516d':6,\n '\u4e03':7,\n '\u516b':8,\n '\u4e5d':9\n };\n const levelChar = {\n '\u5341':10,\n '\u767e':100,\n '\u5343':1000,\n '\u4e07':10000,\n '\u4ebf':100000000,\n };\n let ary = Array.from(str)\n let temp = 0\n let sum = 0\n for(let i = 0; i < ary.length; i++) {\n let char = ary[i]\n if(char === '\u96f6') continue\n if (char === '\u4ebf' || char === '\u4e07') {\n sum += temp * levelChar[char]\n temp = 0\n } else {\n let next = ary[i + 1]\n if(next && next !== '\u4ebf' && next !== '\u4e07') {\n temp += numChar[char] * levelChar[next]\n i++\n } else {\n temp += numChar[char]\n }\n }\n }\n return sum + temp\n}\n\nconsole.log(transform('\u4e00\u5341\u4e8c\u4ebf\u4e09\u5343\u96f6\u4e5d\u5341\u516d\u4e07\u4e09\u5343\u516b\u767e\u4e5d\u5341\u4e03'))\n```", "answer": "", "biz_type": 1, "qid": 164, "subject": "", "title": "\u5b9e\u73b0\u4e00\u4e2a\u51fd\u6570\u5c06\u4e2d\u6587\u6570\u5b57\u8f6c\u6210\u6570\u5b57"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u8fb9\u950b"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 5, "qid": 107, "qtype": "short_answer", "short_answer": {"analysis": "\u53ef\u4ee5\u7406\u89e3\u4e3a\u5728\u505a\u4e00\u4ef6\u4e8b\u65f6\u662f\uff1a\u201c\u8be5\u8ba9\u8c01\u6765\u505a\u201d\u3002\u90a3\u4e2a\u8c01\u5c31\u662f\u5bf9\u8c61\uff0c\u4ed6\u8981\u600e\u4e48\u505a\u662f\u4ed6\u81ea\u5df1\u7684\u4e8b\uff0c\u6700\u540e\u5c31\u662f\u4e00\u7fa4\u5bf9\u8c61\u5408\u529b\u628a\u4e8b\u60c5\u505a\u597d\u3002\u76f8\u6bd4\u8f83\u4e8e\u9762\u5411\u8fc7\u7a0b\u7684\u201c\u6b65\u9aa4\u5316\u201d\u5206\u6790\u95ee\u9898\uff0c\u9762\u5411\u5bf9\u8c61\u5219\u662f\u201c\u529f\u80fd\u5316\u201d\u5206\u6790\u95ee\u9898\uff0c\u5176\u4f18\u70b9\u4f53\u73b0\u5728\uff1a\n\n- \u5c06\u6570\u636e\u548c\u65b9\u6cd5\u5c01\u88c5\u5728\u4e00\u8d77\uff0c\u4ee5\u63a5\u53e3\u7684\u65b9\u5f0f\u63d0\u4f9b\u7ed9\u8c03\u7528\u8005\uff0c\u8c03\u7528\u8005\u65e0\u9700\u5173\u6ce8\u95ee\u9898\u7684\u89e3\u51b3\u8fc7\u7a0b\uff1b\n- \u5bf9\u8c61\u4e4b\u95f4\u901a\u8fc7\u7ee7\u627f\uff0c\u51cf\u5c11\u4ee3\u7801\u7684\u5197\u4f59\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u590d\u7528\u6027\uff1b\n- \u901a\u8fc7\u91cd\u8f7d/\u91cd\u5199\u65b9\u6cd5\u6765\u62d3\u5c55\u5bf9\u8c61\u7684\u529f\u80fd\uff1b\n\n\u4ee5\u4e0a\u7684\u4f18\u70b9\u6765\u6e90\u4e8e\u9762\u5411\u5bf9\u8c61\u7684\u4e09\u5927\u7279\u5f81\uff1a\u5c01\u88c5\u3001\u7ee7\u627f\u548c\u591a\u6001\u3002", "answer": "", "biz_type": 1, "qid": 107, "subject": "", "title": "\u8bf4\u4e00\u4e0b\u5bf9\u9762\u5411\u5bf9\u8c61\u7684\u7406\u89e3\uff0c\u9762\u5411\u5bf9\u8c61\u6709\u4ec0\u4e48\u597d\u5904"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 4, "qid": 75, "qtype": "short_answer", "short_answer": {"analysis": "**\u5927\u81f4\u601d\u8def**\n\n\u98de\u673a\u6309\u7167\u56fe\u5f62\u4e2d\u5fc3\u5c55\u5f00\u4e00\u4e2a\u5750\u6807\u5e8f\u5217\u5bf9\u5e94\u5c0f\u65b9\u5757\uff0c\u7136\u540e\u68c0\u6d4b\u7684\u662f\u5bf9\u5e94\u5b50\u5f39\u5728\u5c0f\u65b9\u5757\u7684\u8986\u76d6\uff0c\u5c0f\u65b9\u5757\u6309\u7167\u4e4b\u524d\u8bf4\u7684\u5212\u5206\u533a\u57df\u53ef\u4ee5\u662f`1*1 2*2`\u7684,\u5177\u4f53\u770b\u60f3\u8981\u7684\u7cbe\u5ea6\u662f\u591a\u5c11", "answer": "", "biz_type": 1, "qid": 75, "subject": "", "title": "1000*1000 \u7684\u753b\u5e03\uff0c\u4e0a\u9762\u6709\u98de\u673a\u3001\u5b50\u5f39\uff0c\u5982\u4f55\u5212\u5206\u533a\u57df\u80fd\u591f\u66f4\u6709\u6548\u7684\u505a\u78b0\u649e\u68c0\u6d4b\uff0c\u7c7b\u4f3c\u5212\u5206\u533a\u57df\u5927\u5c0f\u4e0e\u78b0\u649e\u68c0\u6d4b\u6548\u7387\u7684\u7b97\u6cd5\uff0c\u8bf4\u4e00\u4e0b\u5927\u81f4\u7684\u601d\u8def"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 8, "qid": 94, "qtype": "short_answer", "short_answer": {"analysis": "**1\uff09\u601d\u8def**\n\n1 fetch \u8bf7\u6c42\u4ecb\u7ecd\n2 \u5e76\u53d1\uff0c\u5e76\u53d1\uff0c\u67d0\u4e2a\u5b8c\u6210\u540e\u53ef\u4ee5\u7ee7\u7eed\u53d1\u8bf7\u6c42\n3 \u6240\u6709\u8bf7\u6c42\u7ed3\u675f\u540e callback\n4 \u6ce8\u610f\u5bb9\u9519\uff08\u2460\u53c2\u6570\u9519\u8bef\uff0c\u53c2\u6570\u4e0d\u6b63\u786e\u3001\u63a5\u53e3\u5730\u5740\u4e0d\u6b63\u786e\u3001\u6700\u5927\u6570\u4e0d\u6b63\u786e\u3001\u56de\u8c03\u51fd\u6570\u4e0d\u6b63\u786e\uff1b\u2461\u63a5\u53e3\u9519\u8bef\uff09\n5 \u8fb9\u754c\u503c\n6 \u4e0d\u80fd\u5728 while \u4e2d\u4f7f\u7528 fetch\uff0c\u56e0\u4e3a while \u662f\u540c\u6b65\uff0c\u5b83\u6c38\u8fdc\u4e0d\u4f1a\u7b49\u5f85\u5f02\u6b65\u7684 fetch \u7ed3\u679c\u56de\u6765\n7 \u4f7f\u7528 for \u5faa\u73af + \u9012\u5f52\u7684\u65b9\u6cd5\n\n**2\uff09\u4ee3\u7801\u5b9e\u73b0**\n\n```js\n/**\n *\n * @param { Array } urls \u8bf7\u6c42\u5730\u5740\u6570\u7ec4\n * @param { Number } max \u6700\u5927\u5e76\u53d1\u8bf7\u6c42\u6570\n * @param { Function } callback \u56de\u8c03\u5730\u5740\n */\nfunction parallelFetch(urls, max, callback) {\n // \u5982\u679c\u5f53\u524d\u73af\u5883\u4e0d\u652f\u6301 fetch , \u5219\u63d0\u793a\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u5e38\u8fd0\u884c\n if (!window.fetch || \"function\" !== typeof window.fetch) {\n throw Error(\"\u5f53\u524d\u73af\u5883\u4e0d\u652f\u6301 fetch \u8bf7\u6c42\uff0c\u7a0b\u5e8f\u7ec8\u6b62\");\n }\n\n // \u5982\u679c\u53c2\u6570\u6709\u8bef\uff0c\u5219\u63d0\u793a\u8f93\u5165\u6b63\u786e\u7684\u53c2\u6570\n if (!urls || urls.length <= 0) {\n throw Error(\"urls is empty: \u8bf7\u4f20\u5165\u6b63\u786e\u7684\u8bf7\u6c42\u5730\u5740\");\n }\n\n const _urlsLength = urls.length; // \u8bf7\u6c42\u5730\u5740\u6570\u7ec4\u7684\u957f\u5ea6\n const _max = max || 1; // \u4fdd\u8bc1\u6700\u5927\u5e76\u53d1\u503c\u7684\u6709\u6548\u6027\n let _currentIndex = 0; // \u5f53\u524d\u8bf7\u6c42\u5730\u5740\u7684\u7d22\u5f15\n let _maxFetch = max <= _urlsLength ? max : _urlsLength; // \u5f53\u524d\u53ef\u4ee5\u6b63\u5e38\u8bf7\u6c42\u7684\u6570\u91cf\uff0c\u4fdd\u8bc1\u6700\u5927\u5e76\u53d1\u6570\u7684\u5b89\u5168\u6027\n let _finishedFetch = 0; // \u5f53\u524d\u5b8c\u6210\u8bf7\u6c42\u7684\u6570\u91cf\uff0c\u7528\u4e8e\u5224\u65ad\u4f55\u65f6\u8c03\u7528\u56de\u8c03\n \n console.log(`\u5f00\u59cb\u5e76\u53d1\u8bf7\u6c42\uff0c\u63a5\u53e3\u603b\u6570\u4e3a ${_urlsLength} \uff0c\u6700\u5927\u5e76\u53d1\u6570\u4e3a ${_maxFetch}`);\n // \u6839\u636e\u6700\u5927\u5e76\u53d1\u6570\u8fdb\u884c\u5faa\u73af\u53d1\u9001\uff0c\u4e4b\u540e\u901a\u8fc7\u72b6\u6001\u505a\u9012\u5f52\u8bf7\u6c42\n for (let i = 0; i < _maxFetch; i++) {\n fetchFunc();\n }\n // \u8bf7\u6c42\u65b9\u6cd5\n function fetchFunc() {\n // \u5982\u679c\u6240\u6709\u8bf7\u6c42\u6570\u90fd\u5b8c\u6210\uff0c\u5219\u6267\u884c\u56de\u8c03\u65b9\u6cd5\n if (_finishedFetch === _urlsLength) {\n console.log(`\u5f53\u524d\u4e00\u5171 ${_urlsLength} \u4e2a\u8bf7\u6c42\uff0c\u5df2\u5b8c\u6210 ${_finishedFetch} \u4e2a`)\n if (\"function\" === typeof callback) return callback();\n return false;\n }\n // \u5982\u679c\u5f53\u524d\u8bf7\u6c42\u7684\u7d22\u5f15\u5927\u4e8e\u7b49\u4e8e\u8bf7\u6c42\u5730\u5740\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u5219\u4e0d\u7ee7\u7eed\u8bf7\u6c42\n if (_currentIndex >= _urlsLength) {\n _maxFetch = 0;\n }\n\n //\u5982\u679c\u53ef\u8bf7\u6c42\u7684\u6570\u91cf\u5927\u4e8e0\uff0c\u8868\u793a\u53ef\u4ee5\u7ee7\u7eed\u53d1\u8d77\u8bf7\u6c42\n if (_maxFetch > 0) {\n console.log( `\u5f53\u524d\u6b63\u53d1\u8d77\u7b2c ${_currentIndex + 1 } \u6b21\u8bf7\u6c42\uff0c\u5f53\u524d\u4e00\u5171 ${_urlsLength} \u4e2a\u8bf7\u6c42\uff0c\u5df2\u5b8c\u6210 ${_finishedFetch} \u4e2a\uff0c\u8bf7\u6c42\u5730\u5740\u4e3a\uff1a${urls[_currentIndex]}`);\n // \u53d1\u8d77 fetch \u8bf7\u6c42\n fetch(urls[_currentIndex])\n .then((res) => {\n // TODO \u4e1a\u52a1\u903b\u8f91\uff0c\u6b63\u5e38\u7684\u903b\u8f91\uff0c\u5f02\u5e38\u7684\u903b\u8f91\n // \u5f53\u524d\u8bf7\u6c42\u7ed3\u675f\uff0c\u6b63\u5e38\u8bf7\u6c42\u7684\u6570\u91cf +1\n _maxFetch += 1;\n _finishedFetch += 1;\n fetchFunc();\n })\n .catch((err) => {\n // TODO \u5f02\u5e38\u5904\u7406\uff0c\u5904\u7406\u5f02\u5e38\u903b\u8f91\n // \u5f53\u524d\u8bf7\u6c42\u7ed3\u675f\uff0c\u6b63\u5e38\u8bf7\u6c42\u7684\u6570\u91cf +1\n _maxFetch += 1;\n _finishedFetch += 1;\n fetchFunc();\n });\n // \u6bcf\u6b21\u8bf7\u6c42\uff0c\u5f53\u524d\u8bf7\u6c42\u5730\u5740\u7684\u7d22\u5f15 +1\n _currentIndex += 1;\n // \u6bcf\u6b21\u8bf7\u6c42\uff0c\u53ef\u4ee5\u6b63\u5e38\u8bf7\u6c42\u7684\u6570\u91cf -1\n _maxFetch -= 1;\n }\n }\n}\n\nlet urls = [];\nfor (let i = 0; i < 100; i++) {\n urls.push(`https://jsonplaceholder.typicode.com/todos/${i}`);\n}\nconst max = 10;\nconst callback = () => {\n console.log(\"\u6211\u8bf7\u6c42\u5b8c\u4e86\");\n};\n\nparallelFetch(urls, max, callback);\n```", "answer": "", "biz_type": 1, "qid": 94, "subject": "```js\n/*\n\t\u53ef\u4ee5\u6279\u91cf\u8bf7\u6c42\u6570\u636e\uff0c\u6240\u6709\u7684 URL \u5730\u5740\u5728 urls \u53c2\u6570\u4e2d\uff0c\n \u540c\u65f6\u53ef\u4ee5\u901a\u8fc7 max \u53c2\u6570\u63a7\u5236\u8bf7\u6c42\u7684\u5e76\u53d1\u5ea6\uff0c\u5f53\u6240\u6709\u8bf7\n \u6c42\u7ed3\u675f\u4e4b\u540e\uff0c\u9700\u8981\u6267\u884c callback \u56de\u8c03\u51fd\u6570\u3002\u53d1\u8bf7\u6c42\u7684\n \u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 fetch \u5373\u53ef\n*/\n```\n", "title": "\u8bf7\u5b9e\u73b0\u5982\u4e0b\u7684\u51fd\u6570"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 7, "qid": 68, "qtype": "short_answer", "short_answer": {"analysis": "#### \u5224\u65ad\u6570\u7ec4\u65b9\u5f0f\n\n- `[] instanceof Array`\n- `Object.prototype.toString.call([]) === '[object Array]'`\n- `Array.prototype.isPrototypeOf([])`\n- `[].constructor === Array`\n- `Array.isArray([])`\n\n#### \u5982\u4f55\u5904\u7406\u7c7b\u6570\u7ec4\u5bf9\u8c61\n\n**1\uff09JavaScript \u7c7b\u6570\u7ec4\u5bf9\u8c61\u7684\u5b9a\u4e49**\n\n- \u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20\uff0c\u5e76\u4e14\u62e5\u6709 length \u5c5e\u6027\uff1b\n- \u6ca1\u6709\u6570\u7ec4\u7684\u5176\u4ed6\u65b9\u6cd5\uff0c\u4f8b\u5982 `push` \uff0c `forEach` \uff0c `indexOf` \u7b49\u3002\n\n```js\nvar foo = {\n 0: 'JS',\n 1: 'Node',\n 2: 'TS',\n length: 3\n}\n```\n\n**2\uff09\u8f6c\u6362\u65b9\u5f0f**\n\n```js\n// \u65b9\u5f0f\u4e00\nArray.prototype.slice.call(arguments);\nArray.prototype.slice.apply(arguments)\n[].slice.call(arguments)\n\n// \u65b9\u5f0f\u4e8c\nArray.from(arguments);\n\n// \u65b9\u5f0f\u4e09\n// \u8fd9\u79cd\u65b9\u5f0f\u8981\u6c42 \u6570\u636e\u7ed3\u6784 \u5fc5\u987b\u6709 \u904d\u5386\u5668\u63a5\u53e3\n[...arguments] \n\n// \u65b9\u5f0f\u56db\n[].concat.apply([],arguments)\n\n// \u65b9\u5f0f\u4e94\uff1a\u624b\u52a8\u5b9e\u73b0\nfunction toArray(s){\n var arr = []; \n for(var i = 0,len = s.length; i < len; i++){ \n arr[i] = s[i]; \n } \n return arr; \n}\n```\n\n**3\uff09\u8f6c\u6362\u540e\u6ce8\u610f\u51e0\u70b9**\n\n- \u6570\u7ec4\u957f\u5ea6\u7531\u7c7b\u6570\u7ec4\u7684length\u5c5e\u6027\u51b3\u5b9a\n- \u7d22\u5f15\u4e0d\u8fde\u7eed\uff0c\u4f1a\u81ea\u52a8\u8865\u4f4dundefined\n- \u4ec5\u8003\u86510\u548c\u6b63\u6574\u6570\u7d22\u5f15\uff1b\n- slice\u4f1a\u4ea7\u751f\u7a00\u758f\u6570\u7ec4\uff0c\u5185\u5bb9\u662fempty\u800c\u4e0d\u662fundefined\n- \u7c7b\u6570\u7ec4push\u6ce8\u610f\uff0cpush\u64cd\u4f5c\u7684\u662f\u7d22\u5f15\u503c\u4e3alength\u7684\u4f4d\u7f6e\n\n\n", "answer": "", "biz_type": 1, "qid": 68, "subject": "", "title": "\u600e\u6837\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u6570\u7ec4\uff0c\u5982\u4f55\u5904\u7406\u7c7b\u6570\u7ec4\u5bf9\u8c61"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 4, "qid": 110, "qtype": "short_answer", "short_answer": {"analysis": "#### \u4ee3\u7801\u5b9e\u73b0\n\n\u5927\u4f53\u601d\u8def\n\n- \u751f\u6210\u968f\u673a\u6570\n- \u4ece\u968f\u673a\u6570\u4e2d\u5f97\u523016\u8fdb\u5236\u7684\u989c\u8272\u503c\uff0c\u989c\u8272\u503c\u957f\u5ea6\u662f6\u4f4d\u6216\u80053\u4f4d\uff08\u8981\u4e48\u53d63\u4f4d\u8981\u4e48\u53d66\u4f4d\uff09\n\n**1\uff09\u5b9e\u73b0\u65b9\u5f0f\u4e00**\n\n- for\u5faa\u73af\u904d\u5386\n\n```js\nfunction getColor(){\n var colorElements = \"0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f\";\n var colorArray = colorElements.split(\",\");\n var color =\"#\";\n for(var i =0;i<6;i++){\n color+=colorArray[Math.floor(Math.random()*16)];\n }\n return color;\n}\n```\n\n**2\uff09\u5b9e\u73b0\u65b9\u5f0f\u4e8c**\n\n- while\u5faa\u73af\n\n```js\nconst getColor = () => {\n var hexNums = [\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"a\", \"b\", \"c\", \"d\", \"e\", \"f\"];\n let i = 0;\n let color = '#';\n while (i < 6) {\n color += hexNums[parseInt(Math.random() * 16)];\n i++;\n }\n return color\n}\n```\n\n**3\uff09\u5b9e\u73b0\u65b9\u5f0f\u4e09**\n\n `padEnd()` \u65b9\u6cd5\u4f1a\u7528\u4e00\u4e2a\u5b57\u7b26\u4e32\u586b\u5145\u5f53\u524d\u5b57\u7b26\u4e32\uff08\u5982\u679c\u9700\u8981\u7684\u8bdd\u5219\u91cd\u590d\u586b\u5145\uff09\uff0c\u8fd4\u56de\u586b\u5145\u540e\u8fbe\u5230\u6307\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\u3002\u4ece\u5f53\u524d\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\uff08\u53f3\u4fa7\uff09\u5f00\u59cb\u586b\u5145\u3002\n\n```js\nfunction randomColor(){\n\treturn '#'+Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, '0');\n}\n```\n\n**4\uff09\u5b9e\u73b0\u65b9\u5f0f\u56db**\n\n- substr\u65b9\u6cd5\n\n```js\nconst randomColor = () => color = '#' + Math.random().toString(16).substr(-6);\n```", "answer": "", "biz_type": 1, "qid": 110, "subject": "", "title": "\u5b9e\u73b0\u8f93\u51fa\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u7684\u968f\u673a\u989c\u8272(#af0128a)"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 4, "qid": 112, "qtype": "short_answer", "short_answer": {"analysis": "#### \u4ee3\u7801\u5b9e\u73b0\n\n- **\u8003\u70b9\uff1a** \u5bf9\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u4ee5\u53ca\u591a\u79cd\u5b9e\u73b0\u65b9\u6cd5\n- **\u601d\u8def\uff1a** \u5229\u7528String\u548cArray\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u6216\u6b63\u5219\u6765\u5206\u5272\u3001\u66ff\u6362\u3001\u91cd\u7ec4\u5b57\u7b26\u4e32\uff0c\u5229\u7528toUpperCase()\u6216charCodeAt()+fromCharCode()\u5b9e\u73b0\u5927\u5199\u8f6c\u6362\n\n**1\uff09\u5b9e\u73b0\u65b9\u5f0f\u4e00**\n\n```js\n// \u7b2c\u4e00\u79cd map\nfunction firstUp1(str) {\n return str.split(\"-\").map(function (key) {\n return key[0].toUpperCase() + key.slice(1);\n }).join(\"\");\n}\nconsole.log(firstUp1(string))\n```\n\n**2\uff09\u5b9e\u73b0\u65b9\u5f0f\u4e8c**\n\n```js\n// \u7b2c\u4e8c\u79cd \u6b63\u5219\nfunction firstUp2(str) {\n return str.replace(/-[a-z]/g, function (match) {\n return match.replace(\"-\", \"\").toUpperCase()\n })\n}\nconsole.log(firstUp2(string))\n```\n\n**3\uff09\u7efc\u5408\u7248**\n\n```js\n// \u8f6c\u5316\u6210\u5927\u5199\nfunction myToUpperCase() {\n // 1.String\u81ea\u5e26\u65b9\u6cd5\n return this.toUpperCase()\n\n // 2.toLocaleUpperCase\u4e00\u822c\u60c5\u51b5\u4e0b\u8ddftoUpperCase\u8fd4\u56de\u7684\u7ed3\u679c\u4e00\u6837\n // return this.toLocaleUpperCase()\n\n // 3.\u6839\u636eASCII\u4e4b\u95f4\u7684\u89c4\u5f8b\u7528charCodeAt()+fromCharCode()\u6765\u8f6c\u6362\u5927\u5c0f\u5199\n // if (!/[a-z]/.test(this)) return this\n // return String.fromCharCode(this.charCodeAt() - 32)\n}\n\nString.prototype.myToUpperCase = myToUpperCase\n\n// \u5206\u5272\u3001\u66ff\u6362\u3001\u91cd\u7ec4\u5b57\u7b26\u4e32\nfunction transform(str, separator) {\n // 1.\u5e38\u89c4String\u548cArray\u7684api\n return str\n .split(separator)\n .map(word => word.charAt(0).myToUpperCase() + word.slice(1))\n .join(\"\")\n\n // 2.\u5e38\u89c4String\u548cArray\u7684api\n // return str.split(separator).reduce((pre, word) => {\n // return pre + word.substr(0, 1).myToUpperCase() + word.substring(1)\n // }, \"\")\n\n // 3.\u4f7f\u7528\u6b63\u5219\n // return (separator + str)\n // .replace(new RegExp(separator+'(.)', \"g\"), (_,c) => c.myToUpperCase())\n}\nconsole.log(transform(\"kuai-shou-front-end\", \"-\"))\n```\n\n", "answer": "", "biz_type": 1, "qid": 112, "subject": "", "title": "\u624b\u5199\u4ee3\u7801\u5b9e\u73b0`kuai-shou-front-end=>KuaiShouFrontEnd`"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f34\u9c7c", "\u817e\u8baf\u5e94\u7528\u5b9d"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 6, "qid": 89, "qtype": "short_answer", "short_answer": {"analysis": "#### \u4ec0\u4e48\u662f\u6309\u9700\u52a0\u8f7d\n\n\u548c\u5f02\u6b65\u52a0\u8f7dscript\u7684\u76ee\u7684\u4e00\u6837(\u5f02\u6b65\u52a0\u8f7dscript\u7684\u65b9\u6cd5),\u6309\u9700\u52a0\u8f7d/\u4ee3\u7801\u5207\u5272\u4e5f\u53ef\u4ee5\u89e3\u51b3\u9996\u5c4f\u52a0\u8f7d\u7684\u901f\u5ea6\u3002\n\n**\u8bbe\u4e48\u65f6\u5019\u9700\u8981\u6309\u9700\u52a0\u8f7d**\n\n\u5982\u679c\u662f\u5927\u6587\u4ef6,\u4f7f\u7528\u6309\u9700\u52a0\u8f7d\u5c31\u5341\u5206\u5408\u9002\u3002\u6bd4\u5982\u4e00\u4e2a\u8fd11M\u7684\u5168\u56fd\u57ce\u5e02\u7701\u5e02\u53bf\u7684json\u6587\u4ef6,\u5728\u6211\u9996\u5c4f\u52a0\u8f7d\u7684\u65f6\u5019\u5e76\u4e0d\u9700\u8981\u5f15\u5165,\u800c\u662f\u5f53\u7528\u6237\u70b9\u51fb\u9009\u9879\u7684\u65f6\u5019\u624d\u52a0\u8f7d\u3002\u5982\u679c\u4e0d\u70b9\u51fb,\u5219\u4e0d\u4f1a\u52a0\u8f7d\u3002\u5c31\u53ef\u4ee5\u7f29\u77ed\u9996\u5c4fhttp\u8bf7\u6c42\u7684\u6570\u91cf\u4ee5\u53ca\u65f6\u95f4\u3002\n\n\u5982\u679c\u662f\u5c0f\u6587\u4ef6,\u53ef\u4ee5\u4e0d\u5fc5\u592a\u5728\u610f\u6309\u9700\u52a0\u8f7d\u3002\u8fc7\u591a\u7684http\u8bf7\u6c42\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002\n\n#### \u5b9e\u73b0\u6309\u9700\u52a0\u8f7d\u7684\u65b9\u6cd5\n\n**1\uff09ES2020 \u52a8\u6001\u5bfc\u5165**\n\n```js\nimport('./dynamic-module').then((module) => {\n // do something\n})\n// \u4e5f\u652f\u6301await\u5173\u952e\u5b57\nconst module = await import('./dynamic-module')\n```\n\n**2\uff09vue \u4e2d\u901a\u8fc7 router \u914d\u7f6e**\n\nvue \u4e2d\u901a\u8fc7 router \u914d\u7f6e, \u5b9e\u73b0\u7ec4\u4ef6\u7684\u6309\u9700\u52a0\u8f7d, \u5728\u4e00\u4e9b\u5355\u4e2a\u7ec4\u4ef6\u6587\u4ef6\u8f83\u5927\u7684\u65f6\u5019, \u91c7\u7528\u6309\u9700\u52a0\u8f7d\u80fd\u591f\u51cf\u5c11build.js\u7684\u4f53\u79ef, \u4f18\u5316\u52a0\u8f7d\u901f\u5ea6(\u5982\u679c\u7ec4\u4ef6\u7684\u4f53\u79ef\u8f83\u5c0f, \u90a3\u4e48\u91c7\u7528\u6309\u9700\u52a0\u8f7d\u4f1a\u589e\u52a0\u989d\u5916\u7684http\u8bf7\u6c42, \u53cd\u5012\u589e\u52a0\u4e86\u52a0\u8f7d\u65f6\u95f4)\n\n```js\n//app.js\n\nimport Vue from 'vue'\nimport App from './App.vue'\nimport VueRouter from 'vue-router'\nVue.use(VueRouter)\n\n//AMD\u89c4\u8303\u7684\u5f02\u6b65\u8f7d\u5165\nconst ComA = resolve => require(['./components/A.vue' ], resolve);\nconst ComB = resolve => require(['./components/B.vue' ], resolve);\nconst ComC = resolve => require(['./components/C.vue' ], resolve);\n\n//CMD\u98ce\u683c\u7684\u5f02\u6b65\u52a0\u8f7d\nconst ComA = resolve => require.ensure([], () => resolve(require('./components/A.vue')));\nconst ComB = resolve => require.ensure([], () => resolve(require('./components/B.vue')));\nconst ComC = resolve => require.ensure([], () => resolve(require('./components/C.vue')));\n\nconst router = new VueRouter({\n routes: [\n {\n name: 'component-A',\n path: '/a',\n component: ComA\n },\n {\n name: 'component-B',\n path: '/b',\n component: ComB\n },\n {\n name: 'component-C',\n path: '/c',\n component: ComC\n }\n ]\n})\n\nnew Vue({\n el: '#app',\n router: router,\n render: h => h(App)\n})\n```\n\nvue-cli\u914d\u7f6e\u6216webpack\u914d\u7f6e\n\n```js\n//webpack.config.js\noutput: {\n path: path.resolve(__dirname, './dist'),\n publicPath: '/dist/',\n filename: 'build.js',\n //\u6dfb\u52a0chundkFilename\n chunkFilename: '[name].[chunkhash:5].chunk.js'\n}\n```\n\n**3\uff09Webpack\u6253\u5305\u6a21\u5757\u5de5\u5177\u5b9e\u73b0**\n\n\u5728\u5927\u578b\u9879\u76ee\u4e2d, build.js\u53ef\u80fd\u8fc7\u5927, \u5bfc\u81f4\u9875\u9762\u52a0\u8f7d\u65f6\u95f4\u8fc7\u957f\u3002\u8fd9\u4e2a\u65f6\u5019\u5c31\u9700\u8981code splitting, code splitting\u5c31\u662f\u5c06\u6587\u4ef6\u5206\u5272\u6210\u5757(chunk), \u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e9b\u5206\u5272\u70b9(split point), \u6839\u636e\u8fd9\u4e9b\u5206\u5272\u70b9\u5bf9\u6587\u4ef6\u8fdb\u884c\u5206\u5757, \u5e76\u5b9e\u73b0\u6309\u9700\u52a0\u8f7d.\n\n\n\n", "answer": "", "biz_type": 1, "qid": 89, "subject": "", "title": "\u5982\u4f55\u5b9e\u73b0\u6309\u9700\u52a0\u8f7d"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u8fb9\u950b"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 5, "qid": 109, "qtype": "short_answer", "short_answer": {"analysis": "#### uml\u56fe\n\n![uml\u56fe\u793a](http://img-static.yidengxuetang.com/wxapp/issue-img/qid-109-uml.png)\n\n#### \u4ee3\u7801\u5b9e\u73b0\n\n```js\n// \u505c\u8f66\u573a\u7c7b\nclass ParkingLot{\n constructor(n) {\n // \u505c\u8f66\u4f4d\n this.parkSites = []\n // \u5269\u4f59\u505c\u8f66\u4f4d\u4e2a\u6570\n this.leftSites = n\n this.board = new DisplayBoard()\n // \u521d\u59cb\u5316\u505c\u8f66\u4f4d\n for (let i = 1; i <= n; i++) {\n this.parkSites.push(new ParkingSpace(i))\n }\n console.log('\u505c\u8f66\u573a\u521d\u59cb\u5316\u5b8c\u6bd5')\n this.showLeftSites()\n }\n\n // \u8fdb\u505c\u8f66\u573a\u505c\u8f66\n inPark(car) {\n if (this.leftSites === 0) {\n console.log('\u505c\u8f66\u4f4d\u5df2\u6ee1')\n return\n }\n if (car.site) {\n console.log(car.carId + '\u8f66\u8f86\u5df2\u7ecf\u5728\u505c\u8f66\u573a\u4e86')\n return\n }\n let len = this.parkSites.length\n for (let i = 0; i < len; i++) {\n const site = this.parkSites[i]\n // \u5982\u679c\u505c\u8f66\u4f4d\u662f\u7a7a\u7684\n if(site.car === null) {\n site.car = car\n car.site = site\n this.leftSites--\n console.log(car.carId + '\u8f66\u8f86\u505c\u5165' + site.id + '\u53f7\u505c\u8f66\u4f4d')\n this.showLeftSites()\n return\n }\n }\n }\n\n // \u51fa\u505c\u8f66\u573a\n outPark (car) {\n if (car.site === null) {\n console.log(car.carId + '\u672c\u6765\u5c31\u6ca1\u5728\u505c\u8f66\u573a')\n return;\n }\n let len = this.parkSites.length\n for (let i = 0; i < len; i++) {\n const site = this.parkSites[i]\n // \u5982\u679c\u505c\u8f66\u4f4d\u662f\u7a7a\u7684\n if(site.car.carId === car.carId) {\n site.car = null\n car.site = null\n this.leftSites++\n console.log(car.carId + '\u8f66\u8f86\u5df2\u4ece' + site.id + '\u53f7\u505c\u8f66\u4f4d\uff0c\u51fa\u505c\u8f66\u573a')\n this.showLeftSites()\n return\n }\n }\n }\n\n // \u663e\u793a\u5269\u4f59\u7684\u505c\u8f66\u4f4d\u6570\u91cf\n showLeftSites() {\n this.board.showLeftSapce(this.leftSites)\n }\n\n}\n\n\n// \u505c\u8f66\u4f4d\nclass ParkingSpace {\n constructor(id) {\n // \u505c\u8f66\u4f4d\u7f16\u53f7\n this.id = id\n // \u505c\u5165\u7684\u8f66\u8f86\n this.car = null\n }\n}\n\n// \u8f66 \u7c7b\nclass Car{\n constructor(carId) {\n // \u8f66\u724c\u53f7\n this.carId = carId\n // \u505c\u5165\u7684\u8f66\u4f4d\n this.site = null\n }\n // \u8fdb\u5165\u505c\u8f66\u573a\n inPark (park) {\n park.inPark(this)\n }\n outPark(park) {\n park.outPark(this)\n }\n}\n\n// \u5c55\u793a\u724c\u7c7b\nclass DisplayBoard {\n constructor() {\n }\n // \u5c55\u793a\u5269\u4f59\u505c\u8f66\u4f4d\n showLeftSapce(n) {\n console.log (`\u5f53\u524d\u5269\u4f59${n}\u4e2a\u505c\u8f66\u4f4d` )\n }\n}\n\n\nconst park = new ParkingLot(3)\n\nconst car1 = new Car('\u4eacA1XXX')\nconst car2 = new Car('\u4eacA2XXX')\nconst car3 = new Car('\u4eacA3XXX')\nconst car4 = new Car('\u4eacA4XXX')\ncar1.inPark(park)\ncar1.inPark(park)\ncar1.outPark(park)\ncar1.outPark(park)\ncar1.inPark(park)\n\ncar2.inPark(park)\ncar3.inPark(park)\ncar4.inPark(park)\n\ncar2.outPark(park)\ncar4.inPark(park)\n\nconsole.log(park.parkSites)\n```", "answer": "", "biz_type": 1, "qid": 109, "subject": "```js\n/*\n *\u9898\u76ee\u8981\u6c42\n *\u4f7f\u7528\u9762\u76f8\u5bf9\u8c61\u8bbe\u8ba1\u4e00\u4e2a\u505c\u8f66\u573a\u7ba1\u7406\u7cfb\u7edf\uff0c\u8be5\u505c\u8f66\u573a\u5305\u542b\uff1a\n *\t1.\u505c\u8f66\u4f4d\uff0c\u7528\u4e8e\u505c\u653e\u8f66\u8f86\uff1b\n *\t2.\u505c\u8f66\u4f4d\u63d0\u793a\u724c\uff0c\u7528\u4e8e\u5c55\u793a\u5269\u4f59\u505c\u8f66\u4f4d\uff1b\n *\u53ef\u4ee5\u4e30\u5bcc\u8be5\u7cfb\u7edf\u7684\u5143\u7d20\uff0c\u7ed9\u51fa\u7c7b\uff0c\u7c7b\u5c5e\u6027\uff0c\u7c7b\u63a5\u53e3\u3002\n */\n```\n", "title": "\u7c7b\u8bbe\u8ba1\uff1a\u4f7f\u7528\u9762\u76f8\u5bf9\u8c61\u8bbe\u8ba1\u4e00\u4e2a\u505c\u8f66\u573a\u7ba1\u7406\u7cfb\u7edf"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f34\u9c7c", "\u559c\u9a6c\u62c9\u96c5"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 4, "qid": 80, "qtype": "short_answer", "short_answer": {"analysis": "#### Promise\n\nPromise \u5bf9\u8c61\u662f JavaScript \u7684\u5f02\u6b65\u64cd\u4f5c\u89e3\u51b3\u65b9\u6848\uff0c\u4e3a\u5f02\u6b65\u64cd\u4f5c\u63d0\u4f9b\u7edf\u4e00\u63a5\u53e3\u3002\u5b83\u8d77\u5230\u4ee3\u7406\u4f5c\u7528\uff08proxy\uff09\uff0c\u5145\u5f53\u5f02\u6b65\u64cd\u4f5c\u4e0e\u56de\u8c03\u51fd\u6570\u4e4b\u95f4\u7684\u4e2d\u4ecb\uff0c\u4f7f\u5f97\u5f02\u6b65\u64cd\u4f5c\u5177\u5907\u540c\u6b65\u64cd\u4f5c\u7684\u63a5\u53e3\u3002Promise \u53ef\u4ee5\u8ba9\u5f02\u6b65\u64cd\u4f5c\u5199\u8d77\u6765\uff0c\u5c31\u50cf\u5728\u5199\u540c\u6b65\u64cd\u4f5c\u7684\u6d41\u7a0b\uff0c\u800c\u4e0d\u5fc5\u4e00\u5c42\u5c42\u5730\u5d4c\u5957\u56de\u8c03\u51fd\u6570\u3002\n\n\n#### Promise\u5bf9\u8c61\u72b6\u6001\n\nPromise \u5bf9\u8c61\u901a\u8fc7\u81ea\u8eab\u7684\u72b6\u6001\uff0c\u6765\u63a7\u5236\u5f02\u6b65\u64cd\u4f5c\u3002Promise \u5b9e\u4f8b\u5177\u6709\u4e09\u79cd\u72b6\u6001\u3002\n\n- \u5f02\u6b65\u64cd\u4f5c\u672a\u5b8c\u6210\uff08pending\uff09\n- \u5f02\u6b65\u64cd\u4f5c\u6210\u529f\uff08fulfilled\uff09\n- \u5f02\u6b65\u64cd\u4f5c\u5931\u8d25\uff08rejected\uff09\n\n\u4e0a\u9762\u4e09\u79cd\u72b6\u6001\u91cc\u9762\uff0cfulfilled\u548crejected\u5408\u5728\u4e00\u8d77\u79f0\u4e3aresolved\uff08\u5df2\u5b9a\u578b\uff09\u3002\n\n\u8fd9\u4e09\u79cd\u7684\u72b6\u6001\u7684\u53d8\u5316\u9014\u5f84\u53ea\u6709\u4e24\u79cd\u3002\n\n- \u4ece\u201c\u672a\u5b8c\u6210\u201d\u5230\u201c\u6210\u529f\u201d\n- \u4ece\u201c\u672a\u5b8c\u6210\u201d\u5230\u201c\u5931\u8d25\u201d\n\n\u4e00\u65e6\u72b6\u6001\u53d1\u751f\u53d8\u5316\uff0c\u5c31\u51dd\u56fa\u4e86\uff0c\u4e0d\u4f1a\u518d\u6709\u65b0\u7684\u72b6\u6001\u53d8\u5316\u3002\u8fd9\u4e5f\u662f Promise \u8fd9\u4e2a\u540d\u5b57\u7684\u7531\u6765\uff0c\u5b83\u7684\u82f1\u8bed\u610f\u601d\u662f\u201c\u627f\u8bfa\u201d\uff0c\u4e00\u65e6\u627f\u8bfa\u6210\u6548\uff0c\u5c31\u4e0d\u5f97\u518d\u6539\u53d8\u4e86\u3002\u8fd9\u4e5f\u610f\u5473\u7740\uff0cPromise \u5b9e\u4f8b\u7684\u72b6\u6001\u53d8\u5316\u53ea\u53ef\u80fd\u53d1\u751f\u4e00\u6b21\u3002\n\n\u56e0\u6b64\uff0cPromise \u7684\u6700\u7ec8\u7ed3\u679c\u53ea\u6709\u4e24\u79cd\uff1a\n\n- \u5f02\u6b65\u64cd\u4f5c\u6210\u529f\uff0cPromise \u5b9e\u4f8b\u4f20\u56de\u4e00\u4e2a\u503c\uff08value\uff09\uff0c\u72b6\u6001\u53d8\u4e3afulfilled\n- \u5f02\u6b65\u64cd\u4f5c\u5931\u8d25\uff0cPromise \u5b9e\u4f8b\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\uff08error\uff09\uff0c\u72b6\u6001\u53d8\u4e3arejected\n\n#### \u9519\u8bef\u6355\u83b7\n\n\u901a\u8fc7catch\u6355\u83b7\u5230reject\u4e4b\u540e\uff0c\u5728catch\u540e\u9762\u8fd8\u53ef\u4ee5\u7ee7\u7eed\u987a\u5e8f\u6267\u884cthen\u65b9\u6cd5\uff0c\u4f46\u662f\u53ea\u6267\u884cthen\u7684\u7b2c\u4e00\u4e2a\u56de\u8c03(resolve\u56de\u8c03)\n\n```js\nPromise.reject(2)\n .catch(r => {\n // \u6355\u83b7\u5230\u9519\u8bef\uff0c\u6267\u884c\n console.log('catch1');\n })\n // \u9519\u8bef\u5df2\u7ecf\u88ab\u6355\u83b7\uff0c\u540e\u8fb9\u7684`then`\u90fd\u987a\u5e8f\u6267\u884c\uff0c\u4e14\u53ea\u6267\u884c`then`\u7684\u7b2c\u4e00\u4e2a\u56de\u8c03\uff08resolve\u7684\u56de\u8c03\uff09\n .then(v => {\n console.log('then1');\n }, r => {\n console.log('catch2');\n })\n .catch(r => {\n // \u524d\u8fb9\u6ca1\u6709\u672a\u6355\u83b7\u7684\u9519\u8bef\uff0c\u4e0d\u6267\u884c\n console.log('catch3');\n })\n .then(v => {\n console.log('then2');\n }, r => {\n console.log('catch4');\n });\n```\n\u7ed3\u679c\u4f1a\u6253\u5370\uff1acatch1\u3001then1\u3001then2", "answer": "", "biz_type": 1, "qid": 80, "subject": "", "title": "\u8bf4\u4e00\u8bf4 promise\uff0c\u6709\u51e0\u4e2a\u72b6\u6001\uff0c\u901a\u8fc7 catch \u6355\u83b7\u5230 reject \u4e4b\u540e\uff0c\u5728 catch \u540e\u9762\u8fd8\u80fd\u7ee7\u7eed\u6267\u884c then \u65b9\u6cd5\u561b\uff0c\u5982\u679c\u80fd\u6267\u884c\u6267\u884c\u7684\u662f\u7b2c\u51e0\u4e2a\u56de\u8c03\u51fd\u6570"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 5, "qid": 77, "qtype": "short_answer", "short_answer": {"analysis": "#### ios\u4e0bfixed\u5931\u6548\u95ee\u9898\n\n**\u539f\u56e0**\n\n\u8f6f\u952e\u76d8\u5524\u8d77\u540e\uff0c\u9875\u9762\u7684 fixed \u5143\u7d20\u5c06\u5931\u6548\uff08ios\u8ba4\u4e3a\u7528\u6237\u66f4\u5e0c\u671b\u7684\u662f\u5143\u7d20\u968f\u7740\u6eda\u52a8\u800c\u79fb\u52a8\uff0c\u4e5f\u5c31\u662f\u53d8\u6210\u4e86 absolute \u5b9a\u4f4d\uff09\uff0c\u65e2\u7136\u53d8\u6210\u4e86absolute\uff0c\u6240\u4ee5\u5f53\u9875\u9762\u8d85\u8fc7\u4e00\u5c4f\u4e14\u6eda\u52a8\u65f6\uff0c\u5931\u6548\u7684 fixed \u5143\u7d20\u5c31\u4f1a\u8ddf\u968f\u6eda\u52a8\u4e86\u3002\n\n\u4e0d\u4ec5\u9650\u4e8e type=text \u7684\u8f93\u5165\u6846\uff0c\u51e1\u662f\u8f6f\u952e\u76d8\uff08\u6bd4\u5982\u65f6\u95f4\u65e5\u671f\u9009\u62e9\u3001select \u9009\u62e9\u7b49\u7b49\uff09\u88ab\u5524\u8d77\uff0c\u90fd\u4f1a\u9047\u5230\u540c\u6837\u5730\u95ee\u9898\u3002\n\n**\u89e3\u51b3**\n\n1\uff09\u65e2\u7136\u4f1a\u53d8\u6210absolute\uff0c\u7d22\u6027\u76f4\u63a5\u4f7f\u7528absolute\n\nbottom\u76f4\u63a5\u4ee5body\u4f5c\u4e3a\u7236\u5143\u7d20\u6765\u8fdb\u884c\u7edd\u5bf9\u5b9a\u4f4d\uff0c\u4e0d\u8fc7\u8fd9\u79cd\u7f51\u4e0a\u90fd\u4e0d\u63a8\u8350\uff0c\u60f3\u6765\u6709\u66f4\u591a\u7684\u95ee\u9898\u7b49\u5f85\u4fee\u6b63\uff0c\u524d\u4eba\u7684\u7ecf\u9a8c\u8fd8\u662f\u8981\u501f\u9274\u7684\u3002\n\n2\uff09\u4e0d\u8ba9\u9875\u9762\u6eda\u52a8\uff0c\u800c\u662f\u8ba9\u4e3b\u4f53\u90e8\u5206\u81ea\u5df1\u6eda\u52a8\n\n\u5982\u679cfixed\u7684\u5931\u6548\uff0c\u4f46\u662f\u9875\u9762\u5e76\u6ca1\u6709\u8d85\u8fc7\u4e00\u5c4f\u7684\u957f\u5ea6\uff0c\u90a3\u4e48\u65e0\u8bbaabsolut\u6216\u8005fixed\u4e5f\u6ca1\u4ec0\u4e48\u5dee\u522b\u3002\u987a\u7740\u8fd9\u4e2a\u601d\u8def\uff0c\u5b8c\u5168\u53ef\u4ee5\u8ba9main\u76f4\u63a5\u6eda\u7740\u73a9\u5c31\u884c\u4e86\u3002\u5c06\u5438\u5e95\u7684\u5143\u7d20\u548c\u4e3b\u9898\u4f5c\u4e3a\u4e24\u5927\u5bb9\u5668\uff0c\u4e3b\u4f53\u90e8\u5206\uff0c\u8bbe\u7f6e\u7edd\u5bf9\u5b9a\u4f4d\uff0c\u56fa\u5b9a\u5728\u5c4f\u5e55\u4e2d\u95f4\uff0c\u8d85\u51fa\u90e8\u5206\u5c31\u81ea\u884c\u6eda\u52a8\uff0c\u5438\u5e95\u5143\u7d20\u5c31\u53ef\u4ee5\u81ea\u5df1\u73a9\u4e86\n\n```html\n<body>\n <div class='warper'>\n <div class='top'></div>\n <div class='main'></div>\n <div>\n <div class=\"fix-bottom\"></div>\n</body>\n```\n\n```css\n.cont-warper{\n position: absolute;\n width: 100%;\n left: 0;\n right: 0;\n top: 0;\n bottom: 0;\n overflow-y: scroll;\n /* \u89e3\u51b3ios\u6ed1\u52a8\u4e0d\u6d41\u7545\u95ee\u9898 */\n -webkit-overflow-scrolling: touch;\n}\n\n.fix-bottom{\n position:fixed;\n bottom:0;\n width: 100%;\n}\n```\n\n\u8fd9\u6837\u5c31\u80fd\u907f\u514d\u4e0a\u9762\u90a3\u4e2a\u95ee\u9898\u4e86\u3002\u4f46\u662fios\u4e0b\uff0c\u5bf9\u4e8e\u5438\u5e95\u5143\u7d20\u800c\u8a00\u5728\u5c4f\u5e55\u4e0b\u534a\u90e8\u5206\u5524\u8d77\u952e\u76d8\u7684\u65f6\u5019\uff0c\u4f1a\u88ab\u906e\u4f4f\u90e8\u5206\u4e1c\u897f\uff0c\n\n\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u53ea\u597d\u52a0\u4e2a\u76d1\u542c\u4e8b\u4ef6\uff0c\u5f53\u5524\u8d77\u952e\u76d8\u7684\u65f6\u5019\uff0c\u8bbe\u7f6escrollTop\u503c\n\n```js\n/**\n * \u5524\u8d77\u952e\u76d8\uff0c\u6eda\u52a8\n */\nscrollContent() {\n this.interval = setInterval(() => {\n this.scrollToEnd();\n }, 500)\n}\nscrollToEnd() {\n document.body.scrollTop = document.body.scrollHeight;\n}\nclearSrcoll() {\n clearInterval(this.interval);\n}\n```\n\n#### \u5fae\u4fe1\u6253\u5f00\u7f51\u9875\u952e\u76d8\u5f39\u8d77\u540e\u9875\u9762\u4e0a\u6ed1\uff0c\u5bfc\u81f4\u5f39\u6846\u91cc\u7684\u6309\u94ae\u54cd\u5e94\u533a\u57df\u9519\u4f4d\n\n\u548c\u4e0a\u8fb9\u4e3a\u9898\u7c7b\u4f3c\uff0c\u5904\u7406\u65b9\u5f0f\u4e5f\u662f\u8bbe\u7f6escrollTop\n\n**\u95ee\u9898**\n\n\u952e\u76d8\u5f39\u8d77\u9875\u9762\u4e0a\u6ed1\uff0c\u952e\u76d8\u6536\u8d77\u9875\u9762\u4e0d\u4f1a\u56de\u5230\u539f\u4f4d\u7f6e\uff0c\u5bfc\u81f4\u5f39\u6846(css\u8bbe\u7f6eposition\u4e3afixed\u4f1a\u6709\u95ee\u9898\uff0cabsolute\u4e0d\u4f1a\u6709\u95ee\u9898)\u540e\u6309\u94ae\u54cd\u5e94\u533a\u57df\u9519\u4f4d\u3002\n\n**\u89e3\u51b3**\n\n```js\n//\u6eda\u52a8\u5230\u9876\u90e8\nwindow.scrollTo(0, 0);\n//\u6eda\u52a8\u5230\u5e95\u90e8\nwindow.scrollTo(0, document.documentElement.clientHeight);\n```\n\n```js\n//\u89e3\u51b3\u952e\u76d8\u5f39\u51fabug\n// \u5224\u65ad\u662f\u5426\u662fios\nif(_.isIOS()){\nwindow.addEventListener('focusout', function(){\n //\u8f6f\u952e\u76d8\u6536\u8d77\u7684\u4e8b\u4ef6\u5904\u7406\n setTimeout(()=>{\n window.scrollTo(0 ,document.documentElement.scrollTop || document.body.scrollTop);\n })\n });\n}\n```\n\n", "answer": "", "biz_type": 1, "qid": 77, "subject": "", "title": "iPhone \u91cc\u9762 Safari \u4e0a\u5982\u679c\u4e00\u4e2a\u8f93\u5165\u6846 fixed \u7edd\u5bf9\u5b9a\u4f4d\u5728\u5e95\u90e8\uff0c\u5f53\u8f6f\u952e\u76d8\u5f39\u51fa\u7684\u65f6\u5019\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898\uff0c\u5982\u4f55\u89e3\u51b3"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f34\u9c7c", "\u817e\u8baf\u5e94\u7528\u5b9d"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 5, "qid": 90, "qtype": "short_answer", "short_answer": {"analysis": "#### import\u539f\u7406(\u5b9e\u9645\u4e0a\u5c31\u662fES6 module\u7684\u539f\u7406)\n\n1. \u7b80\u5355\u6765\u8bf4\u5c31\u662f\u95ed\u5305\u7684\u8fd0\u7528\n2. \u4e3a\u4e86\u521b\u5efaModule\u7684\u5185\u90e8\u4f5c\u7528\u57df\uff0c\u4f1a\u8c03\u7528\u4e00\u4e2a\u5305\u88c5\u51fd\u6570\n3. \u5305\u88c5\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e5f\u5c31\u662fModule\u5411\u5916\u516c\u5f00\u7684API\uff0c\u4e5f\u5c31\u662f\u6240\u6709export\u51fa\u53bb\u7684\u53d8\u91cf\n4. import\u4e5f\u5c31\u662f\u62ff\u5230module\u5bfc\u51fa\u53d8\u91cf\u7684\u5f15\u7528\n\n#### \u4e0erequire\u7684\u4e0d\u540c\n\n- CommonJS\u6a21\u5757\u8f93\u51fa\u7684\u662f\u4e00\u4e2a\u503c\u7684\u62f7\u8d1d\uff0cES6\u6a21\u5757\u8f93\u51fa\u7684\u662f\u503c\u7684\u5f15\u7528\n- CommonJS\u6a21\u5757\u662f\u8fd0\u884c\u65f6\u52a0\u8f7d\uff0cES6\u6a21\u5757\u662f\u7f16\u8bd1\u65f6\u8f93\u51fa\u63a5\u53e3\n\nCommonJS\u662f\u8fd0\u884c\u65f6\u52a0\u8f7d\u5bf9\u5e94\u6a21\u5757\uff0c\u4e00\u65e6\u8f93\u51fa\u4e00\u4e2a\u503c\uff0c\u5373\u4f7f\u6a21\u5757\u5185\u90e8\u5bf9\u5176\u505a\u51fa\u6539\u53d8\uff0c\u4e5f\u4e0d\u4f1a\u5f71\u54cd\u8f93\u51fa\u503c\uff0c\u5982\uff1a\n\n```js\n// a.js\nvar a = 1;\nfunction changeA(val) {\n a = val;\n}\nmodule.exports = {\n a: a,\n changeA: changeA,\n}\n\n// b.js\nvar modA = require('./a.js');\nconsole.log('before', modA.a); // \u8f93\u51fa1\nmodA.changeA(2);\nconsole.log('after', modA.a); // \u8fd8\u662f1\n```\n\n\u800cES6\u6a21\u5757\u5219\u4e0d\u540c\uff0cimport\u5bfc\u5165\u662f\u5728JS\u5f15\u64ce\u5bf9\u811a\u6b65\u9759\u6001\u5206\u6790\u65f6\u786e\u5b9a\uff0c\u83b7\u53d6\u5230\u7684\u662f\u4e00\u4e2a\u53ea\u8bfb\u5f15\u7528\u3002\u7b49\u811a\u672c\u589e\u957f\u8fd0\u884c\u65f6\uff0c\u4f1a\u6839\u636e\u8fd9\u4e2a\u5f15\u7528\u53bb\u5bf9\u5e94\u6a21\u5757\u4e2d\u53d6\u503c\u3002\u6240\u4ee5\u5f15\u7528\u5bf9\u5e94\u7684\u503c\u6539\u53d8\u65f6\uff0c\u5176\u5bfc\u5165\u7684\u503c\u4e5f\u4f1a\u53d8\u5316", "answer": "", "biz_type": 1, "qid": 90, "subject": "", "title": "\u8bb2\u4e00\u4e0b import \u7684\u539f\u7406\uff0c\u4e0e require \u6709\u4ec0\u4e48\u4e0d\u540c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 7, "qid": 133, "qtype": "short_answer", "short_answer": {"analysis": "#### 1.setTimeout\n\nsetTimeout \u6709\u4e00\u4e2a\u663e\u8457\u7684\u7f3a\u9677\u5728\u4e8e\u65f6\u95f4\u662f\u4e0d\u7cbe\u786e\u7684\uff0csetTimeout \u53ea\u80fd\u4fdd\u8bc1\u5ef6\u65f6\u6216\u95f4\u9694\u4e0d\u5c0f\u4e8e\u8bbe\u5b9a\u7684\u65f6\u95f4\u3002\u56e0\u4e3a\u5b83\u4eec\u5b9e\u9645\u4e0a\u53ea\u662f\u628a\u4efb\u52a1\u6dfb\u52a0\u5230\u4e86\u4efb\u52a1\u961f\u5217\u4e2d\uff0c\u4f46\u662f\u5982\u679c\u524d\u9762\u7684\u4efb\u52a1\u8fd8\u6ca1\u6709\u6267\u884c\u5b8c\u6210\uff0c\u5b83\u4eec\u5fc5\u987b\u8981\u7b49\u5f85\u3002\n\n#### 2.requestAnimationFrame\n\nrequestAnimationFrame \u662f\u7cfb\u7edf\u65f6\u95f4\u95f4\u9694\uff0c\u4fdd\u6301\u6700\u4f73\u7ed8\u5236\u6548\u7387\uff0c\u4e0d\u4f1a\u56e0\u4e3a\u95f4\u9694\u65f6\u95f4\u8fc7\u77ed\uff0c\u9020\u6210\u8fc7\u5ea6\u7ed8\u5236\uff0c\u589e\u52a0\u5f00\u9500\uff0c\u4ece\u800c\u8282\u7701\u7cfb\u7edf\u8d44\u6e90\uff0c\u63d0\u9ad8\u7cfb\u7edf\u6027\u80fd\uff0c\u6539\u5584\u89c6\u89c9\u6548\u679c\u3002\n\nrequestAnimationFrame \u548c setTimeout/setInterval \u5728\u7f16\u5199\u52a8\u753b\u65f6\u76f8\u6bd4\uff0c\u4f18\u70b9\u5982\u4e0b:\n\n1. requestAnimationFrame \u4e0d\u9700\u8981\u8bbe\u7f6e\u65f6\u95f4\uff0c\u91c7\u7528\u7cfb\u7edf\u65f6\u95f4\u95f4\u9694\uff0c\u80fd\u8fbe\u5230\u6700\u4f73\u7684\u52a8\u753b\u6548\u679c\u3002\n2. requestAnimationFrame \u4f1a\u628a\u6bcf\u4e00\u5e27\u4e2d\u7684\u6240\u6709DOM\u64cd\u4f5c\u96c6\u4e2d\u8d77\u6765\uff0c\u5728\u4e00\u6b21\u91cd\u7ed8\u6216\u56de\u6d41\u4e2d\u5c31\u5b8c\u6210\u3002\n3. \u5f53 `requestAnimationFrame()` \u8fd0\u884c\u5728\u540e\u53f0\u6807\u7b7e\u9875\u6216\u8005\u9690\u85cf\u7684 `<iframe>` \u91cc\u65f6\uff0c`requestAnimationFrame()` \u4f1a\u88ab\u6682\u505c\u8c03\u7528\u4ee5\u63d0\u5347\u6027\u80fd\u548c\u7535\u6c60\u5bff\u547d\uff08\u5927\u591a\u6570\u6d4f\u89c8\u5668\u4e2d\uff09\u3002\n\n```js\nfunction step(timestamp) {\n window.requestAnimationFrame(step);\n}\nwindow.requestAnimationFrame(step);\n```", "answer": "", "biz_type": 1, "qid": 133, "subject": "", "title": "setTimeout \u6709\u4ec0\u4e48\u7f3a\u70b9\uff0c\u548c requestAnimationFrame \u6709\u4ec0\u4e48\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 6, "qid": 76, "qtype": "short_answer", "short_answer": {"analysis": "### \u8f6f\u952e\u76d8\u5728Android\u548cIOS\u82f9\u679c\u4e0a\u9762\u7684\u8868\u73b0\n\n#### IOS \u8f6f\u952e\u76d8\u5f39\u8d77\u8868\u73b0\n\n\u5728 IOS \u4e0a\uff0c\u8f93\u5165\u6846\uff08input\u3001textarea \u6216 \u5bcc\u6587\u672c\uff09\u83b7\u53d6\u7126\u70b9\uff0c\u952e\u76d8\u5f39\u8d77\uff0c\u9875\u9762\uff08webview\uff09\u5e76\u6ca1\u6709\u88ab\u538b\u7f29\uff0c\u6216\u8005\u8bf4\u9ad8\u5ea6\uff08height\uff09\u6ca1\u6709\u6539\u53d8\uff0c\u53ea\u662f\u9875\u9762\uff08webview\uff09\u6574\u4f53\u5f80\u4e0a\u6eda\u4e86\uff0c\u4e14\u6700\u5927\u6eda\u52a8\u9ad8\u5ea6\uff08scrollTop\uff09\u4e3a\u8f6f\u952e\u76d8\u9ad8\u5ea6\u3002\n\n#### Android \u8f6f\u952e\u76d8\u5f39\u8d77\u8868\u73b0\n\n\u5728 Android \u4e0a\uff0c\u8f93\u5165\u6846\u83b7\u53d6\u7126\u70b9\uff0c\u952e\u76d8\u5f39\u8d77\uff0c\u4f46\u662f\u9875\u9762\uff08webview\uff09\u9ad8\u5ea6\u4f1a\u53d1\u751f\u6539\u53d8\uff0c\u4e00\u822c\u6765\u8bf4\uff0c\u9ad8\u5ea6\u4e3a\u53ef\u89c6\u533a\u9ad8\u5ea6\uff08\u539f\u9ad8\u5ea6\u51cf\u53bb\u8f6f\u952e\u76d8\u9ad8\u5ea6\uff09\uff0c\u9664\u4e86\u56e0\u4e3a\u9875\u9762\u5185\u5bb9\u88ab\u6491\u5f00\u53ef\u4ee5\u4ea7\u751f\u6eda\u52a8\uff0cwebview \u672c\u8eab\u4e0d\u80fd\u6eda\u52a8\u3002\n\n#### IOS \u8f6f\u952e\u76d8\u6536\u8d77\u8868\u73b0\n\n\u89e6\u53d1\u8f6f\u952e\u76d8\u4e0a\u7684\u201c\u6536\u8d77\u201d\u6309\u94ae\u952e\u76d8\u6216\u8005\u8f93\u5165\u6846\u4ee5\u5916\u7684\u9875\u9762\u533a\u57df\u65f6\uff0c\u8f93\u5165\u6846\u5931\u53bb\u7126\u70b9\uff0c\u8f6f\u952e\u76d8\u6536\u8d77\u3002\n\n#### Android \u8f6f\u952e\u76d8\u6536\u8d77\u8868\u73b0\n\n\u89e6\u53d1\u8f6f\u952e\u76d8\u4e0a\u7684\u201c\u6536\u8d77\u201d\u6309\u94ae\u952e\u76d8\u6216\u8005\u8f93\u5165\u6846\u4ee5\u5916\u7684\u9875\u9762\u533a\u57df\u65f6\uff0c\u8f93\u5165\u6846\u5931\u53bb\u7126\u70b9\uff0c\u8f6f\u952e\u76d8\u6536\u8d77\u3002\n\n### \u89e3\u51b3\u4e24\u7aef\u5f39\u51fa\u548c\u6536\u8d77\u8868\u73b0\u4e0d\u4e00\u81f4\u65b9\u6848\n\n- \u5728 IOS \u4e0a\uff0c\u76d1\u542c\u8f93\u5165\u6846\u7684 focus \u4e8b\u4ef6\u6765\u83b7\u77e5\u8f6f\u952e\u76d8\u5f39\u8d77\uff0c\u76d1\u542c\u8f93\u5165\u6846\u7684 blur \u4e8b\u4ef6\u83b7\u77e5\u8f6f\u952e\u76d8\u6536\u8d77\u3002\n- \u5728 Android \u4e0a\uff0c\u76d1\u542c webview \u9ad8\u5ea6\u4f1a\u53d8\u5316\uff0c\u9ad8\u5ea6\u53d8\u5c0f\u83b7\u77e5\u8f6f\u952e\u76d8\u5f39\u8d77\uff0c\u5426\u5219\u8f6f\u952e\u76d8\u6536\u8d77\u3002\n\n```js\n// \u5224\u65ad\u8bbe\u5907\u7c7b\u578b\nvar judgeDeviceType = function () {\n var ua = window.navigator.userAgent.toLocaleLowerCase();\n var isIOS = /iphone|ipad|ipod/.test(ua);\n var isAndroid = /android/.test(ua);\n\n return {\n isIOS: isIOS,\n isAndroid: isAndroid\n }\n}()\n\n// \u76d1\u542c\u8f93\u5165\u6846\u7684\u8f6f\u952e\u76d8\u5f39\u8d77\u548c\u6536\u8d77\u4e8b\u4ef6\nfunction listenKeybord($input) {\n if (judgeDeviceType.isIOS) {\n // IOS \u952e\u76d8\u5f39\u8d77\uff1aIOS \u548c Android \u8f93\u5165\u6846\u83b7\u53d6\u7126\u70b9\u952e\u76d8\u5f39\u8d77\n $input.addEventListener('focus', function () {\n console.log('IOS \u952e\u76d8\u5f39\u8d77\u5566\uff01');\n // IOS \u952e\u76d8\u5f39\u8d77\u540e\u64cd\u4f5c\n }, false)\n\n // IOS \u952e\u76d8\u6536\u8d77\uff1aIOS \u70b9\u51fb\u8f93\u5165\u6846\u4ee5\u5916\u533a\u57df\u6216\u70b9\u51fb\u6536\u8d77\u6309\u94ae\uff0c\u8f93\u5165\u6846\u90fd\u4f1a\u5931\u53bb\u7126\u70b9\uff0c\u952e\u76d8\u4f1a\u6536\u8d77\uff0c\n $input.addEventListener('blur', () => {\n console.log('IOS \u952e\u76d8\u6536\u8d77\u5566\uff01');\n // IOS \u952e\u76d8\u6536\u8d77\u540e\u64cd\u4f5c\n })\n }\n\n // Andriod \u952e\u76d8\u6536\u8d77\uff1aAndriod \u952e\u76d8\u5f39\u8d77\u6216\u6536\u8d77\u9875\u9762\u9ad8\u5ea6\u4f1a\u53d1\u751f\u53d8\u5316\uff0c\u4ee5\u6b64\u4e3a\u4f9d\u636e\u83b7\u77e5\u952e\u76d8\u6536\u8d77\n if (judgeDeviceType.isAndroid) {\n var originHeight = document.documentElement.clientHeight || document.body.clientHeight;\n\n window.addEventListener('resize', function () {\n var resizeHeight = document.documentElement.clientHeight || document.body.clientHeight;\n if (originHeight < resizeHeight) {\n console.log('Android \u952e\u76d8\u6536\u8d77\u5566\uff01');\n // Android \u952e\u76d8\u6536\u8d77\u540e\u64cd\u4f5c\n } else {\n console.log('Android \u952e\u76d8\u5f39\u8d77\u5566\uff01');\n // Android \u952e\u76d8\u5f39\u8d77\u540e\u64cd\u4f5c\n }\n\n originHeight = resizeHeight;\n }, false)\n }\n}\n\nvar $inputs = document.querySelectorAll('.input');\n\nfor (var i = 0; i < $inputs.length; i++) {\n listenKeybord($inputs[i]);\n}\n```", "answer": "", "biz_type": 1, "qid": 76, "subject": "", "title": "\u79fb\u52a8\u8bbe\u5907\u5b89\u5353\u4e0e iOS \u7684\u8f6f\u952e\u76d8\u5f39\u51fa\u7684\u5904\u7406\u65b9\u5f0f\u6709\u4ec0\u4e48\u4e0d\u540c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u7f51\u6613", "\u6d0b\u8471\u5b66\u9662"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 5, "qid": 37, "qtype": "short_answer", "short_answer": {"analysis": "### \u7b54\u6848\n\n\u65ad\u70b9\u7eed\u4f20\u7684\u539f\u7406\u5728\u4e8e\u524d\u7aef/\u670d\u52a1\u7aef\u9700\u8981\u8bb0\u4f4f\u5df2\u4e0a\u4f20\u7684\u5207\u7247\uff0c\u8fd9\u6837\u4e0b\u6b21\u4e0a\u4f20\u5c31\u53ef\u4ee5\u8df3\u8fc7\u4e4b\u524d\u5df2\u4e0a\u4f20\u7684\u90e8\u5206\uff0c\u6709\u4e24\u79cd\u65b9\u6848\u5b9e\u73b0\u8bb0\u5fc6\u7684\u529f\u80fd\n\n1. \u524d\u7aef\u4f7f\u7528 localStorage \u8bb0\u5f55\u5df2\u4e0a\u4f20\u7684\u5207\u7247 hash\n2. \u670d\u52a1\u7aef\u4fdd\u5b58\u5df2\u4e0a\u4f20\u7684\u5207\u7247 hash\uff0c\u524d\u7aef\u6bcf\u6b21\u4e0a\u4f20\u524d\u5411\u670d\u52a1\u7aef\u83b7\u53d6\u5df2\u4e0a\u4f20\u7684\u5207\u7247\n\n\u7b2c\u4e00\u79cd\u662f\u524d\u7aef\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u7b2c\u4e8c\u79cd\u662f\u670d\u52a1\u7aef\uff0c\u800c\u524d\u7aef\u65b9\u6848\u6709\u4e00\u4e2a\u7f3a\u9677\uff0c\u5982\u679c\u6362\u4e86\u4e2a\u6d4f\u89c8\u5668\u5c31\u5931\u53bb\u4e86\u8bb0\u5fc6\u7684\u6548\u679c\u3002\n\n![\u5927\u6587\u4ef6\u4e0a\u4f20](http://img-static.yidengxuetang.com/wxapp/issue-img/fileupload.png)\n\n#### \u4e00\u3001\u524d\u7aef\n\n**1\uff09.\u524d\u7aef\u6846\u67b6\u4f7f\u7528Vue+Element UI**\n\n```js\nimport Vue from 'vue'\nimport App from './App.vue'\nimport ElementUI from 'element-ui';\nimport 'element-ui/lib/theme-chalk/index.css';\n\nVue.use(ElementUI);\n\nVue.config.productionTip = false\nnew Vue({\n render: h => h(App)\n}).$mount('#app')\n\n```\n\n**2\uff09.\u4e0a\u4f20\u7ec4\u4ef6**\n\n1. \u4e0a\u4f20\u3001\u6062\u590d\u3001\u6682\u505c\u6682\u505c\u6309\u94ae\n2. hash\u8ba1\u7b97\u8fdb\u5ea6\n3. \u4e0a\u4f20\u6587\u4ef6\u603b\u8fdb\u5ea6\n\n```js\n<template>\n <div id=\"app\">\n <div>\n <input\n type=\"file\"\n :disabled=\"status !== Status.wait\"\n @change=\"handleFileChange\"\n />\n <el-button @click=\"handleUpload\" :disabled=\"uploadDisabled\"\n >\u4e0a\u4f20</el-button\n >\n <el-button @click=\"handleResume\" v-if=\"status === Status.pause\"\n >\u6062\u590d</el-button\n >\n <el-button\n v-else\n :disabled=\"status !== Status.uploading || !container.hash\"\n @click=\"handlePause\"\n >\u6682\u505c</el-button\n >\n </div>\n <div>\n <div>\u8ba1\u7b97\u6587\u4ef6 hash</div>\n <el-progress :percentage=\"hashPercentage\"></el-progress>\n <div>\u603b\u8fdb\u5ea6</div>\n <el-progress :percentage=\"fakeUploadPercentage\"></el-progress>\n </div>\n <el-table :data=\"data\">\n <el-table-column\n prop=\"hash\"\n label=\"\u5207\u7247hash\"\n align=\"center\"\n ></el-table-column>\n <el-table-column label=\"\u5927\u5c0f(KB)\" align=\"center\" width=\"120\">\n <template v-slot=\"{ row }\">\n {{ row.size | transformByte }}\n </template>\n </el-table-column>\n <el-table-column label=\"\u8fdb\u5ea6\" align=\"center\">\n <template v-slot=\"{ row }\">\n <el-progress\n :percentage=\"row.percentage\"\n color=\"#909399\"\n ></el-progress>\n </template>\n </el-table-column>\n </el-table>\n </div>\n</template>\n```\n\n**3\uff09\u6587\u4ef6\u4e0a\u4f20\u548c\u65ad\u70b9\u7eed\u4f20\u903b\u8f91**\n\n1. \u6838\u5fc3\u662f`Blob.prototype.slice` \u65b9\u6cd5\uff0c\u5c06\u6e90\u6587\u4ef6\u5207\u6210\u591a\u4e2a\u5207\u7247\n2. \u6839\u636e\u5207\u7247\u5185\u5bb9\u751f\u6210hash\uff0c\u6b64\u5904\u7528\u5230\u7684\u662fspark-md5.js\uff0c\u56e0\u4e3a\u89e3\u6790\u5207\u7247\u5185\u5bb9\u6bd4\u8f83\u8017\u65f6\uff0c\u6240\u4ee5\u5f00\u8f9f\u4e86WebWorker\u7ebf\u7a0b\u6765\u5904\u7406hash\u7684\u751f\u6210\uff0c\u5728\u5904\u7406\u5207\u7247hash\u7684\u65f6\u5019\uff0c\u8fd8\u4e0e\u4e3b\u7ebf\u7a0b\u8fdb\u884c\u901a\u4fe1\u8fd4\u56de\u8fdb\u5ea6\u3002\n3. \u5411\u670d\u52a1\u5668\u53d1\u8bf7\u6c42\uff0c\u68c0\u9a8c\u6587\u4ef6\u5207\u7247\u662f\u5426\u4e0a\u4f20,\u8fd4\u56de\u662f\u5426\u9700\u8981\u7ee7\u7eed\u4e0a\u4f20\u548c\u5df2\u4e0a\u4f20\u5217\u8868\uff08\u65ad\u70b9\u7eed\u4f20\u6838\u5fc3\uff09\u3002\n4. \u5229\u7528http \u7684\u53ef\u5e76\u53d1\u6027\uff0c\u540c\u65f6\u4e0a\u4f20\u591a\u4e2a\u5207\u7247\uff0c\u51cf\u5c11\u4e0a\u4f20\u65f6\u95f4\n5. \u5207\u7247\u4e0a\u4f20\u5b8c\u6210\uff0c\u7ed9\u670d\u52a1\u5668\u53d1\u9001\u5408\u5e76\u5207\u7247\u8bf7\u6c42\n\n- \u5e38\u91cf\u548c\u57fa\u7840\u5c5e\u6027\n\n```js\n\n //\u5355\u4e2a\u5207\u7247\u5927\u5c0f\nconst SIZE = 10*1024*1024;//10MB\n//\u72b6\u6001\u5e38\u91cf\nconst Status = {\n wait: \"wait\",\n pause: \"pause\",\n uploading: \"uploading\"\n};\nexport default {\n name: \"App\",\n filters: {\n transformByte(val) {\n return Number((val / 1024).toFixed(0));\n }\n },\n data() {\n return {\n Status,\n container: { //\u4fdd\u5b58\u6587\u4ef6\u4fe1\u606f\n file: null,\n hash: \"\",//\u6240\u6709\u5207\u7247hash\n worker: null\n },\n hashPercentage:0,//hash\u8fdb\u5ea6\u767e\u5206\u6bd4\n data: [],//\u4fdd\u5b58\u6240\u6709\u5207\u7247\u4fe1\u606f\n requestList: [],//\u8bf7\u6c42\u5217\u8868\n status: Status.wait,//\u72b6\u6001\uff0c\u9ed8\u8ba4\u4e3a\u7b49\u5f85\n fakeUploadPercentage: 0//\u6587\u4ef6\u4e0a\u4f20\u603b\u8fdb\u5ea6\n };\n },\n}\n\n```\n\n- \u8ba1\u7b97\u5c5e\u6027\u3001watch\n\n```js\ncomputed: {\n //\u4e0a\u4f20\u6309\u94ae\u4e0d\u53ef\u7528\n uploadDisabled() {\n return (\n !this.container.file ||\n [Status.pause, Status.uploading].includes(this.status)\n );\n },\n //\u4e0b\u8f7d\u8fdb\u5ea6\u767e\u5206\u6bd4\n uploadPercentage() {\n if (!this.container.file || !this.data.length) return 0;\n const loaded = this.data\n .map(item => item.size * item.percentage)\n .reduce((acc, cur) => acc + cur);\n return parseInt((loaded / this.container.file.size).toFixed(2));\n }\n },\n watch: {\n //\u4e0b\u8f7d\u8fdb\u5ea6\u767e\u5206\u6bd4\n uploadPercentage(now) {\n if (now > this.fakeUploadPercentage) {\n this.fakeUploadPercentage = now;\n }\n }\n },\n```\n\n- methods\uff1a\u4e09\u4e2a\u6309\u94ae\u4e0a\u9762\u7684\u65b9\u6cd5\u5b9a\u4e49\n\n```js\nmethods: {\n //\u4e2d\u6b62\u5904\u7406\u51fd\u6570\n handlePause() {\n this.status = Status.pause;\n this.resetData();\n },\n resetData() {\n //\u4e2d\u6b62\u8bf7\u6c42\u5217\u8868\u4e2d\u7684\u6240\u6709\u8bf7\u6c42\n this.requestList.forEach(xhr => {\n if (xhr) {\n xhr.abort();\n }\n });\n this.requestList = [];\n if (this.container.worker) {\n this.container.worker.onmessage = null;\n }\n },\n //\u6062\u590d\u5904\u7406\u51fd\u6570\n async handleResume() {\n this.status = Status.uploading;\n //\u83b7\u53d6\u5df2\u7ecf\u4e0a\u4f20\u7684\u6587\u4ef6\u540d\u548chash\n const { uploadedList } = await this.verifyUpload(\n this.container.file.name,\n this.container.hash\n );\n await this.uploadChunks(unloadedList);\n },\n //file input change\u4e8b\u4ef6\u89e6\u53d1\n handleFileChange(e){\n const [file] = e.target.files;\n if(!file) return;\n this.resetData();\n Object.assign(this.$data,this.$options.data());\n this.container.file = file;\n }\n}\n```\n\n- \u6838\u5fc3\uff1a\u6587\u4ef6\u4e0a\u4f20\u903b\u8f91 **\u5207\u7247=>hash=>\u6821\u9a8c=>\u6279\u91cf\u4e0a\u4f20**\n\n```js\nasync handleUpload(){\n if(!this.container.file) return;\n this.status = Status.uploading;\n //\u751f\u6210\u6587\u4ef6\u5207\u7247\n const fileChunkList = this.createFileChunk(this.container.file);\n //\u6839\u636e\u5207\u7247\u5217\u8868\u8ba1\u7b97\u5207\u7247hash\n this.container.hash = await this.calculateHash(fileChunkList);\n //\u68c0\u9a8c\u6587\u4ef6\u5207\u7247\u662f\u5426\u4e0a\u4f20,\u8fd4\u56de\u662f\u5426\u9700\u8981\u4e0a\u4f20\u548c\u5df2\u4e0a\u4f20\u5217\u8868\n const { shouldUpload,uploadedList } = await this.verifyUpload(\n this.container.file.name,\n this.container.hash\n );\n //\u6ca1\u6709\u9700\u8981\u4e0a\u4f20\u7684\u6587\u4ef6\u5207\u7247\n if(!shouldUpload){\n this.$message.sucess('\u79d2\u4f20\uff1a\u4e0a\u4f20\u6210\u529f');\n this.status = Status.wait;\n return;\n }\n //\u6839\u636e\u6587\u4ef6\u5217\u8868\u751f\u6210\u6bcf\u4e2a\u5207\u7247\u7684\u4fe1\u606f\u5bf9\u8c61\n this.data = fileChunkList.map(({file},index)=>({\n fileHash : this.container.hash,\n index,\n hash: this.container.hash + '-'+index,\n chunk:file,\n size:file.size,\n percentage:uploadedList.includes(index)?100:0\n }));\n //\u4e0a\u4f20\u6587\u4ef6\u5207\u7247\n await this.uploadChunks(uploadedList);\n },\n```\n\n- \u6587\u4ef6\u6838\u5fc3\u903b\u8f91\u5b9e\u73b0\uff1a\u751f\u6210\u6587\u4ef6\u5207\u7247\uff1afile.slice()\n\n```js\n// \u751f\u6210\u6587\u4ef6\u5207\u7247 file.slice\ncreateFileChunk(file, size = SIZE) {\n const fileChunkList = [];\n let cur = 0;\n while (cur < file.size) {\n fileChunkList.push({ file: file.slice(cur, cur + size) });\n cur += size;\n }\n return fileChunkList;\n},\n```\n\n- \u751f\u6210\u6587\u4ef6\u5207\u7247hash: webworker\n\n```js\n// \u751f\u6210\u6587\u4ef6 hash\uff08web-worker\uff09\ncalculateHash(fileChunkList) {\n return new Promise(resolve => {\n this.container.worker = new Worker(\"/hash.js\");\n //\u4e0eworker\u901a\u4fe1\n this.container.worker.postMessage({ fileChunkList });\n this.container.worker.onmessage = e => {\n const { percentage, hash } = e.data;\n this.hashPercentage = percentage;\n //\u8fd4\u56de\u603b\u6587\u4ef6\u751f\u6210hash\u8fdb\u5ea6\u7684\u767e\u5206\u6bd4\uff0c\u5982\u679c\u5207\u7247hash\u5168\u90e8\u751f\u6210\uff0c\u8fd4\u56de\u6240\u6709\u5207\u7247hash\u7ec4\u6210\u7684\u5bf9\u8c61\n if (hash) {\n resolve(hash);\n }\n };\n });\n},\n```\n\n- hash.js\uff1a\u8fb9\u8ba1\u7b97\u8fb9\u4e0e\u4e3b\u7ebf\u7a0b\u8fdb\u884c\u901a\u4fe1\uff0c\u8fd4\u56dehash\u8ba1\u7b97\u8fdb\u5ea6\n\n```js\nself.importScripts(\"/spark-md5.min.js\"); // \u5bfc\u5165\u811a\u672c\n\n// \u751f\u6210\u6587\u4ef6 hash\nself.onmessage = e => {\n const { fileChunkList } = e.data;\n const spark = new self.SparkMD5.ArrayBuffer();\n let percentage = 0;\n let count = 0;\n const loadNext = index => {\n const reader = new FileReader();//\u5f02\u6b65\u8bfb\u53d6\u6587\u4ef6\uff0c\u5728webworker\u4e2d\u4f7f\u7528\n reader.readAsArrayBuffer(fileChunkList[index].file);//\u8bfb\u53d6\u6587\u4ef6\u5b8c\u6210\u540e\uff0c\u5c5e\u6027result\u4fdd\u5b58\u7740\u4e8c\u8fdb\u5236\u6570\u636e\u5bf9\u8c61\n //\u6587\u4ef6\u8bfb\u53d6\u5b8c\u6210\u540e\u89e6\u53d1\n reader.onload = e => {\n //\u9012\u5f52\u8ba1\u6570\u5668\n count++;\n spark.append(e.target.result);//append ArrayBuffer\u6570\u636e\n if (count === fileChunkList.length) {\n self.postMessage({\n percentage: 100,\n hash: spark.end()//\u5b8c\u6210hash\n });\n self.close();//\u5173\u95ed Worker \u7ebf\u7a0b\u3002\n } else {\n percentage += 100 / fileChunkList.length;\n self.postMessage({\n percentage\n });\n loadNext(count);//\u9012\u5f52\u7ee7\u7eed\n }\n };\n };\n loadNext(0);\n};\n```\n\n- \u65ad\u70b9\u7eed\u4f20\u6838\u5fc3\uff1a**\u6587\u4ef6\u5207\u7247\u5b8c\u6210\u4e4b\u540e\uff0c\u5411\u670d\u52a1\u5668\u53d1\u8bf7\u6c42\u68c0\u9a8c\u6587\u4ef6\u5207\u7247\u662f\u5426\u5df2\u7ecf\u4e0a\u4f20**\n\n```js\n async verifyUpload(filename,fileHash){\n const { data } = await this.request({\n url:'http://localhost:3000/verify',//\u9a8c\u8bc1\u63a5\u53e3\n headers:{\n 'content-type':'application/json'\n },\n data:JSON.stringify({\n filename,\n fileHash\n })\n })\n //\u8fd4\u56de\u6570\u636e\n return JSON.parse(data);\n },\n```\n\n- \u4e0a\u4f20\u6587\u4ef6\u5207\u7247\uff1a**\u8fc7\u6ee4\u5df2\u7ecf\u4e0a\u4f20\u7684\u6587\u4ef6+Promise.all\u5e76\u53d1\u8bf7\u6c42**\n\n```js\n//\u4e0a\u4f20\u6587\u4ef6\u5207\u7247\uff0c\u540c\u65f6\u8fc7\u6ee4\u5df2\u7ecf\u4e0a\u4f20\u7684\u5207\u7247\n async uploadChunks(uploadedList = []){\n const requestList = this.data\n .filter(({hash})=>!uploadedList.includes(hash)) //\u8fc7\u6ee4\u5df2\u7ecf\u4e0a\u4f20\u7684chunks\n .map(({chunk,hash,index})=>{\n const formData = new FormData();\n formData.append('chunk',chunk);\n formData.append('hash',hash);\n formData.append(\"filename\", this.container.file.name);\n formData.append(\"fileHash\", this.container.hash);\n return { formData,index }\n })//\u521b\u5efa\u8868\u5355\u6570\u636e\n .map(async ({formData,index})=>\n this.request({\n url:'http://localhost:3000',\n data:formData,\n onProgress : this.createProgressHandler(this.data[index]),\n requestList:this.requestList//\u5c06xhr push\u5230\u8bf7\u6c42\u5217\u8868\n })\n )//\u521b\u5efa\u8bf7\u6c42\u5217\u8868\n //\u5e76\u53d1\u4e0a\u4f20\n await Promise.all(requestList);\n //\u5df2\u7ecf\u4e0a\u4f20\u5207\u7247\u6570\u91cf+\u672c\u6b21\u4e0a\u4f20\u5207\u7247\u6570\u91cf==\u6240\u6709\u5207\u7247\u6570\u91cf\u65f6 \n //\u5207\u7247\u4e0a\u4f20\u5b8c\u6210\uff0c\u7ed9\u670d\u52a1\u5668\u53d1\u9001\u5408\u5e76\u5207\u7247\u8bf7\u6c42\n if(uploadedList.length + requestList.length === this.data.length){\n await this.mergeRequest();\n } \n }\n```\n\n- \u5408\u5e76\u5207\u7247\uff1a\u670d\u52a1\u7aef\u53d1\u9001\u8bf7\u6c42\n\n```js\n// \u901a\u77e5\u670d\u52a1\u7aef\u5408\u5e76\u5207\u7247\n async mergeRequest() {\n await this.request({\n url: \"http://localhost:3000/merge\",\n headers: {\n \"content-type\": \"application/json\"\n },\n data: JSON.stringify({\n size: SIZE,\n fileHash: this.container.hash,\n filename: this.container.file.name\n })\n });\n this.$message.success(\"\u4e0a\u4f20\u6210\u529f\");\n this.status = Status.wait;\n },\n```\n\n- **\u7528\u539f\u751fxhr\u8fdb\u884c\u5c01\u88c5http\u8bf7\u6c42**\n\n```js\n// xhr\n request({\n url,\n method = \"post\",\n data,\n headers = {},\n onProgress = e => e,\n requestList\n }) {\n return new Promise(resolve => {\n const xhr = new XMLHttpRequest();\n xhr.upload.onprogress = onProgress;\n xhr.open(method, url);\n Object.keys(headers).forEach(key =>\n xhr.setRequestHeader(key, headers[key])\n );\n xhr.send(data);\n xhr.onload = e => {\n // \u5c06\u8bf7\u6c42\u6210\u529f\u7684 xhr \u4ece\u5217\u8868\u4e2d\u5220\u9664\n if (requestList) {\n const xhrIndex = requestList.findIndex(item => item === xhr);\n requestList.splice(xhrIndex, 1);\n }\n resolve({\n data: e.target.response\n });\n };\n // \u66b4\u9732\u5f53\u524d xhr \u7ed9\u5916\u90e8\n requestList?.push(xhr);\n });\n }\n```\n\n#### \u4e8c\u3001\u670d\u52a1\u7aef\n\n**1\uff09\u5f00\u542f\u670d\u52a1\uff1a\u672a\u4f7f\u7528node\u6846\u67b6\uff0c\u539f\u751f\u5229\u7528http\u6a21\u5757**\n\n```js\nconst Controller = require(\"./controller\");\nconst http = require(\"http\");\nconst server = http.createServer();\n\nconst controller = new Controller();\n\nserver.on(\"request\", async (req, res) => {\n //\u8bbe\u7f6e\u54cd\u5e94\u5934\uff0c\u5141\u8bb8\u8de8\u57df\n res.setHeader(\"Access-Control-Allow-Origin\", \"*\");\n res.setHeader(\"Access-Control-Allow-Headers\", \"*\");\n if (req.method === \"OPTIONS\") {\n res.status = 200;\n res.end();\n return;\n }\n //\u5207\u7247\u9a8c\u8bc1\n if (req.url === \"/verify\") {\n console.log(req);\n await controller.handleVerifyUpload(req, res);\n return;\n }\n//\u5207\u7247\u5408\u5e76\n if (req.url === \"/merge\") {\n await controller.handleMerge(req, res);\n return;\n }\n//\u5207\u7247\u63d0\u4ea4\n if (req.url === \"/\") {\n await controller.handleFormData(req, res);\n }\n});\n\nserver.listen(3000, () => console.log(\"\u6b63\u5728\u76d1\u542c 3000 \u7aef\u53e3\"));\n```\n\n**2\uff09controller.js**\n\n\u5408\u5e76\u5207\u7247\u7684\u65b9\u5f0f\uff1a**\u4f7f\u7528stream pipe\u65b9\u5f0f\uff0c\u8282\u7701\u5185\u5b58\uff0c\u8fb9\u8bfb\u8fb9\u5199\u5165\uff0c\u5360\u7528\u5185\u5b58\u66f4\u5c0f\uff0c\u6548\u7387\u66f4\u9ad8**\n\n```js\nconst multiparty = require(\"multiparty\");//\u89e3\u6790\u6587\u4ef6\u4e0a\u4f20\nconst path = require(\"path\");\nconst fse = require(\"fs-extra\");//fs\u6a21\u5757\u62d3\u5c55\n\nconst extractExt = filename =>\nfilename&&filename.slice(filename.lastIndexOf(\".\"), filename.length); // \u63d0\u53d6\u540e\u7f00\u540d\nconst UPLOAD_DIR = path.resolve(__dirname, \"..\", \"target\"); // \u5927\u6587\u4ef6\u5b58\u50a8\u76ee\u5f55\n\n//\u4f7f\u7528stream pipe\u65b9\u5f0f\uff0c\u5408\u5e76\u5207\u7247\nconst pipeStream = (path, writeStream) =>\n new Promise(resolve => {\n const readStream = fse.createReadStream(path);\n readStream.on(\"end\", () => {\n fse.unlinkSync(path);\n resolve();\n });\n readStream.pipe(writeStream);\n });\n\n// \u5408\u5e76\u5207\u7247\nconst mergeFileChunk = async (filePath, fileHash, size) => {\n const chunkDir = path.resolve(UPLOAD_DIR, fileHash);\n const chunkPaths = await fse.readdir(chunkDir);\n // \u6839\u636e\u5207\u7247\u4e0b\u6807\u8fdb\u884c\u6392\u5e8f\n // \u5426\u5219\u76f4\u63a5\u8bfb\u53d6\u76ee\u5f55\u7684\u83b7\u5f97\u7684\u987a\u5e8f\u53ef\u80fd\u4f1a\u9519\u4e71\n chunkPaths.sort((a, b) => a.split(\"-\")[1] - b.split(\"-\")[1]);\n await Promise.all(\n chunkPaths.map((chunkPath, index) =>\n pipeStream(\n path.resolve(chunkDir, chunkPath),\n // \u6307\u5b9a\u4f4d\u7f6e\u521b\u5efa\u53ef\u5199\u6d41\n fse.createWriteStream(filePath, {\n start: index * size,\n end: (index + 1) * size\n })\n )\n )\n );\n fse.rmdirSync(chunkDir); // \u5408\u5e76\u540e\u5220\u9664\u4fdd\u5b58\u5207\u7247\u7684\u76ee\u5f55\n};\n\nconst resolvePost = req =>\n new Promise(resolve => {\n let chunk = \"\";\n req.on(\"data\", data => {\n chunk += data;\n });\n req.on(\"end\", () => {\n resolve(JSON.parse(chunk));\n });\n });\n\n// \u8fd4\u56de\u5df2\u7ecf\u4e0a\u4f20\u5207\u7247\u540d\nconst createUploadedList = async fileHash =>\n fse.existsSync(path.resolve(UPLOAD_DIR, fileHash))\n ? await fse.readdir(path.resolve(UPLOAD_DIR, fileHash))\n : [];\n\nmodule.exports = class {\n // \u5408\u5e76\u5207\u7247\n async handleMerge(req, res) {\n const data = await resolvePost(req);\n const { fileHash, filename, size } = data;\n const ext = extractExt(filename);\n const filePath = path.resolve(UPLOAD_DIR, `${fileHash}${ext}`);\n await mergeFileChunk(filePath, fileHash, size);\n res.end(\n JSON.stringify({\n code: 0,\n message: \"file merged success\"\n })\n );\n }\n // \u5904\u7406\u5207\u7247\n async handleFormData(req, res) {\n const multipart = new multiparty.Form();\n multipart.parse(req, async (err, fields, files) => {\n if (err) {\n console.error(err);\n res.status = 500;\n res.end(\"process file chunk failed\");\n return;\n }\n const [chunk] = files.chunk;\n const [hash] = fields.hash;\n const [fileHash] = fields.fileHash;\n const [filename] = fields.filename;\n const filePath = path.resolve(\n UPLOAD_DIR,\n `${fileHash}${extractExt(filename)}`\n );\n const chunkDir = path.resolve(UPLOAD_DIR, fileHash);\n\n // \u6587\u4ef6\u5b58\u5728\u76f4\u63a5\u8fd4\u56de\n if (fse.existsSync(filePath)) {\n res.end(\"file exist\");\n return;\n }\n\n // \u5207\u7247\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u521b\u5efa\u5207\u7247\u76ee\u5f55\n if (!fse.existsSync(chunkDir)) {\n await fse.mkdirs(chunkDir);\n }\n await fse.move(chunk.path, path.resolve(chunkDir, hash));\n res.end(\"received file chunk\");\n });\n }\n // \u9a8c\u8bc1\u662f\u5426\u5df2\u4e0a\u4f20/\u8fd4\u56de\u5df2\u4e0a\u4f20\u5207\u7247\u4e0b\u6807\n async handleVerifyUpload(req, res) {\n const data = await resolvePost(req);\n console.log(data);\n const { fileHash, filename } = data;\n const ext = extractExt(filename);\n const filePath = path.resolve(UPLOAD_DIR, `${fileHash}${ext}`);\n if (fse.existsSync(filePath)) {\n res.end(\n JSON.stringify({\n shouldUpload: false\n })\n );\n } else {\n res.end(\n JSON.stringify({\n shouldUpload: true,\n uploadedList: await createUploadedList(fileHash)\n })\n );\n }\n }\n};\n```\n", "answer": "", "biz_type": 1, "qid": 37, "subject": "", "title": "\u6587\u4ef6\u4e0a\u4f20\u5982\u4f55\u505a\u65ad\u70b9\u7eed\u4f20"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": [], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 5, "qid": 41, "qtype": "short_answer", "short_answer": {"analysis": "### \u7b54\u6848\n\n```js\nfunction checkHtml (data) {\n let tag = ['a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base', 'bdi', 'bdo', 'blockquote', 'body', 'button', 'canvas', 'caption', 'cite', 'code', 'col', 'colgroup', 'datalist', 'dd', 'del', 'dfn', 'dialog', 'div', 'dl', 'dt', 'em', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'frame', 'head', 'header', 'hgroup', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'iframe', 'ins', 'label', 'legend', 'li', 'map', 'mark', 'menu', 'meter', 'nav', 'noscript', 'object', 'ol', 'outgroup', 'option', 'output', 'p', 'pre', 'html', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select', 'small', 'span', 'strike', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'u', 'ul', 'var', 'video', 'wbr']\n //\u6e05\u9664\u6807\u7b7e\u4e2d\u5185\u5bb9\u3001\u6362\u884c\u7b26\n data = data.replace(/.*?(<.*>).*/g, '$1').replace(/[\\r\\n]/g, '').replace(/\\s+.*?>/g, '>')\n //\u6392\u9664\u65e0\u5185\u5bb9\u5143\u7d20\n data = data.replace(/<(img|br|hr|input|link|meta|area|base|col|command|embed|keygen|param|source|track|wbr).*?>/g, '')\n //\u6e05\u9664\u975e\u6807\u7b7e\u5143\u7d20\uff0c\u66ff\u6362\u6807\u7b7e\u4e3a()\u7684\u5f62\u5f0f\uff0c\u4f8b\u5982\uff1a<div>\u66ff\u6362\u4e3a(\u3001</div>\u66ff\u6362\u4e3a)\n data = data.replace(/>.*?</g, '><').replace(/<[^\\/].*?>/g, '(').replace(/<\\/.*?[^<]>/g, ')')\n //\u5224\u65ad()\u662f\u5426\u4e3a\u5076\u6570\n if (data.length % 2 != 0) {\n return false\n }\n //\u5faa\u73af\u5220\u9664()\u76f4\u81f3\u6ca1\u6709()\u6216\u8005\u4e3a\u7a7a\n while (data.length) {\n let temp = data\n let i = 0\n while (i < tag.length) {\n let key = '<' + tag[i] + '></' + tag[i] + '>'\n data = data.replace(new RegExp(key, 'g'), '')\n i++\n }\n if (data == temp) {\n return false\n }\n }\n return true\n}\n\ncheckHtml(\"<div></div>\") // true\ncheckHtml(\"<div>\") // false\ncheckHtml(\"<br>\") // true\ncheckHtml(\"</img>\") // false\ncheckHtml(\"<img/>\") // true\ncheckHtml(\"<img>\") // true\n```\n", "answer": "", "biz_type": 1, "qid": 41, "subject": "", "title": "\u5b9e\u73b0\u4e00\u4e2a\u65b9\u6cd5\u5224\u65ad html \u4e2d\u7684\u6807\u7b7e\u662f\u5426\u95ed\u5408"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9177\u5bb6\u4e50"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 5, "qid": 46, "qtype": "short_answer", "short_answer": {"analysis": "### \u7b54\u6848\n\n1. \u7bad\u5934\u51fd\u6570\u6ca1\u6709\u81ea\u5df1\u7684 this,\u53ea\u80fd\u901a\u8fc7\u4f5c\u7528\u57df\u94fe\u6765\u5411\u4e0a\u67e5\u627e\u79bb\u81ea\u5df1\u6700\u8fd1\u7684\u90a3\u4e2a\u51fd\u6570\u7684 this\n2. \u7bad\u5934\u51fd\u6570\u4e0d\u80fd\u4f5c\u4e3a constructor\uff0c\u56e0\u6b64\u4e0d\u80fd\u901a\u8fc7 new \u6765\u8c03\u7528\uff0c\u6240\u4ee5\u4ed6\u5e76\u6ca1\u6709`new.target`\u8fd9\u4e2a\u5c5e\u6027\n3. \u7bad\u5934\u51fd\u6570\u6ca1\u6709 argument \u5c5e\u6027 ,\u53ef\u4ee5\u901a\u8fc7 rest \u53ef\u4ee5\u83b7\u53d6\n4. \u7bad\u5934\u51fd\u6570\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528 call \u548c apply\uff0cbind \u6765\u6539\u53d8 this\n5. \u7bad\u5934\u51fd\u6570\u4e0d\u80fd\u4f7f\u7528 yield,\u4e0d\u80fd\u7528\u6765\u505a\u4e3a generator \u51fd\u6570\n6. \u7bad\u5934\u51fd\u6570\u8bed\u6cd5\u6bd4\u666e\u901a\u51fd\u6570\u66f4\u52a0\u7b80\u6d01\n\n> ES6 \u4e3a new \u547d\u4ee4\u5f15\u5165\u4e86\u4e00\u4e2a new.target \u5c5e\u6027\uff0c\u8be5\u5c5e\u6027\u4e00\u822c\u7528\u5728\u6784\u9020\u51fd\u6570\u4e4b\u4e2d\uff0c\u8fd4\u56de new \u547d\u4ee4\u4f5c\u7528\u4e8e\u7684\u90a3\u4e2a\u6784\u9020\u51fd\u6570\u3002\u5982\u679c\u6784\u9020\u51fd\u6570\u4e0d\u662f\u901a\u8fc7 new \u547d\u4ee4\u6216 Reflect.construct()\u8c03\u7528\u7684\uff0cnew.target \u4f1a\u8fd4\u56de undefined\uff0c\u56e0\u6b64\u8fd9\u4e2a\u5c5e\u6027\u53ef\u4ee5\u7528\u6765\u786e\u5b9a\u6784\u9020\u51fd\u6570\u662f\u600e\u4e48\u8c03\u7528\u7684\uff09\u3002\u5305\u62ec super \u4e5f\u4e0d\u5b58\u5728\u4ee5\u53ca\u539f\u578b prototype=>\u56e0\u4e3a\u5728\u6267\u884c new \u7684\u65f6\u5019\u9700\u8981\u5c06\u51fd\u6570\u7684\u539f\u578b\u8d4b\u503c\u7ed9\u5b9e\u4f8b\u5bf9\u8c61\u7684\u539f\u578b\u5c5e\u6027\u3002\n", "answer": "", "biz_type": 1, "qid": 46, "subject": "", "title": "\u7bad\u5934\u51fd\u6570\u548c\u666e\u901a\u51fd\u6570\u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6ef4\u6ef4", "\u5feb\u624b", "\u643a\u7a0b"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 7, "qid": 31, "qtype": "short_answer", "short_answer": {"analysis": "### \u7b54\u6848\n\n**1\uff09\u65b9\u5f0f\u4e00**\n\n```js\nfunction flatten(arr){\n const stack = [...arr];\n const res = [];\n while (stack.length) {\n const next = stack.pop();\n if(Array.isArray(next)){\n stack.push(...next);\n }else{\n res.push(next);\n }\n }\n return res.reverse();\n}\n```\n\n**2\uff09\u65b9\u5f0f\u4e8c**\n\n```js\n//flatten(arr,[depth]),depth\u6307\u5b9a\u5c55\u5f00\u5d4c\u5957\u6570\u7ec4\u7684\u6df1\u5ea6,\u9ed8\u8ba4\u4e3a1\nfunction flatten(arr, depth = 1){\n if(depth > 0){\n return arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flattenArray(val, depth - 1) : val), []);\n } else {\n return arr.slice();\n }\n}\n```\n\n**3\uff09\u65b9\u5f0f\u4e09**\n\n```js\n// \u672a\u6392\u9664null\u9879\nvar arr = [1, 2, [3, 4, 5, [6, 7, 8, [9, , 0]]]]\nArray.prototype.flat = function (deep = 1) {\n const source = this\n return deep > 0 ?\n source.reduce(\n (res, val) => val === null ? \"\" : res.concat(\n Array.isArray(val) ? val.flat(deep - 1) : val)\n , [])\n : source.slice();\n\n}\nconsole.log(arr.flat(3))\n```\n\n**4\uff09\u65b9\u5f0f\u56db**\n\n```js\n// \u4f7f\u7528forEach \u81ea\u52a8\u8fc7\u6ee4\u7a7a\u9879\nvar arr = [1, 2, [3, 4, 5, [6, 7, 8, [9, , 0]]]]\nArray.prototype.flat = function (deep = 1) {\n let result = []\n function ergodic(array, deep) {\n array.forEach(element => {\n Array.isArray(element) && deep > 0 ? ergodic(element, deep - 1) : result.push(element)\n });\n }\n ergodic(this, deep)\n return result\n}\nconsole.log(arr.flat(3))\n```\n", "answer": "", "biz_type": 1, "qid": 31, "subject": "", "title": "\u624b\u5199\u5b9e\u73b0 Array.flat()"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u987a\u4e30", "\u65b0\u4e1c\u65b9", "\u9ad8\u5fb7", "\u864e\u6251", "\u5fae\u533b", "\u767e\u5206\u70b9", "\u9177\u72d7", "\u65b0\u4e1c\u65b9"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 8, "qid": 40, "qtype": "short_answer", "short_answer": {"analysis": "### \u7b54\u6848\n\n#### 1.\u6d45\u62f7\u8d1d\n\n\u5bf9\u4e8e\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u6d45\u62f7\u8d1d\u662f\u5bf9\u503c\u7684\u590d\u5236\uff0c\u5bf9\u4e8e\u5bf9\u8c61\u6765\u8bf4\uff0c\u6d45\u62f7\u8d1d\u662f\u5bf9\u5bf9\u8c61\u5730\u5740\u7684\u590d\u5236, \u4e3e\u4e2a\u4f8b\u5b50\uff0cA,B\u4e24\u4e2a\u5bf9\u8c61\uff0cA=B\uff0c\u6539\u53d8A\u7684\u5c5e\u6027\uff0cB\u4e5f\u76f8\u5bf9\u4f1a\u53d1\u751f\u6539\u53d8\u3002\u56e0\u4e3aA,B\u6307\u5411\u540c\u4e00\u4e2a\u5730\u5740\u3002\n\n#### 2.\u6df1\u62f7\u8d1d\n\n\u6df1\u62f7\u8d1d\u5f00\u8f9f\u4e00\u4e2a\u65b0\u7684\u6808\uff0c\u4e24\u4e2a\u5bf9\u8c61\u5bf9\u5e94\u4e24\u4e2a\u4e0d\u540c\u7684\u5730\u5740\uff0c\u4fee\u6539\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u4e0d\u4f1a\u6539\u53d8\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\n\n#### 3.\u4ee3\u7801\u5b9e\u73b0\u6df1\u62f7\u8d1d\n\n**1\uff09\u65b9\u5f0f\u4e00-JSON.parse(JSON.stringfy(source))**\n\n\u5229\u7528\u73b0\u4ee3\u6d4f\u89c8\u5668\u652f\u6301\u7684JSON\u5bf9\u8c61\u505a\u4e00\u6b21\u4e2d\u8f6c\uff0c\u5b9e\u73b0\u6df1\u5ea6\u514b\u9686\n\n\u4f18\u70b9\uff1a\u7b80\u5355\u4fbf\u6377\n\n\u7f3a\u70b9\uff1a\n\n- undefined\u3001\u51fd\u6570\u3001symble\u503c\uff0c\u5728\u5e8f\u5217\u5316\u8fc7\u7a0b\u4e2d\u4f1a\u88ab\u5ffd\u7565\n- \u4e0d\u80fd\u5904\u7406 BigInt \u7c7b\u578b\u7684\u6570\u636e\u548c\u5faa\u73af\u5f15\u7528\uff0c\u4f1a\u62a5\u9519\n- Map, Set, RegExp \u7c7b\u578b\u7684\u6570\u636e\uff0c\u4f1a\u5f15\u7528\u4e22\u5931\uff0c\u53d8\u6210\u7a7a\u503c\n- Date \u7c7b\u578b\u7684\u6570\u636e\u4f1a\u88ab\u5f53\u505a\u5b57\u7b26\u4e32\u5904\u7406\n- NaN \u548c Infinity \u683c\u5f0f\u7684\u6570\u503c\u53ca null \u90fd\u4f1a\u88ab\u5f53\u505a null\u3002\n- \u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5305\u62ec Map/Set/WeakMap/WeakSet\uff0c\u4ec5\u4f1a\u5e8f\u5217\u5316\u53ef\u679a\u4e3e\u7684\u5c5e\u6027\n\n```js\nfunction deepClone(obj) {\n var _tmp,result;\n _tmp = JSON.stringify(obj);\n result = JSON.parse(_tmp);\n return result;\n}\n```\n\n**2\uff09\u65b9\u5f0f\u4e8c-\u786e\u5b9a\u53c2\u6570\u7c7b\u578b\u4e3aobject**\n\n\u786e\u5b9a\u53c2\u6570\u7c7b\u578b\u4e3aobject \uff08\u8fd9\u91cc\u4ec5\u6307object literal\u3001Array literal\uff09\u540e\uff0c\u590d\u5236\u6e90\u5bf9\u8c61\u7684\u952e/\u503c\u5230\u76ee\u6807\u5bf9\u8c61\uff0c\u5426\u5219\u76f4\u63a5\u8fd4\u56de\u6e90\u5bf9\u8c61\u3002\n\n```js\nfunction deepClone(obj){\n var result = typeof obj.splice === 'function'?[]:{},\n key;\n if (obj && typeof obj === 'object'){\n for (key in obj ){\n if (obj[key] && typeof obj[key] === 'object'){\n result[key] = deepClone(obj[key]);//\u5982\u679c\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u4e3aobject\u7684\u65f6\u5019\uff0c\u9012\u5f52\u8c03\u7528deepClone\uff0c\u5373\u518d\u628a\u67d0\u4e2a\u503c\u5bf9\u8c61\u590d\u5236\u4e00\u4efd\u5230\u65b0\u7684\u5bf9\u8c61\u7684\u5bf9\u5e94\u503c\u4e2d\n }else{\n result[key] = obj[key];//\u5982\u679c\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u4e0d\u4e3aobject\u7684\u65f6\u5019\uff0c\u76f4\u63a5\u590d\u5236\u53c2\u6570\u5bf9\u8c61\u7684\u6bcf\u4e00\u4e2a\u952e/\u503c\u5230\u65b0\u5bf9\u8c61\u5bf9\u5e94\u7684\u952e/\u503c\u4e2d\n }\n }\n return result;\n }\n return obj;\n}\n```\n\n**3\uff09\u65b9\u5f0f\u4e09-Reflect\u4ee3\u7406\u6cd5**\n\n```js\nfunction deepClone(obj) {\n if (!isObject(obj)) {\n throw new Error('obj \u4e0d\u662f\u4e00\u4e2a\u5bf9\u8c61\uff01')\n }\n let isArray = Array.isArray(obj)\n let cloneObj = isArray ? [...obj] : { ...obj }\n Reflect.ownKeys(cloneObj).forEach(key => {\n cloneObj[key] = isObject(obj[key]) ? deepClone(obj[key]) : obj[key]\n })\n return cloneObj\n}\n```\n\n**4\uff09\u65b9\u5f0f\u56db-\u7ec8\u6781\u7248**\n\n> \u5faa\u73af\u5f15\u7528\u7684\u95ee\u9898\uff0c\u5373obj.x = obj\n> \u7528\u4e00\u4e2aweakMap<\u539f\u5bf9\u8c61\u5f15\u7528,\u65b0\u5bf9\u8c61\u5f15\u7528>\u4fdd\u5b58\u5df2\u7ecf\u521b\u5efa\u7684\u5bf9\u8c61\uff0c\u5982\u679c\u518d\u4e0b\u6b21\u9012\u5f52\u4e2d\u5982\u679c\u5f53\u524d\u7b49\u4e8e\u539f\u5bf9\u8c61\u5f15\u7528\uff0c\u90a3\u4e48\u76f4\u63a5\u8fd4\u56de\u65b0\u521b\u5efa\u5bf9\u8c61\u7684\u5f15\u7528\n\n```js\nfunction isObject(obj) {\n return (typeof obj === 'object' || typeof obj === 'function') && obj !== null\n}\nfunction isFunc(obj) {\n return typeof obj === 'function'\n}\nfunction isArray(obj) {\n return Array.isArray(obj)\n}\nfunction isDate(obj) {\n return Object.prototype.toString.call(obj) === '[object Date]'\n}\nfunction isMap(obj) {\n return Object.prototype.toString.call(obj) === '[object Map]'\n}\nfunction isSet(obj) {\n return Object.prototype.toString.call(obj) === '[object Set]'\n}\nfunction isRegExp(obj) {\n return Object.prototype.toString.call(obj) === '[object RegExp]'\n}\n\nfunction deepCopy(obj,weakMap = new WeakMap()) {\n if (!isObject(obj)) return obj\n if (weakMap.get(obj)) return weakMap.get(obj)\n // \u5982\u679c\u662f\u51fd\u6570\n if (isFunc(obj)) {\n let result = null\n // \u83b7\u5f97\u51fd\u6570\u7684\u4e3b\u4f53\n const bodyReg = /(?<={)(.|\\n)+(?=})/m;\n // \u83b7\u5f97\u53c2\u6570\n const paramReg = /(?<=\\().+(?=\\)\\s+{)/;\n const funcString = obj.toString();\n // \u5224\u65ad\u662f\u5426\u662f\u7bad\u5934\u51fd\u6570\n if (obj.prototype) {\n const param = paramReg.exec(funcString);\n const body = bodyReg.exec(funcString);\n if (body) {\n if (param) {\n const paramArr = param[0].split(',');\n result = new Function(...paramArr, body[0]);\n } else {\n result = new Function(body[0]);\n }\n }\n } else {\n result = eval(funcString);\n }\n weakMap.set(obj,result)\n return result\n }\n\n // \u5982\u679c\u662f\u6570\u7ec4\n if (Array.isArray(obj)) {\n let result = []\n for (let val of obj) {\n result.push(deepCopy(val, weakMap))\n }\n weakMap.set(obj,result)\n return result\n }\n // \u5982\u679c\u662fDate\n if (isDate(obj)) {\n let result = new obj.constructor(obj)\n weakMap.set(obj,result)\n return result\n }\n // \u5982\u679c\u662fmap\n if (isSet(obj)) {\n let result = new Set()\n obj.forEach((val)=> {\n result.add(deepCopy(val, weakMap))\n })\n weakMap.set(obj,result)\n return result\n }\n // \u5982\u679c\u662fset\n if (isMap(obj)) {\n let result = new Map()\n obj.forEach((val, key) => {\n result.set(key, deepCopy(key, weakMap))\n })\n weakMap.set(obj,result)\n return result\n }\n // \u5982\u679c\u662f\u6b63\u5219\n if (isRegExp(obj)) {\n const reFlags = /\\w*$/;\n const result = new obj.constructor(obj.source, reFlags.exec(obj));\n result.lastIndex = obj.lastIndex;\n weakMap.set(obj,result)\n return result;\n }\n let result = {}\n weakMap.set(obj,result)\n // \u8003\u8651symbol\u7c7b\u578b\u7684\u5c5e\u6027\u540d\n let symbols = Object.getOwnPropertySymbols(obj)\n if(symbols.length > 0) {\n for(let key of symbols) {\n let val = obj[key]\n result[key] = isObject(val) ? deepCopy(val, weakMap) : val\n }\n }\n // \u975esymbol\u7c7b\u578b\u5c5e\u6027\u540d\n for (let key in obj) {\n if (obj.hasOwnProperty(key)) {\n let val = obj[key]\n result[key] = isObject(val) ? deepCopy(val, weakMap) : val\n }\n }\n return result\n}\n\nvar map = new Map()\nmap.set(1,1)\nmap.set(2,2)\nvar obj = {\n a: 1,\n b: '1',\n c: Symbol(),\n d: undefined,\n e: null,\n f: true,\n g: {\n g1: 1,\n g2: '2',\n g3: undefined\n },\n [Symbol()]: 'symbol',\n h: function (a) {\n console.log(a)\n },\n i: [1,2,3],\n j: new Date(),\n k: new Set([1,2,3,4]),\n l: map,\n m: /\\w*$/g,\n}\nobj.x = obj.i\n\nvar deepObj = deepCopy(obj)\nconsole.log(deepObj.x === deepObj.i) // true\nconsole.log(deepObj)\n```\n", "answer": "", "biz_type": 1, "qid": 40, "subject": "", "title": "\u4ec0\u4e48\u662f\u6df1\u62f7\u8d1d\uff0c\u548c\u6d45\u62f7\u8d1d\u6709\u4ec0\u4e48\u533a\u522b\uff0c\u52a8\u624b\u5b9e\u73b0\u4e00\u4e2a\u6df1\u62f7\u8d1d"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u987a\u4e30"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 8, "qid": 121, "qtype": "short_answer", "short_answer": {"analysis": "### \u5b9e\u73b0\u4ee3\u7801\n\n```js\nconst getMonths = (startDateStr, endDateStr) => {\n let startTime = getDate(startDateStr).getTime()\n const endTime = getDate(endDateStr).getTime()\n const result = []\n while (startTime < endTime) {\n let curDate = new Date(startTime)\n result.push(formatDate(curDate))\n curDate.setMonth(curDate.getMonth() + 1)\n startTime = curDate.getTime()\n }\n return result.slice(1)\n}\nconst getDate = (dateStr) => {\n const [year, month] = dateStr.split('-')\n return new Date(year, month - 1)\n}\nconst formatDate = (date) => {\n return `${date.getFullYear()}-${(date.getMonth()+1).toString().padStart(2, '0')}`\n}\nconsole.log(getMonths('2018-08', '2018-12'))\n```", "answer": "", "biz_type": 1, "qid": 121, "subject": "```js\n// \u8f93\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32 2018-08 2018-12\n// \u8f93\u51fa\u4ed6\u4eec\u4e2d\u95f4\u7684\u6708\u4efd [2018-10, 2018-11]\n```\n", "title": "\u7ed9\u5b9a\u8d77\u6b62\u65e5\u671f\uff0c\u8fd4\u56de\u4e2d\u95f4\u7684\u6240\u6709\u6708\u4efd"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u987a\u4e30"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 10, "qid": 128, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n```js\nfunction getAllKey(obj) {\n if (typeof obj !== 'object') {\n return\n }\n let keys = []\n\n for (let index in obj) {\n if (obj[index] instanceof Object && !Array.isArray(obj[index])) {\n keys = keys.concat(getAllKey(obj[index]))\n } else {\n keys.push(index)\n }\n }\n return keys\n}\ngetAllKey(obj)\n```", "answer": "", "biz_type": 1, "qid": 128, "subject": "```js\nvar obj = {\n a: \"12\",\n b: \"23\",\n first: {\n c: \"34\",\n d: \"45\",\n second: { 3: \"56\", f: \"67\", three: { g: \"78\", h: \"89\", i: \"90\" } },\n },\n};\n// => [a,b,c,d,e,f,g,h,i]\n```\n", "title": "\u8bf7\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u8f93\u51fa\u51fa\u591a\u7ea7\u5d4c\u5957\u7ed3\u6784\u7684 Object \u7684\u6240\u6709 key \u503c"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 7, "qid": 62, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n`Number.isInteger()` \u65b9\u6cd5\u7528\u6765\u5224\u65ad\u7ed9\u5b9a\u7684\u53c2\u6570\u662f\u5426\u4e3a\u6574\u6570\n\n**1\uff09\u4ee3\u7801\u5b9e\u73b0\u4e00**\n\n- `isFinite` \u65b9\u6cd5\u68c0\u6d4b\u5b83\u53c2\u6570\u7684\u6570\u503c\u3002\u5982\u679c\u53c2\u6570\u662f NaN\uff0cInfinity\u6216\u8005-Infinity\uff0c\u4f1a\u8fd4\u56defalse\uff0c\u5176\u4ed6\u8fd4\u56de true\u3002\n- `Number.isInteger(Infinity) === false`, \u800c`typeof Infinity` \u548c `Math.floor(Infinity)`\u5f97\u5230\u7684\u5747\u4e3atrue\n- \u53d6\u6574\uff1a`Math.floor`\u3001`Math.ceil`\u3001`Math.round`\u5747\u53ef\n\n```js\nNumber.isInteger = function(value) {\n return typeof value === \"number\" && \n isFinite(value) && \n Math.floor(value) === value;\n};\n```\n\n**2\uff09\u4ee3\u7801\u5b9e\u73b0\u4e8c**\n\n- \u5f02\u6216\u8fd0\u7b97\n\n```js\nfunction isInteger(x) {\n return typeof value === \"number\" && \n isFinite(value) && x ^ 0 === x\n}\n```\n\n**3\uff09\u4ee3\u7801\u5b9e\u73b0\u4e09**\n\n- \u53d6\u4f59\n\n```js\nfunction isInteger(x) {\n return typeof value === \"number\" && \n isFinite(value) && (x % 1 === 0)\n}\n```\n\n", "answer": "", "biz_type": 1, "qid": 62, "subject": "", "title": "es5 \u5b9e\u73b0 isInteger"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 11, "qid": 79, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n**1\uff09\u66b4\u529b\u53cc\u91cd\u5faa\u73af**\n\n```js\nfunction findMaxRight(array) {\n let result = []\n for (let i = 0; i < array.length - 1; i++) {\n for (let j = i + 1; j < array.length; j++) {\n if (array[j] > array[i]) {\n result[i] = array[j]\n break\n } else {\n result[i] = -1\n }\n }\n }\n result[result.length] = -1\n return result\n}\n```\n\n**2\uff09\u5229\u7528\u6808\u7684\u7279\u6027**\n\n```js\nfunction findMaxRightWithStack(array) {\n const size = array.length\n let indexArr = [0]\n let result = []\n let index = 1\n while (index < size) {\n if (indexArr.length > 0 && array[indexArr[indexArr.length - 1]] < array[index]) {\n result[indexArr[indexArr.length - 1]] = array[index]\n indexArr.pop()\n } else {\n indexArr.push(index)\n index++\n }\n }\n indexArr.forEach((item) => {\n result[item] = -1\n })\n return result\n}\n```\n\n**3\uff09\u5355\u8c03\u9012\u51cf\u6808, \u53cd\u5411\u904d\u5386**\n\n```js\nconst firstBiggerItem = (T) => {\n const res = new Array(T.length).fill(-1);\n const stack = [];\n for (let i = T.length - 1; i >= 0; i--) {\n while (stack.length && T[i] >= T[stack[stack.length - 1]]) {\n stack.pop();\n }\n if (stack.length && T[i] < T[stack[stack.length - 1]]) {\n res[i] = T[stack[stack.length - 1]];\n }\n stack.push(i);\n }\n return res;\n};\n// test\nvar T = [2, 6, 3, 8, 10, 9];\nconsole.log(firstBiggerItem(T));\n```\n\n", "answer": "", "biz_type": 1, "qid": 79, "subject": "```js\n/*\n *\u793a\u4f8b\uff1a\n *\u7ed9\u5b9a\u6570\u7ec4\uff1a[2,6,3,8,10,9]\n *\u8fd4\u56de\u6570\u7ec4\uff1a[6,8,8,10,-1,-1]\n */\n```\n", "title": "\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4\uff0c\u6309\u627e\u5230\u6bcf\u4e2a\u5143\u7d20\u53f3\u4fa7\u7b2c\u4e00\u4e2a\u6bd4\u5b83\u5927\u7684\u6570\u5b57\uff0c\u6ca1\u6709\u7684\u8bdd\u8fd4\u56de-1 \u89c4\u5219\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 6, "qid": 135, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n#### 1.\u7b80\u5355\u5b9e\u73b0\u6f14\u793a\n\n```html\n<style>\n .box {\n width: 800px;\n height: 400px;\n background: rgba(0, 0, 0, 0.1);\n margin: 100px auto 0;\n overflow: hidden;\n }\n\n .track {\n height: 40px;\n line-height: 40px;\n border: 2px solid rgba(0, 0, 0, 0.3);\n margin-bottom: 5px;\n }\n\n .child {\n width: 80px;\n line-height: 20px;\n margin-bottom: 10px;\n text-shadow: 2px 2px 2px rgba(0, 0, 0, 0.1)\n }\n\n .child-1 {\n color: brown;\n text-shadow: 2px 2px 3px rgb(248, 81, 20);\n transform: translateX(1000px);\n animation: scrollTo linear 4s infinite;\n }\n\n .child-2 {\n color: rgb(127, 197, 35);\n text-shadow: 2px 2px 3px rgb(173, 255, 80);\n transform: translateX(1050px);\n animation: scrollTo linear 7s infinite;\n }\n\n .child-3 {\n color: coral;\n text-shadow: 2px 2px 3px coral;\n transform: translateX(800px);\n animation: scrollTo linear 5s infinite;\n }\n\n @keyframes scrollTo {\n to {\n transform: translateX(-100px);\n }\n }\n </style>\n\n <div class=\"box\">//\u5c4f\u5e55\n <div class=\"track\">//\u5f39\u5e55\u8f68\u9053\n <div class=\"child child-1\">\u6211\u662f\u5f39\u5e55</div>\n </div>\n <div class=\"track\">\n <div class=\"child child-2\">\u6211\u662f\u5f39\u5e55</div>\n </div>\n <div class=\"track\">\n <div class=\"child child-3\">\u6211\u662f\u5f39\u5e55</div>\n </div>\n </div>\n```\n\n#### 2.\u53ef\u52a8\u6001\u6dfb\u52a0\u5f39\u5e55\n\n```html\n<style>\n * {\n margin: 0;\n padding: 0;\n }\n\n html,\n body {\n /* \u81ea\u9002\u5e94\u9ad8\u5ea6 */\n width: 100%;\n height: 100%;\n }\n\n #main {\n width: 100%;\n height: 100%;\n /*\u80cc\u666f\u8272\u7ebf\u6027\u53d8\u5316*/\n background: -webkit-gradient(linear,\n 0% 0%,\n 0% 100%,\n from(#add8e6),\n to(#f6f6f8));\n overflow: hidden;\n }\n\n span {\n /*\u5f3a\u5236\u4e0d\u6362\u884c*/\n white-space: nowrap;\n position: absolute;\n }\n\n #mainScreen {\n width: 800px;\n height: 600px;\n margin: 8px auto;\n border: 1px solid gray;\n background-color: white;\n /*\u9690\u85cfspan\u6807\u7b7e\u8d85\u51faoScreen\u5c4f\u5e55\u8303\u56f4\u7684\u5185\u5bb9*/\n overflow: hidden;\n position: relative;\n }\n\n #bottom {\n width: 800px;\n height: 32px;\n margin: 5px auto;\n }\n\n #txt {\n width: 240px;\n height: 30px;\n line-height: 30px;\n font-family: \u5fae\u8f6f\u96c5\u9ed1;\n padding-left: 8px;\n border: 1px solid lightslategrey;\n float: left;\n }\n\n #btn {\n width: 60px;\n height: 30px;\n line-height: 30px;\n margin-left: 30px;\n margin-top: 2px;\n border-radius: 4px;\n float: left;\n }\n </style>\n <div id=\"main\">\n <div id=\"mainScreen\"></div>\n <div id=\"bottom\">\n <input id=\"txt\" type=\"text\" value=\"say some thing...\" />\n <input id=\"btn\" type=\"button\" value=\"Send\" />\n </div>\n </div>\n <script>\n window.onload = function () {\n var oBtn = document.getElementById(\"btn\");\n var oText = document.getElementById(\"txt\");\n var oScreen = document.getElementById(\"mainScreen\");\n oBtn.onclick = sendMessage;\n // \u6bcf\u6b21\u70b9\u51fb\u6e05\u7a7a\u8f93\u5165\u6846\n oText.onclick = function () {\n oText.value = \"\";\n };\n //\u6dfb\u52a0\u56de\u8f66\u63d0\u4ea4\u4e8b\u4ef6\n document.onkeydown = function (evt) {\n var event = evt || window.event; //\u517c\u5bb9IE\n if (event.keyCode == 13) {\n sendMessage();\n }\n };\n\n function sendMessage() {\n //\u5982\u679c\u8f93\u5165\u4e3a\u7a7a\u5219\u5f39\u51fa\u5bf9\u8bdd\u6846\n if (oText.value.trim() == \"\") {\n alert(\"\u8bf7\u6b63\u786e\u8f93\u5165\");\n } else {\n //\u5982\u679c\u6709\u8f93\u5165\u5219\u52a8\u6001\u521b\u5efaspan\u5e76\u5c06\u5185\u5bb9\u6dfb\u52a0\u5230span\u4e2d\uff0c\u7136\u540e\u518d\u5c06span\u6dfb\u52a0\u5230mainScreen\u4e2d\n var oDan1 = document.createElement(\"span\");\n oDan1.innerText = oText.value;\n\n // \u5b9a\u4e49\u968f\u673a\u5b57\u4f53\u5927\u5c0f\n var oFontSize = parseInt(Math.random() * 16 + 16);\n // \u521b\u5efa\u968f\u673a\u989c\u8272\n var oFontColor =\n \"#\" + Math.floor(Math.random() * 16777215).toString(16);\n // \u968f\u673a\u9ad8\u5ea6\n var oMax = oScreen.offsetHeight - oFontSize;\n var oMin = oScreen.offsetTop;\n var oHeight = Math.floor(Math.random() * (oMax - oMin) + oMin);\n\n oDan1.style.color = oFontColor;\n oDan1.style.fontSize = oFontSize + \"px\";\n oDan1.style.marginTop = oHeight + \"px\";\n\n // Move\n var variable = 800; //800\u662fmainScreen\u7684\u5bbd\u5ea6\uff0c\u4e5f\u53ef\u5199\u6210\uff1aoDan1.offsetLeft\n var timer = setInterval(function () {\n oDan1.style.marginLeft = variable + \"px\";\n //\u5982\u679c\u6ca1\u6709\u8d85\u51fa\u8fb9\u754c\u5c31\u5c06span\u52a8\u6001\u6dfb\u52a0\u5230oScreen\n if (variable > -oDan1.offsetWidth) {\n variable -= 2;\n oScreen.appendChild(oDan1);\n } else {\n clearInterval(timer);\n // \u5f53\u663e\u793a\u8d85\u51fa\u8303\u56f4\u5c31\u5220\u9664\u8282\u70b9\uff0c\u8fd9\u91cc\u6211\u4e4b\u524d\u7528display:none\u4e0d\u7ba1\u7528\n oDan1.parentNode.removeChild(oDan1);\n }\n }, 10);\n }\n }\n };\n </script>\n```", "answer": "", "biz_type": 1, "qid": 135, "subject": "", "title": "\u600e\u6837\u7528 css \u5b9e\u73b0\u4e00\u4e2a\u5f39\u5e55\u7684\u6548\u679c\uff0c\u52a8\u624b\u5b9e\u73b0\u4e00\u4e0b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 11, "qid": 132, "qtype": "short_answer", "short_answer": {"analysis": "### \u4ee3\u7801\u5b9e\u73b0\n\n#### 1.\u5b9e\u73b0\u65b9\u5f0f\u4e00\n\n```js\nfunction repeat (func, times, wait) {\n\tif (typeof\u00a0func\u00a0!==\u00a0'function') return;\n\tif (times <= 0) return;\n\treturn function (value) {\n\t\tlet timesTmp = times\n\t\tlet interval = setInterval(function(){\n\t\t\tfunc(value);\n\t\t\ttimesTmp--;\n\t\t\ttimesTmp === 0 && clearInterval(interval)\n\t\t}, wait)\n\t}\n}\nconst repeatConsole = repeat(console.log, 4, 2000)\nrepeatConsole('helllwww')\n```\n\n#### \u4e8c\u3001\u5b9e\u73b0\u65b9\u5f0f\u4e8c\n\n```js\nfunction repeat (func, times, wait) {\n return function(...args){\n let i = 0;\n let _args = [...args]\n let handle = setInterval(() => {\n i += 1;\n if(i > times){\n clearInterval(handle);\n return;\n }\n func.apply(null, _args);\n },wait);\n }\n}\n```", "answer": "", "biz_type": 1, "qid": 132, "subject": "```js\nfunction repeat(func, times, wait) {\n // TODO\n}\nconst repeatFunc = repeat(alert, 4, 3000);\n// \u8c03\u7528\u8fd9\u4e2a repeatFunc (\"hellworld\")\uff0c\u4f1aalert4\u6b21 helloworld, \u6bcf\u6b21\u95f4\u96943\u79d2\n```\n", "title": "\u52a8\u624b\u5b9e\u73b0\u4e00\u4e2a repeat \u65b9\u6cd5"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f34\u9c7c"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 5, "qid": 82, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001\u5e38\u7528\u7684\u5783\u573e\u56de\u6536\u673a\u5236\n\n#### 1\uff09\u5f15\u7528\u8ba1\u6570\u6cd5 reference counting\n\n1. \u8ddf\u8e2a\u8bb0\u5f55\u6bcf\u4e2a\u503c\u88ab\u5f15\u7528\u7684\u6b21\u6570\n2. \u5f53\u58f0\u660e\u53d8\u91cf\u5e76\u5c06\u4e00\u4e2a\u5f15\u7528\u7c7b\u578b\u7684\u503c\u8d4b\u503c\u7ed9\u8be5\u53d8\u91cf\u65f6\uff0c\u5219\u8fd9\u4e2a\u503c\u7684\u5f15\u7528\u6b21\u6570\u52a01\uff0c\n3. \u540c\u4e00\u503c\u88ab\u8d4b\u4e88\u53e6\u4e00\u4e2a\u53d8\u91cf\uff0c\u8be5\u503c\u7684\u5f15\u7528\u8ba1\u6570\u52a01 \u3002\n4. \u5f53\u5f15\u7528\u8be5\u503c\u7684\u53d8\u91cf\u88ab\u53e6\u4e00\u4e2a\u503c\u6240\u53d6\u4ee3\uff0c\u5219\u5f15\u7528\u8ba1\u6570\u51cf1\uff0c\n5. \u5f53\u8ba1\u6570\u4e3a 0 \u7684\u65f6\u5019\uff0c\u8bf4\u660e\u65e0\u6cd5\u5728\u8bbf\u95ee\u8fd9\u4e2a\u503c\u4e86\uff0c\u7cfb\u7edf\u5c06\u4f1a\u6536\u56de\u8be5\u503c\u6240\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u3002\n6. \u7f3a\u70b9\uff1a\u5faa\u73af\u5f15\u7528\u7684\u65f6\u5019\uff0c\u5f15\u7528\u6b21\u6570\u4e0d\u4e3a0\uff0c\u4e0d\u4f1a\u88ab\u91ca\u653e\uff1b\n\n#### 2\uff09\u6807\u8bb0\u6e05\u9664\u6cd5 mark-sweep\n\n\u7ed9\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u7684\u53d8\u91cf\u90fd\u52a0\u4e0a\u6807\u8bb0\uff0c\u5224\u65ad\u54ea\u4e9b\u53d8\u91cf\u6ca1\u6709\u5728\u6267\u884c\u73af\u5883\u4e2d\u5f15\u7528\uff0c\u8fdb\u884c\u5220\u9664\uff1b\n\n#### 3\uff09\u505c\u6b62\u590d\u5236 stop-copy\n\n\u5185\u5b58\u5206\u4e3a\u4e24\u5757\uff0c\u6b63\u5728\u4f7f\u7528\u7684\u5b58\u6d3b\u5bf9\u8c61\u590d\u5236\u5230\u672a\u88ab\u4f7f\u7528\u7684\u5185\u5b58\u5757\u4e2d\uff0c\u6e05\u9664\u6b63\u5728\u4f7f\u7528\u7684\u5185\u5b58\u5757\u4e2d\u7684\u6240\u6709\u5bf9\u8c61\uff0c\u7136\u540e\u4ea4\u6362\u4e24\u4e2a\u5185\u5b58\u7684\u89d2\u8272\n\n#### 4\uff09\u6807\u8bb0\u538b\u7f29 mark-compact\n\n\u6240\u6709\u53ef\u8fbe\u5bf9\u8c61\u505a\u4e00\u6b21\u6807\u8bb0\uff0c\u6240\u6709\u5b58\u6d3b\u5bf9\u8c61\u538b\u7f29\u5230\u5185\u5b58\u7684\u4e00\u7aef\uff0c\u51cf\u5c11\u5185\u5b58\u788e\u7247\n\n#### 5\uff09\u589e\u91cf\u7b97\u6cd5 incremental collecting\n\n\u6bcf\u6b21GC\u7ebf\u7a0b\u53ea\u6536\u96c6\u4e00\u5c0f\u7247\u5185\u5b58\u7a7a\u95f4\uff0c\u63a5\u7740\u5207\u6362\u5230\u5e94\u7528\u7a0b\u5e8f\u7ebf\u7a0b\uff0c\u4f9d\u6b21\u53cd\u590d\uff0c\u76f4\u5230\u5783\u573e\u6536\u96c6\u5b8c\u6210\n\n### \u4e8c\u3001\u5806\u3001\u6808\u7684\u6570\u636e\u7684GC\n\njs\u7684\u5185\u5b58\u7a7a\u95f4\u6709 \u8c03\u7528\u6808 stack \u548c\u5806\u7a7a\u95f4 heap\n\n#### 1\uff09\u8c03\u7528\u6808\u4e2d\u7684\u6570\u636e\u7684GC\n\n\u8c03\u7528\u6808\u6709\u4e00\u4e2a\u8bb0\u5f55\u5f53\u524d\u6267\u884c\u72b6\u6001\u7684\u6307\u9488\uff08\u79f0\u4e3a ESP\uff09\uff0cJS\u5f15\u64ce\u901a\u8fc7\u4e0b\u79fbESP\u6307\u9488\u6765\u9500\u6bc1\u6808\u9876\u67d0\u4e2a\u6267\u884c\u4e0a\u4e0b\u6587\uff0c\u91ca\u653e\u6808\u7a7a\u95f4\n\n#### 2\uff09\u5806\u7a7a\u95f4\u4e2d\u6570\u636e\u7684GC\n\n**\u5206\u4ee3\u6536\u96c6\uff1a** \u65b0\u751f\u4ee3\u3001\u8001\u751f\u4ee3\n\n\u63d0\u9ad8\u5783\u573e\u56de\u6536\u7684\u6548\u7387\uff0cV8\u5c06\u5806\u5206\u4e3a\u65b0\u751f\u4ee3\u548c\u8001\u751f\u4ee3\u4e24\u4e2a\u90e8\u5206\uff1a\n\n- \u5176\u4e2d\u65b0\u751f\u4ee3\u4e3a\u5b58\u6d3b\u65f6\u95f4\u8f83\u77ed\u7684\u5bf9\u8c61(\u9700\u8981\u7ecf\u5e38\u8fdb\u884c\u5783\u573e\u56de\u6536)\uff0c\u5185\u5b58\u5360\u7528\u5c0f\uff0cGC\u9891\u7e41\uff1b\u53ea\u652f\u63011-8M\u5bb9\u91cf\uff1b\u4f7f\u7528\u526f\u5783\u573e\u56de\u6536\u5668\uff1b\n- \u800c\u8001\u751f\u4ee3\u4e3a\u5b58\u6d3b\u65f6\u95f4\u8f83\u957f\u7684\u5bf9\u8c61(\u5783\u573e\u56de\u6536\u7684\u9891\u7387\u8f83\u4f4e)\uff0c\u5185\u5b58\u5360\u7528\u591a\uff0cGC\u4e0d\u9891\u7e41\uff1b\u4f7f\u7528\u4e3b\u5783\u573e\u56de\u6536\u5668\uff1b\n\n**\u65b0\u751f\u4ee3\u7684GC\u7b97\u6cd5**\n\n\u65b0\u751f\u4ee3\u7684\u5bf9\u8c61\u901a\u8fc7\u00a0Scavenge\u00a0\u7b97\u6cd5\u8fdb\u884cGC\u3002\u5728\u00a0Scavenge\u00a0\u7684\u5177\u4f53\u5b9e\u73b0\u4e2d\uff0c\u4e3b\u8981\u91c7\u7528\u4e86\u00a0Cheney\u00a0\u7b97\u6cd5\u3002\n\n1. Cheney \u7b97\u6cd5\u662f\u4e00\u79cd\u91c7\u7528\u505c\u6b62\u590d\u5236\uff08stop-copy\uff09\u7684\u65b9\u5f0f\u5b9e\u73b0\u7684\u5783\u573e\u56de\u6536\u7b97\u6cd5\u3002\n2. \u5b83\u5c06\u5806\u5185\u5b58\u4e00\u5206\u4e3a\u4e8c\uff0c\u6bcf\u4e00\u90e8\u5206\u7a7a\u95f4\u6210\u4e3a semispace-\u534a\u7a7a\u95f4\u3002\n3. \u5728\u8fd9\u4e24\u4e2a semispace \u7a7a\u95f4\u4e2d\uff0c\u53ea\u6709\u4e00\u4e2a\u5904\u4e8e\u4f7f\u7528\u4e2d\uff0c\u53e6\u4e00\u4e2a\u5904\u4e8e\u95f2\u7f6e\u4e2d\u3002\n4. \u5904\u4e8e\u4f7f\u7528\u4e2d\u7684 semispace \u7a7a\u95f4\u6210\u4e3a From \u5bf9\u8c61\u7a7a\u95f4\uff0c\u5904\u4e8e\u95f2\u7f6e\u72b6\u6001\u7684\u7a7a\u95f4\u6210\u4e3a To \u7a7a\u95f2\u7a7a\u95f4\u3002\n5. \u5f53\u6211\u4eec\u5206\u914d\u5bf9\u8c61\u65f6\uff0c\u5148\u662f\u5728 From \u7a7a\u95f4\u4e2d\u8fdb\u884c\u5206\u914d\u3002\u5f53\u5f00\u59cb\u8fdb\u884c\u5783\u573e\u56de\u6536\u65f6\uff0c\u4f1a\u68c0\u67e5 From \u7a7a\u95f4\u4e2d\u7684\u5b58\u6d3b\u5bf9\u8c61\uff0c\u8fd9\u4e9b\u5b58\u6d3b\u5bf9\u8c61\u5c06\u88ab\u590d\u5236\u5230 To \u7a7a\u95f4\u4e2d\uff0c\u540c\u65f6\u8fd8\u4f1a\u5c06\u8fd9\u4e9b\u5bf9\u8c61\u6709\u5e8f\u7684\u6392\u5217\u8d77\u6765~~\u76f8\u5f53\u4e8e\u5185\u5b58\u6574\u7406\uff0c\u6240\u4ee5\u6ca1\u6709\u5185\u5b58\u788e\u7247\uff0c\u800c\u975e\u5b58\u6d3b\u5bf9\u8c61\u5360\u7528\u7684\u7a7a\u95f4\u5c06\u88ab\u91ca\u653e\u3002\n\u5b8c\u6210\u590d\u5236\u540e\uff0cFrom\u7a7a\u95f4\u548cTo\u7a7a\u95f4\u7684\u89d2\u8272\u53d1\u751f\u5bf9\u6362\u3002\n6. Scavenge \u662f\u5178\u578b\u7684\u7a7a\u95f4\u6362\u53d6\u65f6\u95f4\u7684\u7b97\u6cd5\uff0c\u800c\u4e14\u590d\u5236\u9700\u8981\u65f6\u95f4\u6210\u672c\uff0c\u65e0\u6cd5\u5927\u89c4\u6a21\u5730\u5e94\u7528\u5230\u6240\u6709\u7684\u5783\u573e\u56de\u6536\u4e2d\uff0c\u4f46\u975e\u5e38\u9002\u5408\u5e94\u7528\u5728\u65b0\u751f\u4ee3\u4e2d\u8fdb\u884c\u5feb\u901f\u9891\u7e41\u6e05\u7406\u3002\n\n**\u5bf9\u8c61\u664b\u5347\u7b56\u7565**\n\n\u5bf9\u8c61\u4ece\u65b0\u751f\u4ee3\u4e2d\u79fb\u52a8\u5230\u8001\u751f\u4ee3\u4e2d\u7684\u8fc7\u7a0b\u79f0\u4e3a\u664b\u5347\u3002\n\n\u664b\u5347\u6761\u4ef6\u4e3b\u8981\u6709\u4e24\u4e2a\uff1a\n\n- \u5bf9\u8c61\u662f\u5426\u7ecf\u5386\u8fc7\u4e24\u6b21 Scavenge \u56de\u6536\u90fd\u672a\u6e05\u9664\uff0c\u5219\u79fb\u52a8\u5230\u8001\u751f\u4ee3\n- To \u7a7a\u95f4\u5df2\u7ecf\u4f7f\u7528\u8d85\u8fc7 25%\uff0cTo \u7a7a\u95f4\u5bf9\u8c61\u79fb\u52a8\u5230\u8001\u751f\u4ee3\n\n> \u56e0\u4e3a\u8fd9\u6b21 Scavenge \u56de\u6536\u5b8c\u6210\u540e\uff0c\u8fd9\u4e2a To \u7a7a\u95f4\u5c06\u53d8\u6210 From \u7a7a\u95f4\uff0c\u63a5\u4e0b\u6765\u7684\u5185\u5b58\u5206\u914d\u5c06\u5728\u8fd9\u4e2a\u7a7a\u95f4\u4e2d\u8fdb\u884c\uff0c\u5982\u679c\u5360\u6bd4\u8fc7\u9ad8\uff0c\u4f1a\u5f71\u54cd\u540e\u7eed\u7684\u5185\u5b58\u5206\u914d\n\n**\u5199\u5c4f\u969c**\n\n \u5199\u7f13\u51b2\u533a\u4e2d\u6709\u4e00\u4e2a\u5217\u8868(CrossRefList)\uff0c\u5217\u8868\u4e2d\u8bb0\u5f55\u4e86\u6240\u6709\u8001\u751f\u533a\u5bf9\u8c61\u6307\u5411\u65b0\u751f\u533a\u7684\u60c5\u51b5\n\n \u8fd9\u6837\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u6307\u5411\u65b0\u751f\u4ee3\u8be5\u5bf9\u8c61\u7684\u8001\u751f\u4ee3\u5bf9\u8c61\uff0c\u6839\u636e\u4ed6\u662f\u5426\u6d3b\u8dc3\uff0c\u6765\u6e05\u7406\u8fd9\u4e2a\u65b0\u751f\u4ee3\u5bf9\u8c61\uff1b\n\n **\u8001\u751f\u4ee3\u7684GC**\n\n\u8001\u751f\u4ee3\u7684\u5185\u5b58\u7a7a\u95f4\u8f83\u5927\u4e14\u5b58\u6d3b\u5bf9\u8c61\u8f83\u591a\uff0c\u4f7f\u7528\u65b0\u751f\u4ee3\u7684Scavenge\u00a0\u590d\u5236\u7b97\u6cd5\uff0c\u4f1a\u8017\u8d39\u5f88\u591a\u65f6\u95f4\uff0c\u6548\u7387\u4e0d\u9ad8\uff1b\u800c\u4e14\u8fd8\u4f1a\u6d6a\u8d39\u4e00\u534a\u7684\u7a7a\u95f4\uff1b\n\n\u4e3a\u6b64V8\u4f7f\u7528\u4e86\u6807\u8bb0-\u6e05\u9664\u7b97\u6cd5 (Mark-Sweep)\u8fdb\u884c\u5783\u573e\u56de\u6536\uff0c\u5e76\u4f7f\u7528\u6807\u8bb0-\u538b\u7f29\u7b97\u6cd5 (Mark-Compact)\u6574\u7406\u5185\u5b58\u788e\u7247\uff0c\u63d0\u9ad8\u5185\u5b58\u7684\u5229\u7528\u7387\u3002\u6b65\u9aa4\u5982\u4e0b\uff1a\n\n1. \u5bf9\u8001\u751f\u4ee3\u8fdb\u884c\u7b2c\u4e00\u904d\u626b\u63cf\uff0c\u6807\u8bb0\u5b58\u6d3b\u7684\u5bf9\u8c61,\u4ece\u4e00\u7ec4\u6839\u5143\u7d20\u5f00\u59cb\uff0c\u9012\u5f52\u904d\u5386\u8fd9\u7ec4\u6839\u5143\u7d20\uff0c\u5728\u8fd9\u4e2a\u904d\u5386\u8fc7\u7a0b\u4e2d\uff0c\u80fd\u5230\u8fbe\u7684\u5143\u7d20\u79f0\u4e3a\u6d3b\u52a8\u5bf9\u8c61\uff0c\u6ca1\u6709\u5230\u8fbe\u7684\u5143\u7d20\u5c31\u53ef\u4ee5\u5224\u65ad\u4e3a\u5783\u573e\u6570\u636e\uff1b\n2. \u5bf9\u8001\u751f\u4ee3\u8fdb\u884c\u7b2c\u4e8c\u6b21\u626b\u63cf\uff0c\u6e05\u9664\u672a\u88ab\u6807\u8bb0\u7684\u5bf9\u8c61\n3. \u6807\u8bb0-\u6574\u7406\u7b97\u6cd5\uff0c\u6807\u8bb0\u9636\u6bb5\u4e00\u6837\u662f\u9012\u5f52\u904d\u5386\u5143\u7d20\uff0c\u6574\u7406\u9636\u6bb5\u662f\u5c06\u5b58\u6d3b\u5bf9\u8c61\u5f80\u5185\u5b58\u7684\u4e00\u7aef\u79fb\u52a8\n4. \u6e05\u9664\u6389\u5b58\u6d3b\u5bf9\u8c61\u8fb9\u754c\u5916\u7684\u5185\u5b58\n\n> \u6ce8\u610f\uff0c\u4e0d\u7ba1\u90a3\u79cd\uff0c\u6574\u7406\u5185\u5b58\u540e\u53ea\u8981\u5730\u5740\u6709\u53d8\u5316\uff0c\u9700\u8981\u53ca\u65f6\u66f4\u65b0\u5230\u8c03\u7528\u6808\u7684\n\n **\u5168\u505c\u987fStop-The-World**\n\nJavaScript \u662f\u8fd0\u884c\u5728\u4e3b\u7ebf\u7a0b\u4e4b\u4e0a\u7684\uff0c\u4e00\u65e6\u6267\u884c\u5783\u573e\u56de\u6536\u7b97\u6cd5\uff0c\u90fd\u9700\u8981\u5c06\u6b63\u5728\u6267\u884c\u7684 JavaScript \u811a\u672c\u6682\u505c\u4e0b\u6765\uff0c\u5f85\u5783\u573e\u56de\u6536\u5b8c\u6bd5\u540e\u518d\u6062\u590d\u811a\u672c\u6267\u884c\u3002\u6211\u4eec\u628a\u8fd9\u79cd\u884c\u4e3a\u53eb\u505a\u5168\u505c\u987f\uff08Stop-The-World\uff09\uff1b\n\n\u65b0\u751f\u4ee3\u56e0\u4e3a\u5185\u5b58\u5c0f\uff0c\u6d3b\u52a8\u5bf9\u8c61\u5c11\uff0c\u5168\u505c\u987f\u5f71\u54cd\u4e0d\u5927\uff0c\u4f46\u662f\u8001\u751f\u4ee3\u53ef\u80fd\u4f1a\u9020\u6210\u5361\u987f\u660e\u663e\uff1b\n\n\u89e3\u51b3\u529e\u6cd5\uff1a\u589e\u91cf\u6807\u8bb0\u7b97\u6cd5Incremental Marking\n\nV8 \u5c06\u6807\u8bb0\u8fc7\u7a0b\u5206\u4e3a\u4e00\u4e2a\u4e2a\u7684\u5b50\u6807\u8bb0\u8fc7\u7a0b\uff0c\u540c\u65f6\u8ba9\u5783\u573e\u56de\u6536\u6807\u8bb0\u548c JavaScript \u5e94\u7528\u903b\u8f91\u4ea4\u66ff\u8fdb\u884c\uff0c\u76f4\u5230\u6807\u8bb0\u9636\u6bb5\u5b8c\u6210\uff1b\n\n\n### \u4e09\u3001JavaScript\u4e2d\u7684\u5783\u573e\u56de\u6536\n\nV8\uff08javascript\u5f15\u64ce\uff09\u7684\u65b0\u8001\u7a7a\u95f4\u5185\u5b58\u5206\u914d\u4e0e\u5927\u5c0f\u9650\u5236\n\n#### \u65b0\u8001\u7a7a\u95f4\n\n\u51e1\u4e8b\u90fd\u6709\u4e00\u628a\u53cc\u5203\u5251\uff0c\u5728\u5783\u573e\u56de\u6536\u7684\u6f14\u53d8\u8fc7\u7a0b\u4e2d\u4eba\u4eec\u53d1\u73b0\uff0c\u6ca1\u6709\u4e00\u79cd\u7279\u5b9a\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u662f\u53ef\u4ee5\u5b8c\u7f8e\u7684\u89e3\u51b3\u95ee\u9898\uff0c\u56e0\u6b64V8\u91c7\u7528\u4e86\u65b0\u751f\u4ee3\u4e0e\u8001\u751f\u4ee3\u7ed3\u5408\u7684\u5783\u573e\u56de\u6536\u65b9\u5f0f\uff0c\u5c06\u5185\u5b58\u5206\u4e3a\u65b0\u751f\u4ee3\u548c\u8001\u751f\u4ee3\u3002 \u65b0\u751f\u4ee3\u9891\u7e41\u8fdb\u884cGC\uff0c\u7a7a\u95f4\u5c0f\uff0c\u91c7\u7528\u7684\u662f\u7a7a\u95f4\u6362\u65f6\u95f4\u7684scavenge\u7b97\u6cd5\uff0c\u6240\u4ee5\u53c8\u5212\u5206\u4e3a\u4e24\u5757semispace\uff0cFrom\u548cTo\u3002 \u8001\u751f\u4ee3\u5927\u90e8\u5206\u4fdd\u5b58\u7684\u662f\u5b58\u6d3b\u65f6\u95f4\u8f83\u957f\u7684\u6216\u8005\u8f83\u5927\u7684\u5bf9\u8c61\u3002\u91c7\u7528\u7684\u662fmark-sweep\uff08\u4e3b\uff09&mark-compact\uff08\u8f85\uff09\u7b97\u6cd5\u3002\n\nV8\u9650\u5236\u4e86js\u5bf9\u8c61\u53ef\u4ee5\u4f7f\u7528\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u4e0d\u6b62\u662f\u56e0\u4e3a\u6700\u521dV8\u662f\u4f5c\u4e3a\u6d4f\u89c8\u5668\u5f15\u64ce\u800c\u8bbe\u8ba1\u7684\u3002\u8fd8\u6709\u5176\u5783\u573e\u56de\u6536\u673a\u5236\u7684\u5f71\u54cd\u56e0\u7d20\u3002V8\u4f7f\u7528stop-the-world\uff08\u5168\u505c\u987f\uff09, generational, accurate\u7684\u5783\u573e\u56de\u6536\u5668\u3002\u5728\u6267\u884c\u56de\u6536\u4e4b\u65f6\u4f1a\u6682\u65f6\u4e2d\u65ad\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u800c\u4e14\u53ea\u5904\u7406\u5bf9\u8c61\u5806\u6808\u3002\u5f53\u5185\u5b58\u8fbe\u5230\u4e00\u5b9a\u7684\u4f53\u79ef\u65f6\uff0c\u8fdb\u884c\u4e00\u6b21\u5783\u573e\u56de\u6536\u7684\u65f6\u95f4\u5c06\u4f1a\u5f88\u957f\uff0c\u4ece\u800c\u5f71\u54cd\u5176\u76f8\u5e94\u800c\u9020\u6210\u6d4f\u89c8\u5668\u5047\u6b7b\u7684\u72b6\u51b5\u3002\u56e0\u6b64\uff0c\u5728V8\u4e2d\u9650\u5236\u8001\u751f\u4ee364\u4f4d\u4e3a1.4GB\uff0c32\u4f4d\u4e3a0.7GB\uff0c\u65b0\u751f\u4ee364\u4f4d\u4e3a32M\uff0c32\u4f4d\u4e3a16M\u3002 \u5f53\u7136\uff0c\u5982\u679c\u9700\u8981\u66f4\u5927\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u5728node\u4e2d\u53ef\u4ee5\u8fdb\u884c\u66f4\u6539\u3002\n\n#### \u5bf9\u8c61\u664b\u5347\n\n\u65b0\u751f\u6210\u7684\u5bf9\u8c61\u653e\u5165\u65b0\u751f\u4ee3\u5185\u5b58\u4e2d\uff0c\u90a3\u54ea\u4e9b\u5bf9\u8c61\u4f1a\u88ab\u653e\u5165\u8001\u751f\u4ee3\u4e2d\u5462\uff1f\u5927\u90e8\u5206\u653e\u5165\u8001\u751f\u4ee3\u7684\u5bf9\u8c61\u662f\u7531\u65b0\u751f\u4ee3\u664b\u5347\u800c\u6765\u3002\u5bf9\u8c61\u7684\u664b\u5347\u7684\u65b9\u5f0f\uff1a\n\n\u5f53\u65b0\u751f\u4ee3\u7684To semispace\u5185\u5b58\u5360\u6ee125%\u65f6\uff0c\u6b64\u65f6\u518d\u4eceFrom semispace\u62f7\u8d1d\u5bf9\u8c61\u5c06\u4e0d\u4f1a\u518d\u653e\u5165To\u7a7a\u95f4\u4e2d\u4ee5\u9632\u5f71\u54cd\u540e\u7eed\u7684\u65b0\u5bf9\u8c61\u5206\u914d\uff0c\u800c\u5c06\u5176\u76f4\u63a5\u590d\u5236\u5230\u8001\u751f\u4ee3\u7a7a\u95f4\u4e2d\u3002\n\n\u5728\u8fdb\u884c\u4e00\u6b21\u5783\u573e\u56de\u6536\u540e\uff0c\u7b2c\u4e8c\u6b21GC\u65f6\uff0c\u53d1\u73b0\u5df2\u7ecf\u7ecf\u5386\u8fc7\u4e00\u6b21GC\u7684\u5bf9\u8c61\u5728\u4eceFrom\u7a7a\u95f4\u590d\u5236\u65f6\u76f4\u63a5\u590d\u5236\u5230\u8001\u751f\u4ee3\u3002\n\n\u5728\u65b0\u5bf9\u8c61\u5206\u914d\u65f6\u5927\u90e8\u5206\u5bf9\u8c61\u88ab\u5206\u914d\u5230\u65b0\u751f\u4ee3\u7684From semispace\uff0c\u4f46\u5f53\u8fd9\u4e2a\u5bf9\u8c61\u7684\u4f53\u79ef\u8fc7\u5927\uff0c\u8d85\u8fc71MB\u7684\u5185\u5b58\u9875\u65f6\uff0c\u76f4\u63a5\u5206\u914d\u5230\u8001\u751f\u4ee3\u4e2d\u7684large Object Space\u3002\n\n#### \u65b0\u751f\u4ee3\u7684GC\u673a\u5236\u4e0e\u4f18\u7f3a\u70b9\n\n**\u56de\u6536\u673a\u5236**\n\n\u65b0\u751f\u4ee3\u91c7\u7528Scavenge\u7b97\u6cd5\uff0c\u5728scavenge\u7b97\u6cd5\u7684\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\uff0c\u5219\u4e3b\u8981\u91c7\u7528\u4e86cheney\u7b97\u6cd5\u3002\u5373\u4f7f\u7528\u590d\u5236\u65b9\u5f0f\u6765\u5b9e\u73b0\u5783\u573e\u56de\u6536\u3002\u5b83\u5c06\u5185\u5b58\u4e00\u5206\u4e3a\u4e8c\uff0c\u6bcf\u4e00\u4e2a\u7a7a\u95f4\u90fd\u662f\u4e00\u4e2asemispace\u3002\n\n\u5904\u4e8e\u4f7f\u7528\u72b6\u6001\u7684\u662fFrom\u7a7a\u95f4\uff0c\u95f2\u7f6e\u7684\u662fTo\u7a7a\u95f4\u3002\u5f53\u5206\u914d\u5bf9\u8c61\u65f6\uff0c\u5148\u662f\u5206\u914d\u5230From\u7a7a\u95f4\uff0c\u5783\u573e\u56de\u6536\u65f6\u4f1a\u68c0\u67e5From\u7a7a\u95f4\u4e2d\u5b58\u6d3b\u7684\u5bf9\u8c61\uff0c\u5c06\u5176\u590d\u5236\u5230To\u7a7a\u95f4\uff0c\u56de\u6536\u5176\u4ed6\u7684\u5bf9\u8c61\u3002\u5b8c\u6210\u590d\u5236\u540e\u4f1a\u8fdb\u884c\u7d27\u7f29\uff0cFrom\u548cTo\u7a7a\u95f4\u7684\u8c03\u6362\u3002\u5982\u6b64\u5faa\u73af\u5f80\u590d\u3002\n\n**\u4f18\u52bf**\n\n\u7531\u5176\u6267\u884c\u7684\u7b97\u6cd5\u53ca\u8fc7\u7a0b\u6211\u4eec\u53ef\u4ee5\u4e86\u89e3\u5230\uff0c\u5728\u65b0\u751f\u4ee3\u7684\u5783\u573e\u56de\u6536\u8fc7\u7a0b\u4e2d\uff0c\u603b\u662f\u7531\u4e00\u534a\u7684semispace\u662f\u7a7a\u4f59\u7684\u3002scavenge\u53ea\u590d\u5236\u5b58\u6d3b\u7684\u5bf9\u8c61\uff0c\u5728\u65b0\u751f\u4ee3\u7684\u5185\u5b58\u4e2d\uff0c\u5b58\u6d3b\u7684\u5bf9\u8c61\u76f8\u5bf9\u8f83\u5c11\uff0c\u6240\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u7b97\u6cd5\u6070\u5230\u597d\u5904\u3002\n\n#### \u8001\u751f\u4ee3\u7684GC\u673a\u5236\u4e0e\u4f18\u7f3a\u70b9\n\n**\u56de\u6536\u673a\u5236**\n\n\u7531\u4e8e\u7684scavenge\u7b97\u6cd5\u53ea\u590d\u5236\u5b58\u6d3b\u7684\u5bf9\u8c61\uff0c\u5982\u679c\u5728\u8001\u751f\u4ee3\u4e2d\u4e5f\u4f7f\u7528\u6b64\u7b97\u6cd5\u7684\u8bdd\u5c31\u4f1a\u9020\u6210\u590d\u5236\u5f88\u591a\u5bf9\u8c61\uff0c\u6548\u7387\u4f4e\uff0c\u5e76\u4e14\u9020\u6210\u5f88\u5927\u7684\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u3002 \u8001\u751f\u4ee3\u4e2d\u91c7\u7528\u7684\u5219\u662fmark-sweep\uff08\u6807\u8bb0\u6e05\u9664\uff09\u548cmark-compact\uff08\u6807\u8bb0\u6574\u7406\uff09\u7ed3\u5408\u7684\u65b9\u5f0f\u3002\u800c\u4e3a\u4ec0\u4e48\u4f7f\u7528\u4e24\u8005\u7ed3\u5408\u5462\uff1f\u8fd9\u5c31\u8981\u8bb2\u5230\u4e24\u8005\u7684\u4f18\u70b9\u4e0e\u7f3a\u70b9\u3002\n\n**mark-sweep\uff08\u6807\u8bb0\u6e05\u9664\uff09**\n\n1\uff09\u4f18\u70b9\n\n- \u6807\u8bb0\u6e05\u9664\u9700\u8981\u6807\u8bb0\u5806\u5185\u5b58\u4e2d\u7684\u6240\u6709\u5bf9\u8c61\uff0c\u6807\u8bb0\u51fa\u5728\u4f7f\u7528\u7684\u5bf9\u8c61\uff0c\u6e05\u9664\u90a3\u4e9b\u6ca1\u6709\u88ab\u6807\u8bb0\u7684\u5bf9\u8c61\u3002\u5728\u8001\u751f\u4ee3\u5185\u5b58\u4e2d\u4e0e\u65b0\u751f\u4ee3\u76f8\u53cd\uff0c\u4e0d\u4f7f\u7528\u7684\u5bf9\u8c61\u53ea\u5360\u5f88\u5c0f\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u6e05\u9664\u4e0d\u7528\u7684\u5bf9\u8c61\u6548\u7387\u9ad8\u3002\n- mark-sweep\u4e0d\u4f1a\u5c06\u5185\u5b58\u7a7a\u95f4\u5206\u4e3a\u4e24\u534a\uff0c\u6240\u4ee5\uff0c\u4e0d\u4f1a\u6d6a\u8d39\u4e00\u534a\u7a7a\u95f4\u3002\n\n2\uff09\u7f3a\u70b9\n\n\u4f46\u6807\u8bb0\u6e05\u9664\u4f1a\u9020\u6210\u4e00\u4e2a\u95ee\u9898\uff0c\u5c31\u662f\u5728\u6e05\u9664\u8fc7\u540e\u4f1a\u5bfc\u81f4\u5185\u5b58\u4e0d\u8fde\u7eed\uff0c\u9020\u6210\u5185\u5b58\u788e\u7247\uff0c\u5982\u679c\u6b64\u65f6\u9700\u8981\u50a8\u5b58\u4e00\u4e2a\u5f88\u5927\u7684\u5185\u5b58\u800c\u7a7a\u95f4\u53c8\u4e0d\u591f\u7684\u65f6\u5019\u5c31\u4f1a\u9020\u6210\u6ca1\u6709\u5fc5\u8981\u7684\u53cd\u590d\u5783\u573e\u56de\u6536\u3002\n\n**mark-compact\uff08\u6807\u8bb0\u6574\u7406\uff09**\n\n1\uff09\u4f18\u70b9\n\n\u6b64\u65f6\u6807\u8bb0\u6574\u7406\u5c31\u53ef\u4ee5\u51fa\u573a\u4e86\uff0c\u5728\u6807\u8bb0\u6e05\u9664\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6807\u8bb0\u6574\u7406\u4f1a\u5c06\u5b58\u6d3b\u7684\u5bf9\u8c61\u548c\u9700\u8981\u6e05\u9664\u7684\u5bf9\u8c61\u79fb\u52a8\u5230\u4e24\u7aef\u3002\u7136\u540e\u5c06\u5176\u4e2d\u4e00\u6bb5\u9700\u8981\u6e05\u9664\u7684\u6d88\u706d\u6389\uff0c\u53ef\u4ee5\u89e3\u51b3\u6807\u8bb0\u6e05\u9664\u9020\u6210\u7684\u5185\u5b58\u788e\u7247\u95ee\u9898\u3002\n\n2\uff09\u7f3a\u70b9\n\n\u4f46\u662f\u5728\u7d27\u7f29\u5185\u5b58\u7684\u8fc7\u7a0b\u4e2d\u9700\u8981\u79fb\u52a8\u5bf9\u8c61\uff0c\u6548\u7387\u6bd4\u8f83\u4f4e\u3002\u6240\u4ee5V8\u5728\u6e05\u7406\u65f6\u4e3b\u8981\u4f1a\u4f7f\u7528Mark-sweep,\u5728\u7a7a\u95f4\u4e0d\u8db3\u4ee5\u5bf9\u65b0\u751f\u4ee3\u4e2d\u664b\u5347\u8fc7\u6765\u7684\u5bf9\u8c61\u8fdb\u884c\u5206\u914d\u65f6\u624d\u4f1a\u4f7f\u7528Mark-compact\u3002\n\n#### \u5783\u573e\u56de\u6536\u673a\u5236\u7684\u4f18\u5316\n\n\u589e\u91cf\u6807\u8bb0(\u5728\u8001\u7a7a\u95f4\u91cc\u5f15\u5165\u4e86\u6b64\u65b9\u5f0f)\n\nscavenge\u7b97\u6cd5,mark-sweep\u53camark-compact\u90fd\u4f1a\u5bfc\u81f4stop-the-world\uff08\u5168\u505c\u987f\uff09\u3002\u800c\u5168\u505c\u987f\u5f88\u5bb9\u6613\u5e26\u6765\u660e\u663e\u7684\u7a0b\u5e8f\u8fdf\u6ede\uff0c\u6807\u8bb0\u9636\u6bb5\u5f88\u5bb9\u6613\u5c31\u4f1a\u8d85\u8fc7100ms\uff0c\u56e0\u6b64V8\u5f15\u5165\u4e86\u589e\u91cf\u6807\u8bb0\uff0c\u5c06\u6807\u8bb0\u9636\u6bb5\u5206\u4e3a\u82e5\u5e72\u5c0f\u6b65\u9aa4\uff0c\u6bcf\u4e2a\u6b65\u9aa4\u63a7\u5236\u57285ms\u5185\uff0c\u6bcf\u8fd0\u884c\u4e00\u6bb5\u65f6\u95f4\u6807\u8bb0\u52a8\u4f5c\uff0c\u5c31\u8ba9JavaScript\u7a0b\u5e8f\u6267\u884c\u4e00\u4f1a\u513f\uff0c\u5982\u6b64\u4ea4\u66ff\uff0c\u660e\u663e\u5730\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u6d41\u7545\u6027\uff0c\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u907f\u514d\u4e86\u957f\u65f6\u95f4\u5361\u987f\u3002\n\n\n\n\n\n\n\n\n", "answer": "", "biz_type": 1, "qid": 82, "subject": "", "title": "\u8bf4\u4e00\u4e0b GC"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u987a\u4e30"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 8, "qid": 125, "qtype": "short_answer", "short_answer": {"analysis": "### \u4e00\u3001\u57fa\u672c\u64cd\u4f5c\n\n\u4e00\u4e2a\u57fa\u672c\u7684fetch\u64cd\u4f5c\u5f88\u7b80\u5355\u3002\u5c31\u662f\u901a\u8fc7fetch\u8bf7\u6c42\uff0c\u8fd4\u56de\u4e00\u4e2apromise\u5bf9\u8c61\uff0c\u7136\u540e\u5728promise\u5bf9\u8c61\u7684then\u65b9\u6cd5\u91cc\u9762\u7528fetch\u7684response.json()\u7b49\u65b9\u6cd5\u8fdb\u884c\u89e3\u6790\u6570\u636e\uff0c\u7531\u4e8e\u8fd9\u4e2a\u89e3\u6790\u8fd4\u56de\u7684\u4e5f\u662f\u4e00\u4e2apromise\u5bf9\u8c61\uff0c\u6240\u4ee5\u9700\u8981\u4e24\u4e2athen\u624d\u80fd\u5f97\u5230\u6211\u4eec\u9700\u8981\u7684json\u6570\u636e\u3002\n\n```js\nfetch('http://example.com/movies.json')\n.then(function(response) {\n return response.json();\n})\n.then(function(myJson) {\n console.log(myJson);\n});\n```\n\n### \u4e8c\u3001\u4e3a\u4f55\u4e0d\u76f4\u63a5\u4f7f\u7528\u57fa\u672c\u64cd\u4f5c\n\nfetch\u89c4\u8303\u4e0ejQuery.ajax()\u4e3b\u8981\u6709\u4e24\u79cd\u65b9\u5f0f\u7684\u4e0d\u540c\uff1a\n\n1. \u5f53\u63a5\u6536\u5230\u4e00\u4e2a\u4ee3\u8868\u9519\u8bef\u7684 HTTP \u72b6\u6001\u7801\u65f6,\u6bd4\u5982400, 500\uff0cfetch\u4e0d\u4f1a\u628apromise\u6807\u8bb0\u4e3areject, \u800c\u662f\u6807\u8bb0\u4e3aresolve\uff0c\u4ec5\u5f53\u7f51\u7edc\u6545\u969c\u65f6\u6216\u8bf7\u6c42\u88ab\u963b\u6b62\u65f6\uff0c\u624d\u4f1a\u6807\u8bb0\u4e3a reject\u3002\n2. \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cfetch \u4e0d\u4f1a\u4ece\u670d\u52a1\u7aef\u53d1\u9001\u6216\u63a5\u6536\u4efb\u4f55 cookies, \u5982\u679c\u7ad9\u70b9\u4f9d\u8d56\u4e8e\u7528\u6237 session\uff0c\u5219\u4f1a\u5bfc\u81f4\u672a\u7ecf\u8ba4\u8bc1\u7684\u8bf7\u6c42\uff08\u8981\u53d1\u9001 cookies\uff0c\u5fc5\u987b\u8bbe\u7f6e credentials \u9009\u9879\uff09\u3002\n\n\u4ece\u8fd9\u91cc\u53ef\u4ee5\u770b\u51fa\u6765\uff0c\u5982\u679c\u6211\u4eec\u8981\u5728fetch\u8bf7\u6c42\u51fa\u9519\u7684\u65f6\u5019\u53ca\u65f6\u5730\u6355\u83b7\u9519\u8bef\uff0c\u662f\u9700\u8981\u5bf9response\u7684\u72b6\u6001\u7801\u8fdb\u884c\u89e3\u6790\u7684\u3002\u53c8\u7531\u4e8efetch\u8fd4\u56de\u7684\u6570\u636e\u4e0d\u4e00\u5b9a\u662fjson\u683c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u4eceheader\u91cc\u9762Content-Type\u83b7\u53d6\u8fd4\u56de\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8fdb\u800c\u4f7f\u7528\u6b63\u786e\u7684\u89e3\u6790\u65b9\u6cd5\u3002\n\n### \u4e09\u3001\u4f7f\u7528async/awiait\u7684\u539f\u56e0\n\nPromise \u5c06\u5f02\u6b65\u64cd\u4f5c\u89c4\u8303\u5316.\u4f7f\u7528then\u8fde\u63a5, \u4f7f\u7528catch\u6355\u83b7\u9519\u8bef, \u582a\u79f0\u5b8c\u7f8e, \u7f8e\u4e2d\u4e0d\u8db3\u7684\u662f, then\u548ccatch\u4e2d\u4f20\u9012\u7684\u4f9d\u7136\u662f\u56de\u8c03\u51fd\u6570, \u4e0e\u5fc3\u76ee\u4e2d\u7684\u540c\u6b65\u4ee3\u7801\u4e0d\u662f\u4e00\u4e2a\u5957\u8def.\n\n\u4e3a\u6b64, ES7 \u63d0\u4f9b\u4e86\u66f4\u6807\u51c6\u7684\u89e3\u51b3\u65b9\u6848 \u2014 async/await. async/await \u51e0\u4e4e\u6ca1\u6709\u5f15\u5165\u65b0\u7684\u8bed\u6cd5, \u8868\u9762\u4e0a\u770b\u8d77\u6765, \u5b83\u5c31\u548calert\u4e00\u6837\u6613\u7528\u3002\n\n```js\nlet searchWord = '123',\n url = `https://www.baidu.com/s?wd=${searchWord}`;\n(async ()=>{\n try {\n let res = await fetch(url, {mode: 'no-cors'});//\u7b49\u5f85fetch\u88abresolve()\u540e\u624d\u80fd\u7ee7\u7eed\u6267\u884c\n console.log(res);//fetch\u6b63\u5e38\u8fd4\u56de\u540e\u624d\u6267\u884c\n return res;//\u8fd9\u6837\u5c31\u80fd\u8fd4\u56deres\u4e0d\u7528\u62c5\u5fc3\u5f02\u6b65\u7684\u95ee\u9898\u5566\u5566\u5566\n } catch(e) {\n console.log(e);\n }\n})();\n```\n\n### \u4e09\u3001\u7b80\u5355\u5c01\u88c5\n\n```js\nclass EasyHttp{\n //get \n async get(url) {\n const response = await fetch(url);\n const data = await response.json();\n return data;\n }\n\n //POST\n async post(url,datas){\n const response = await fetch(url,{\n method: \"POST\",\n headers: {\n 'Content-type': 'application/json'\n },\n body: JSON.stringify(datas) \n })\n const data = await response.json();\n return data;\n }\n\n //PUT\n async put(url,datas){\n const response = await fetch(url,{\n method: \"PUT\",\n headers: {\n 'Content-type': 'application/json'\n },\n body: JSON.stringify(datas) \n })\n const data = await response.json();\n return data;\n }\n\n //delete\n async delete(url){\n const response = await fetch(url,{\n method: \"DELETE\",\n headers: {\n 'Content-type': 'application/json'\n }\n })\n const data = await \"\u6570\u636e\u5220\u9664\u6210\u529f\"; //await\u540e\u9762\u8fd8\u53ef\u4ee5\u76f4\u63a5\u8ddf\u5b57\u7b26\u4e32\u989d \u8fd9\u64cd\u4f5c666...\n return data;\n }\n}\n```\n\n", "answer": "", "biz_type": 1, "qid": 125, "subject": "", "title": "\u7b80\u5355\u5c01\u88c5\u4e00\u4e2a\u5f02\u6b65 fecth\uff0c\u4f7f\u7528 async await \u7684\u65b9\u5f0f\u6765\u4f7f\u7528"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 7, "qid": 74, "qtype": "short_answer", "short_answer": {"analysis": "## \u4ee3\u7801\u5b9e\u73b0\n\n```js\n// \u8f6c\u6362\u4e3a\u771f\u6b63\u7684\u6570\u7ec4\nlet el = Array.from(document.getElementsByTagName(\"*\"));\nlet elObj = {};\n// \u6b63\u5219\u5224\u65ad\u662f\u5426\u662fh/s\u5f00\u5934\nlet reg = /^[h|s].+/gi\nel.map(item=>{\n const tagName = item.tagName;\n if(reg.test(tagName)){\n !elObj[tagName] ? elObj[tagName] = 1 : elObj[tagName]++\n }\n});\nconsole.log(elObj)\n```\n", "answer": "", "biz_type": 1, "qid": 74, "subject": "", "title": "\u968f\u4fbf\u6253\u5f00\u4e00\u4e2a\u7f51\u9875\uff0c\u7528 JavaScript \u6253\u5370\u6240\u6709\u4ee5 s \u548c h \u5f00\u5934\u7684\u6807\u7b7e\uff0c\u5e76\u8ba1\u7b97\u51fa\u6807\u7b7e\u7684\u79cd\u7c7b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u6d0b\u8471\u5b66\u9662"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 4, "qid": 38, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 38, "subject": "", "title": "\u5927\u6570\u8ba1\u7b97\u5982\u4f55\u5b9e\u73b0"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5fc3\u5a31"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 4, "qid": 64, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 64, "subject": "```js\nfunction Foo() {\n getName = function () {\n alert(1);\n };\n return this;\n}\nvar getName;\nfunction getName() {\n alert(5);\n}\nFoo.getName = function () {\n alert(2);\n};\nFoo.prototype.getName = function () {\n alert(3);\n};\ngetName = function () {\n alert(4);\n};\n\nFoo.getName(); // \uff1f\ngetName(); // \uff1f\nFoo().getName(); // \uff1f\ngetName(); // \uff1f\nnew Foo.getName(); // ?\nnew Foo().getName(); // ?\nnew new Foo().getName(); // \uff1f\n```\n", "title": "\u5199\u51fa\u8f93\u51fa\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u8fb9\u950b"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 4, "qid": 105, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 105, "subject": "", "title": "\u662f\u5426\u7528\u8fc7 restful \u63a5\u53e3\uff0c\u548c\u5176\u4ed6\u98ce\u683c\u7684\u6709\u4ec0\u4e48\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u8fb9\u950b", "\u864e\u6251", "\u9177\u5bb6\u4e50", "\u9177\u72d7", "\u5b89\u5c45\u5ba2"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 5, "qid": 106, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 106, "subject": "", "title": "\u8bf4\u4e00\u4e0b get\u3001post\u3001put \u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5feb\u624b"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 4, "qid": 111, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 111, "subject": "", "title": "justify-content:space-between around \u6709\u4ec0\u4e48\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u9ad8\u5fb7", "\u767e\u5206\u70b9"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 9, "qid": 119, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 119, "subject": "", "title": "\u5b9e\u73b0 Promise.then"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u963f\u91cc", "\u987a\u4e30", "\u6613\u8f66", "\u6709\u8d5e", "\u6316\u8d22", "\u559c\u9a6c\u62c9\u96c5", "\u5151\u5427", "\u5bfa\u5e93", "\u6d77\u5eb7\u5a01\u89c6", "\u9053\u4e00\u4e91", "58"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 7, "qid": 120, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 120, "subject": "", "title": "\u5e73\u65f6\u5728\u9879\u76ee\u5f00\u53d1\u4e2d\u90fd\u505a\u8fc7\u54ea\u4e9b\u524d\u7aef\u6027\u80fd\u4f18\u5316"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u987a\u4e30"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 6, "qid": 122, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 122, "subject": "```js\n// \u7ed9\u4e24\u4e2a\u6570\u7ec4 [A1,A2,B1,B2,C1,C2,D1,D2] [A,B,C,D]\n// \u8f93\u51fa [A1,A2,A,B1,B2,B,C1,C2,C,D1,D2,D]\n```\n", "title": "\u8f93\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32\uff0c\u8f93\u51fa\u4ed6\u4eec\u4e2d\u95f4\u7684\u6708\u4efd"}, "tech_tag": ["JavaScript", "JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 13, "qid": 139, "qtype": "short_answer", "short_answer": {"analysis": "", "answer": "", "biz_type": 1, "qid": 139, "subject": "```js\nconst a = sum(); // => a === 0\nconst b = sum(); // => b === 2\nconst c = sum(4)(5); // c === 9\nconst k = sum(n1)...(nk) // k === n1 + n2 + ... + nk\n```\n", "title": "\u6309\u8981\u6c42\u5b9e\u73b0\u4e00\u4e2a sum \u51fd\u6570"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761", "\u7f51\u6613"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 6, "qid": 138, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u6267\u884c\u7ed3\u679c\n\n```js\nscript start\nasync1 start\nasync2\npromise1\nscripts end\nasync1 end\npromise2\nsetTimeout\n```\n\n### \u8bf4\u660e\n\n- \u9996\u5148\uff0c\u540c\u6b65\u4ee3\u7801\u6267\u884c\n- script start\n- async1 start\n- \u8fd9\u91cc\u9047\u5230\u4e86 await async2() ,\u6211\u4eec\u77e5\u9053 await \u4f1a\u9020\u6210\u540c\u6b65\u4ee3\u7801\u963b\u585e\uff0c\u6240\u4ee5\u4f1a\u6267\u884c async2,await \u4e0b\u4e00\u884c\u7684\u4ee3\u7801\u5e76\u4e0d\u4f1a\u7b49\u5f85\uff0c\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5fae\u4efb\u52a1\n- \u53ef\u4ee5\u8fd9\u6837\u7406\u89e3\uff0casync \u4f1a\u9690\u5f0f\u8fd4\u56de promise \u4f5c\u4e3a\u7ed3\u679c\u7684\u51fd\u6570\uff0cawait \u540e\u9762\u7684\u51fd\u6570\u6267\u884c\u5b8c\uff0c\u4f1a\u4ea7\u751f\u4e00\u4e2a promise.then\uff0c\u4e5f\u5c31\u662f\u5fae\u4efb\u52a1\n- async2\n- \u7ee7\u7eed\u540c\u6b65\u4ee3\u7801\u6267\u884c\n- promise1\n- scripts end\n- \u540c\u6b65\u4ee3\u7801\u6267\u884c\u5b8c\u6bd5\uff0c\u5f00\u59cb\u770b\u5fae\u4efb\u52a1\u961f\u5217\n- \u56e0\u4e3a\u4e0a\u9762 async/await \u7684\u539f\u56e0\uff0cawait \u540e\u9762\u6253\u5370\u7684\u4f1a\u88ab\u63a8\u5165\u81f3\u5fae\u4efb\u52a1\u961f\u5217\n- async1c end\n- promise2\n- \u5fae\u4efb\u52a1\u961f\u5217\u6267\u884c\u5b8c\u6bd5\uff0c\u73b0\u5728\u5f00\u59cb\u6267\u884c\u5b8f\u4efb\u52a1\u961f\u5217\n- setTimeout", "answer": "", "biz_type": 1, "qid": 138, "subject": "```js\nasync function async1() {\n console.log(\"async1 start\");\n await async2();\n console.log(\"async1 end\");\n}\nasync function async2() {\n console.log(\"async2\");\n}\nconsole.log(\"script start\");\nsetTimeout(function () {\n console.log(\"setTimeout\");\n}, 0);\nasync1();\nnew Promise(function (resolve) {\n console.log(\"promise1\");\n resolve();\n}).then(function () {\n console.log(\"promise2\");\n});\nconsole.log(\"scripts end\");\n// \u5199\u51fa\u4ee3\u7801\u6267\u884c\u5b8c\u6210\u6253\u5370\u7684\u7ed3\u679c\n```\n", "title": "\u5199\u51fa\u4ee3\u7801\u6267\u884c\u7ed3\u679c"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u8001\u864e"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 4, "qid": 117, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n```js\nimport React, { useState } from 'react';\n\nconst Student = () => {\n const [name, setName] = useState('');\n const [sex, setSex] = useState('');\n const [score, setScore] = useState('');\n const [habby, setHabby] = useState('FE');\n const handleClick = () => {\n if (name && sex && score) {\n alert(score);\n }\n };\n return (\n <>\n <input\n placeholder=\"\u8bf7\u8f93\u5165\u5b66\u751f\u59d3\u540d\"\n value={name}\n onChange={(e) => setName(e.target.value)}/>\n <input\n placeholder=\"\u8bf7\u8f93\u5165\u5b66\u751f\u6027\u522b\"\n value={sex}\n onChange={(e) => setSex(e.target.value)}/>\n <input\n placeholder=\"\u8bf7\u8f93\u5165\u5b66\u751f\u5206\u6570\"\n value={score}\n onChange={(e) => setScore(e.target.value)}/>\n <input\n placeholder=\"\u8bf7\u8f93\u5165\u5b66\u751f\u7231\u597d\"\n value={habby}\n onChange={(e) => setHabby(e.target.value)}/>\n <button onClick={handleClick} />\n </>\n );\n};\n\nexport default Student;\n```", "answer": "", "biz_type": 1, "qid": 117, "subject": "", "title": "\u8bbe\u8ba1\u4e00\u4e2a Student \u7ec4\u4ef6\uff0c\u5b9e\u73b0\u8f93\u5165\u59d3\u540d\u6027\u522b\u6210\u7ee9\uff08\u8fd9\u4e09\u4e2a\u5fc5\u586b\uff09\uff0c\u8fd8\u6709\u51e0\u4e2a\u4e0d\u662f\u5fc5\u586b\u7684\u5c5e\u6027\uff0c\u8981\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff0c\u70b9\u51fb\u5f39\u51fa\u6210\u7ee9"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u5934\u6761"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 13, "qid": 134, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n```js\nfunction sortVersion(list){\n return list.sort((a, b) => {\n let aa = a.split('.') \n let bb = b.split('.') \n let length = aa.length>bb.length?aa.length:bb.length\n for (var i =0; i < length; i++){\n let x = aa[i] || 0;\n let y = bb[i] || 0;\n if(x-y !==0 )return x - y;\n }\n });\n}\nsortVersion(['1.0.0', '1.2.3.4.5','2.12.1', '0.18.1','3.3.2','0.18.1'])\n```", "answer": "", "biz_type": 1, "qid": 134, "subject": "```js\nvar versions = [\"1.45.0\", \"1.5\", \"6\", \"3.3.3.3.3.3.3\"];\n// \u8981\u6c42\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff0c\u6ce8\u610f'1.45'\u6bd4'1.5'\u5927\nfunction sortVersion(versions) {\n // TODO\n}\n// => ['1.5','1.45.0','3.3.3.3.3.3','6']\n```\n", "title": "versions \u662f\u4e00\u4e2a\u9879\u76ee\u7684\u7248\u672c\u53f7\u5217\u8868\uff0c\u56e0\u591a\u4eba\u7ef4\u62a4\uff0c\u4e0d\u89c4\u5219\uff0c\u52a8\u624b\u5b9e\u73b0\u4e00\u4e2a\u7248\u672c\u53f7\u5904\u7406\u51fd\u6570"}, "tech_tag": ["JavaScript", "\u7f16\u7a0b\u9898"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u8001\u864e"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 5, "qid": 118, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n```js\nconst fn = () => {\n (fn.count & 1) === 0 ? console.log(2) : console.log(1)\n fn.count++\n}\nfn.count = 1\n```", "answer": "", "biz_type": 1, "qid": 118, "subject": "", "title": "\u8bbe\u8ba1\u4e00\u4e2a\u51fd\u6570\uff0c\u5947\u6570\u6b21\u6267\u884c\u7684\u65f6\u5019\u6253\u5370 1\uff0c\u5076\u6570\u6b21\u6267\u884c\u7684\u65f6\u5019\u6253\u5370 2"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u987a\u4e30"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 4, "qid": 124, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4ee3\u7801\u5b9e\u73b0\n\n- \u65b9\u5f0f\u4e00\n\n```js\n[1,2,3,4,5,10,12,13].reduce((acc, cur, index, arr) => {\n if(cur > 10){\n acc += cur;\n }\n return acc;\n}, 0);\n```\n\n- \u65b9\u5f0f\u4e8c\n\n```js\neval([1,2,3,4,5,10,12,13].filter(item=>item > 10).join('-').replace('-','+'));\n```", "answer": "", "biz_type": 1, "qid": 124, "subject": "", "title": "\u7528\u5c3d\u91cf\u77ed\u7684\u4ee3\u7801\u5b9e\u73b0\u4e00\u4e2a arrary \u7684\u94fe\u5f0f\u64cd\u4f5c\uff0c\u5c06\u6570\u7ec4\u4e2d\u7684\u5927\u4e8e 10 \u7684\u503c\u8fdb\u884c\u4e00\u4e2a\u7d2f\u52a0"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u4f34\u9c7c", "\u767e\u5206\u70b9", "\u5fc3\u5a31"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 5, "qid": 81, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4e00\u3001\u7b80\u6d01\u7248\n\n1. var\u58f0\u660e\u7684\u53d8\u91cf\u4f1a\u6302\u8f7d\u5728window\u4e0a\uff0c\u800clet\u548cconst\u58f0\u660e\u7684\u53d8\u91cf\u4e0d\u4f1a\n2. var\u58f0\u660e\u53d8\u91cf\u5b58\u5728\u53d8\u91cf\u63d0\u5347\uff0clet\u548cconst\u4e0d\u5b58\u5728\u53d8\u91cf\u63d0\u5347\n3. let\u548cconst\u58f0\u660e\u5f62\u6210\u5757\u4f5c\u7528\u57df\n4. \u540c\u4e00\u4f5c\u7528\u57df\u4e0blet\u548cconst\u4e0d\u80fd\u58f0\u660e\u540c\u540d\u53d8\u91cf\uff0c\u800cvar\u53ef\u4ee5\n5. const\u4e00\u65e6\u58f0\u660e\u5fc5\u987b\u8d4b\u503c,\u4e0d\u80fd\u4f7f\u7528null\u5360\u4f4d\uff1b\u58f0\u660e\u540e\u4e0d\u80fd\u518d\u4fee\u6539 \uff1b\u5982\u679c\u58f0\u660e\u7684\u662f\u590d\u5408\u7c7b\u578b\u6570\u636e\uff0c\u53ef\u4ee5\u4fee\u6539\u5176\u5c5e\u6027\n\n### \u4e8c\u3001\u8be6\u7ec6\u7248\n\n#### 1.var\n\n**var\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u6ca1\u6709\u5757\u7684\u6982\u5ff5\uff0c\u53ef\u4ee5\u8de8\u5757\u8bbf\u95ee, \u4e0d\u80fd\u8de8\u51fd\u6570\u8bbf\u95ee\uff08\u5982\u679c\u4f60\u5b9a\u4e49\u4e3a\u5168\u5c40\u53d8\u91cf\uff0c\u662f\u53ef\u4ee5\u8de8\u51fd\u6570\u8bbf\u95ee\u7684\uff09\uff0c\u6709\u53d8\u91cf\u63d0\u5347\uff08\u662f\u6307\u53d8\u91cf\u91cd\u65b0\u8d4b\u503c\uff09\u3002**\n\n```js\nvar a = 1;\n// var a;//\u4e0d\u4f1a\u62a5\u9519\nconsole.log('\u51fd\u6570\u5916var\u5b9a\u4e49a\uff1a' + a);//\u53ef\u4ee5\u8f93\u51faa=1\nfunction change(){\na = 4;\nconsole.log('\u51fd\u6570\u5185var\u5b9a\u4e49a\uff1a' + a);//\u53ef\u4ee5\u8f93\u51faa=4\n}\nchange();\nconsole.log('\u51fd\u6570\u8c03\u7528\u540evar\u5b9a\u4e49a\u4e3a\u51fd\u6570\u5185\u90e8\u4fee\u6539\u503c\uff1a' + a);//\u53ef\u4ee5\u8f93\u51faa=4\n```\n\n#### 2.let\n\n**let\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u53ea\u80fd\u5728\u5757\u4f5c\u7528\u57df\u91cc\u8bbf\u95ee\uff0c\u4e0d\u80fd\u8de8\u5757\u8bbf\u95ee\uff0c\u4e5f\u4e0d\u80fd\u8de8\u51fd\u6570\u8bbf\u95ee\uff0c\u65e0\u53d8\u91cf\u63d0\u5347\uff0c\u4e0d\u53ef\u4ee5\u91cd\u590d\u58f0\u660e\u3002**\n\n```js\n'use strict';\nfunction func(args){\n if(true){\n let i = 6;\n console.log('inside: ' + i); //\u4e0d\u62a5\u9519\n }\n console.log('outside: ' + i); // \u62a5\u9519 \"i is not defined\"\n};\nfunc();\n```\n\nlet \u58f0\u660e\u7684\u53d8\u91cf\u53ea\u5728\u5757\u7ea7\u4f5c\u7528\u57df\u5185\u6709\u6548\n\n```js\n'use strict';\nfunction func(args){\n if(true){\n let i = 6;\n console.log('inside: ' + i); //\u4e0d\u62a5\u9519\n }\n console.log('outside: ' + i); // \u62a5\u9519 \"i is not defined\"\n};\nfunc();\n```\n\n\u4e0d\u5b58\u5728\u53d8\u91cf\u63d0\u5347\uff0c\u800c\u662f\u201c\u7ed1\u5b9a\u201d\u5728\u6682\u65f6\u6027\u6b7b\u533a\n\n```js\n// \u4e0d\u5b58\u5728\u53d8\u91cf\u63d0\u5347\n'use strict';\nfunction func(){\n console.log(i);\n let i;\n};\nfunc(); // \u62a5\u9519\n```\n\n\u5728let\u58f0\u660e\u53d8\u91cf\u524d\uff0c\u4f7f\u7528\u8be5\u53d8\u91cf\uff0c\u5b83\u662f\u4f1a\u62a5\u9519\u7684\uff0c\u800c\u4e0d\u662f\u50cfvar\u90a3\u6837\u4f1a\u2018\u53d8\u91cf\u63d0\u5347\u2019\u3002 \u5176\u5b9e\u8bf4let\u6ca1\u6709\u2018\u53d8\u91cf\u63d0\u5347\u2019\u7684\u7279\u6027\uff0c\u4e0d\u592a\u5bf9\u3002\u6216\u8005\u8bf4\u5b83\u63d0\u5347\u4e86\uff0c\u4f46\u662fES6\u89c4\u5b9a\u4e86\u5728let\u58f0\u660e\u53d8\u91cf\u524d\u4e0d\u80fd\u4f7f\u7528\u8be5\u53d8\u91cf\u3002\n\n```js\n'use strict';\nvar test = 1;\nfunction func(){\n console.log(test);\n let test = 2;\n};\nfunc(); // \u62a5\u9519\n```\n\n\u5982\u679clet\u58f0\u660e\u7684\u53d8\u91cf\u6ca1\u6709\u53d8\u91cf\u63d0\u5347\uff0c\u5e94\u8be5\u6253\u5370\u20191\u2019\uff08func\u51fd\u6570\u5916\u7684test\uff09\uff1b\u800c\u5b83\u5374\u62a5\u9519\uff0c\u8bf4\u660e\u5b83\u662f\u63d0\u5347\u4e86\u7684\uff0c\u53ea\u662f\u89c4\u5b9a\u4e86\u4e0d\u80fd\u5728\u5176\u58f0\u660e\u4e4b\u524d\u4f7f\u7528\u800c\u5df2\u3002\u6211\u4eec\u79f0\u8fd9\u7279\u6027\u53eb\u201c\u6682\u65f6\u6027\u6b7b\u533a\uff08temporal dead zone\uff09\u201d\u3002\u4e14\u8fd9\u4e00\u7279\u6027\uff0c\u4ec5\u5bf9\u9075\u5faa\u2018\u5757\u7ea7\u4f5c\u7528\u57df\u2019\u7684\u547d\u4ee4\u6709\u6548\uff08let\u3001const\uff09\u3002\n\n#### 3.const\n\nconst\u7528\u6765\u5b9a\u4e49\u5e38\u91cf\uff0c\u4f7f\u7528\u65f6\u5fc5\u987b\u521d\u59cb\u5316(\u5373\u5fc5\u987b\u8d4b\u503c)\uff0c\u53ea\u80fd\u5728\u5757\u4f5c\u7528\u57df\u91cc\u8bbf\u95ee\uff0c\u800c\u4e14\u4e0d\u80fd\u4fee\u6539\uff0c\u65e0\u53d8\u91cf\u63d0\u5347\uff0c\u4e0d\u53ef\u4ee5\u91cd\u590d\u58f0\u660e\u3002\n\nconst \u4e0e let \u7684\u4f7f\u7528\u89c4\u8303\u4e00\u6837\uff0c\u4e0e\u4e4b\u4e0d\u540c\u7684\u662f\uff1aconst \u58f0\u660e\u7684\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u4e14\u8fd9\u4e2a\u5e38\u91cf\u5fc5\u987b\u8d4b\u503c\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002\n\n**\u6ce8\u610f:** const\u5e38\u91cf\uff0c\u6307\u7684\u662f\u5e38\u91cf\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\u4e0d\u5f97\u6539\u53d8\uff0c\u800c\u4e0d\u662f\u5bf9\u5e94\u7684\u503c\u4e0d\u5f97\u6539\u53d8\uff0c\u6240\u6709\u628a\u5e94\u7528\u7c7b\u578b\u7684\u6570\u636e\u8bbe\u7f6e\u4e3a\u5e38\u91cf\uff0c\u5176\u5185\u90e8\u7684\u503c\u662f\u53ef\u4ee5\u6539\u53d8\u7684\uff0c\u4f8b\u5982\uff1aconst a={}; a.b=13;//\u4e0d\u4f1a\u62a5\u9519 const arr=[]; arr.push(123);//\u4e0d\u4f1a\u62a5\u9519\n\n```js\n'use strict';\nfunction func(){\n const PI;\n PI = 3.14;\n console.log(PI);\n};\nfunc(); // \u62a5\u9519\u201cMissing initializer in const declaration\u201d\n```\n", "answer": "", "biz_type": 1, "qid": 81, "subject": "", "title": "var\u3001let\u3001const \u7684\u533a\u522b"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}, {"audit_flag": 0, "corp_tag": ["\u987a\u4e30"], "date": "Fri, 19 Jun 2020 16:20:40 GMT", "favorite_num": 4, "qid": 126, "qtype": "short_answer", "short_answer": {"analysis": "\n### \u4e00\u3001B \u548c C \u7684\u542b\u4e49\n\n`B`\u5176\u5b9e\u662f`Bussiness`\u7684\u7f29\u5199\uff0c\u76f4\u8bd1\u7684\u610f\u601d\u5c31\u662f\u5546\u4e1a\uff0c\u5927\u5bb6\u666e\u904d\u628a\u5b83\u4f5c\u4e3a\u673a\u6784\u5ba2\u6237\u7684\u4ee3\u540d\u8bcd\u3002`C`\u5176\u5b9e\u662f`Consumer` \u7684\u7f29\u5199\uff0c\u76f4\u8bd1\u7684\u610f\u601d\u662f\u6d88\u8d39\u8005\u3001\u7528\u6237\u6216\u8005\u987e\u5ba2\uff0c\u56e0\u4e3a\u65e5\u5e38\u751f\u6d3b\u4e2d\u63a5\u89e6\u7684\u7edd\u5927\u90e8\u5206\u76f4\u63a5\u6d88\u8d39\u8005\u90fd\u662f\u4e2a\u4eba\uff0c\u6240\u4ee5\u5927\u5bb6\u666e\u904d\u628a`C`\u4f5c\u4e3a\u4e2a\u4eba\u5ba2\u6237\u7684\u4ee3\u540d\u8bcd\u3002\n\n- To B \u5c31\u662f To business\uff0c\u9762\u5411\u4f01\u4e1a\u6216\u8005\u7279\u5b9a\u7528\u6237\u7fa4\u4f53\u7684\u9762\u5546\u7c7b\u4ea7\u54c1\n- To C \u5c31\u662f To customer\uff0c\u4ea7\u54c1\u9762\u5411\u6d88\u8d39\u8005\n- To C \u4ea7\u54c1\u662f\u4f60\u53bb\u6316\u6398\u7528\u6237\u9700\u6c42\uff0c\u662f\u521b\u9020\uff0c\u4ece\u65e0\u5230\u6709\uff1b\u66f4\u6ce8\u91cd\u7684\u662f\u7528\u6237\u4f53\u9a8c\n- To B \u4ea7\u54c1\u662f\u516c\u53f8\u6218\u7565\u6216\u76f8\u5173\u65b9\u7ed9\u4f60\u63d0\u51fa\u8981\u6c42\uff0c\u4ea7\u54c1\u7ecf\u7406\u5c06\u8fd9\u7c7b\u300c\u7ebf\u4e0b\u5df2\u6709\u7684\u9700\u6c42\u300d\u7cfb\u7edf\u5316\uff0c\u8fbe\u5230\u63d0\u9ad8\u73b0\u6709\u6d41\u7a0b\u7684\u6548\u7387\u7684\u76ee\u7684\u3002\u4ea7\u54c1\u66f4\u6ce8\u91cd\u7684\u662f\u529f\u80fd\u4ef7\u503c\u4ee5\u53ca\u7cfb\u7edf\u6027\n\n### \u4e8c\u3001ToB \u548c ToC \u9700\u8981\u6ce8\u610f\u7684\u70b9\n\n1. **\u4e1a\u52a1\u5f62\u6001\u4e0d\u540c**\u3002`ToC`\u7684\u9700\u6c42\u66f4\u591a\u7684\u662f\u56f4\u7ed5\u8863\u98df\u4f4f\u884c\u6765\u5c55\u5f00\u7684,ToB \u7684\u9700\u6c42\u66f4\u591a\u7684\u662f\u56f4\u7ed5\u673a\u6784\u6240\u5904\u7684\u67d0\u4e2a\u884c\u4e1a\u6216\u8005\u67d0\u4e2a\u9886\u57df\u6765\u5c55\u5f00\u7684\uff0c\u573a\u666f\u66f4\u4e3a\u590d\u6742\u591a\u6837\u3002\u603b\u4e4b\uff0c`ToC`\u662f**\u751f\u6d3b**\uff0c\u662f**\u56e0\u70b9\u751f\u70b9**\uff1b`ToB`\u662f\u9762\u5411\u751f\u4ea7\uff0c\u662f\u56e0**\u56e0\u9762\u751f\u70b9**\n2. **\u4ea7\u54c1\u9700\u6c42\u4e0d\u540c**\u3002`ToC`\u4f5c\u4e3a\u72ec\u7acb\u7684\u4e2a\u4f53\u6240\u5b58\u5728\uff0c\u9762\u5411\u7684\u7528\u6237\u662f\u66f4\u52a0\u5927\u4f17\u7684\uff1a90 \u540e\u7684\u53d4\u53d4\u963f\u59e8\uff0c00 \u540e\uff0c10 \u540e\uff0c\u5bf9\u4ea7\u54c1\u7684\u9700\u6c42\u662f\u662f\u529f\u80fd\u7684\u5916\u90e8\u5316\uff0c\u4f53\u9a8c\u548c\u89c6\u89c9\u6548\u679c\u8981\u597d\u3002`ToB`\u66f4\u591a\u7684\u8003\u8651\u7684\u662f\u4ea7\u54c1\u7684\u529f\u80fd\u65f6\u6548\u5316\u4ee5\u53ca\u4e13\u4e1a\u5316\uff0c\u8981\u6c42\u4ea7\u54c1\u80fd\u591f\u5b9e\u5b9e\u5728\u5728\u7684\u4ef7\u503c\u548c\u6548\u7528\n3. **\u4ea7\u54c1\u5355\u4f53\u6d88\u8d39\u91cf\u4e0d\u540c**\u3002`ToC`\u7684\u6d88\u8d39\u80fd\u529b\u4e00\u822c\u60c5\u51b5\u4e0b\u8981\u8fdc\u5c0f\u4e8e`ToB`\u7684\u6d88\u8d39\u80fd\u529b.\u6240\u4ee5\u4e00\u822c`ToC`\u9700\u8981\u4e00\u5b9a\u7684\u5ba2\u6237\u7fa4\u4f53\u624d\u80fd\u652f\u6491\u8d77\u4e00\u4e2a\u4e1a\u52a1\u7684\u53d1\u5c55\n4. **\u6027\u4ef7\u6bd4\u8981\u6c42\u4e0d\u540c**\u3002`ToC`\u5bf9\u4e8e\u6027\u4ef7\u6bd4\u7684\u5b9e\u9645\u8981\u6c42\u8981\u8fdc\u4f4e\u4e8e\u7406\u8bba\u9700\u6c42\uff0c\u56e0\u4e3a`ToC`\u5927\u591a\u6570\u65e0\u6cd5\u5177\u6709\u51c6\u786e\u5206\u6790`\u6027`\u7684\u4e13\u4e1a\u80fd\u529b\uff0c\u6240\u4ee5\u4ea7\u751f\u4e86**\u8d35\u7684\u5c31\u662f\u597d\u7684**\u7684\u7406\u8bba\uff0c\u76f8\u6bd4\u800c\u8a00`ToB`\u5177\u6709\u76f8\u5bf9\u4e13\u4e1a\u5224\u65ad\u80fd\u529b\uff0c\u800c\u4e14\u9700\u8981\u8003\u8651\u5230\u6295\u5165/\u4ea7\u51fa\u6bd4\uff0c\u540c\u65f6\u5177\u6709\u9700\u8981\u5b9e\u73b0\u6027\u4ef7\u6bd4\u7684\u610f\u613f\u548c\u80fd\u529b\n5. **\u5e02\u573a\u58f0\u8a89\u5f71\u54cd\u529b\u4e0d\u540c**\u3002\u4e2a\u4eba\u66f4\u5bb9\u6613\u53d7\u5916\u90e8\u73af\u5883\u7684\u5f71\u54cd\u800c\u505a\u51fa\u4e0d\u9002\u5408\u81ea\u5df1\u7684\u51b3\u7b56\u3002\u5177\u6709\u5f88\u597d\u5e02\u573a\u58f0\u8a89\uff08\u5e02\u573a\u66dd\u5149\u5ea6\u6bd4\u8f83\u9ad8\uff09\u7684\u4f01\u4e1a\uff0c\u5f80\u5f80\u5bf9`ToC`\u66f4\u5177\u6709\u5438\u5f15\u529b\u3002\u8fd9\u4e2a\u5c31\u662f\u4ece\u4f17\u5fc3\u7406\u7684\u201c\u5999\u7528\u201d\u3002\u53cd\u4e4b`ToB`\u9664\u4e86\u4f1a\u8003\u8651\u5e02\u573a\u58f0\u8a89\uff08\u8fd9\u4e2a\u53ef\u80fd\u662f\u8fdb\u5165\u95e8\u69db\uff09\uff0c\u66f4\u591a\u7684\u4f1a\u5173\u6ce8\u4ea7\u54c1\u672c\u8eab\u7684\u8d28\u91cf\u3001\u6548\u80fd\uff0c\u7136\u540e\u7efc\u5408\u8bc4\u4f30\u4ea7\u54c1\u7684\u53ef\u8d2d\u4e70\u6027\u3002\u4e00\u53e5\u8bdd\u603b\u7ed3\uff0c`ToC`**\u611f\u6027**\uff0c`ToB`**\u7406\u6027**\u3002\n6. **\u552e\u540e\u8981\u6c42\u4e0d\u540c**\u3002`ToC`\u5bf9\u4ea7\u54c1\u7684\u552e\u540e\u8981\u6c42\u4e00\u822c\u6bd4\u8f83\u4f4e\uff0c\u5546\u5bb6\u4e5f\u591a\u662f\u6807\u51c6\u5316\u670d\u52a1\uff0c\u5176\u4e2d\u7f18\u7531\u4e00\u662f\u4ea7\u54c1\u7684\u751f\u547d\u5468\u671f\u504f\u77ed\uff0c\u5f88\u591a\u5c5e\u4e8e\u201c\u9605\u540e\u5373\u711a\u201d\u7c7b\u578b\u7684\u4ea7\u54c1\uff1b\u4e8c\u662f\u56e0\u4e3a\u4ea7\u54c1\u7684\u6280\u672f\u542b\u91cf\u5e76\u4e0d\u5341\u5206\u590d\u6742\uff0c\u5927\u5bb6\u53ef\u4ee5`DIY`\u7ef4\u4fee\u3002\u53cd\u89c2`ToB`\uff0c\u5176\u5bf9\u4ea7\u54c1\u7684\u552e\u540e\u8981\u6c42\u666e\u904d\u504f\u9ad8\uff0c\u4e0d\u4ec5\u56e0\u4e3a\u91cf\u5927\uff0c\u800c\u4e14\u56e0\u4e3a\u590d\u6742\u3002\u6240\u4ee5`ToB`\u7684\u884c\u4e1a\u4e00\u822c\u90fd\u5efa\u7acb\u5b8c\u5907\u7684\u5ba2\u6237\u670d\u52a1\u7cfb\u7edf\uff0c\u5305\u62ec\u4ea7\u54c1\u7ef4\u4fee\u3001\u54a8\u8be2\u3001\u6295\u8bc9\u7b49\u7b49\u3002\n7. **\u51b3\u7b56\u4f53\u7cfb\u4e0d\u540c**\u3002`ToC`\u7684\u51b3\u7b56\u76f8\u5bf9\u7b80\u5355\uff0c\u5bf9\u4e8e\u662f\u5426\u8d2d\u4e70\uff0c\u6709\u65f6\u5019\u53ea\u9700\u8981\u4e00\u4e2a\u4eba\u5c31\u80fd\u51b3\u5b9a\uff0c`ToB`\u7684\u51b3\u7b56\u6d41\u7a0b\u76f8\u5bf9\u590d\u6742\u4ece\u90e8\u95e8\u7684\u76f8\u4e92\u5408\u4f5c\uff0c\u7ecf\u8fc7`boss`\u7b49\u7b49\uff0c\u6240\u4ee5\u9700\u8981\u7684\u65f6\u95f4\u4e5f\u76f8\u5bf9\u8f83\u957f\n8. **\u53ef\u62d3\u5c55\u6027\u4e0d\u540c**\u3002\u6240\u8c13\u7684\u53ef\u62d3\u5c55\u6027\uff0c\u6307\u7684\u5c31\u662f\u9664\u4e86\u4e00\u79cd\u5546\u54c1\u4e4b\u5916\uff0c\u662f\u5426\u8fd8\u6709\u5411\u5176\u51fa\u552e\u66f4\u591a\u5546\u54c1\u7684\u53ef\u884c\u6027\u3002\u8fd9\u4e2a\u5728\u6d41\u91cf\u7ecf\u6d4e\u65f6\u4ee3\u5c24\u4e3a\u91cd\u8981\u3002\u57fa\u4e8e\u4ee5\u4e0a\u6240\u8ff0\u7684\u79cd\u79cd\u5dee\u522b\u548c\u7279\u6b8a\u5c5e\u6027\u6765\u770b\uff0c`ToC`\u7684\u53ef\u62d3\u5c55\u6027\u8f83\u5f3a\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4ee5\u70b9\u5e26\u9762\uff0c\u4e00\u4ef6\u6781\u5177\u8bf1\u60d1\u529b\u7684\u4ea7\u54c1\u5c31\u53ef\u80fd\u628a\u5ba2\u6237\u7262\u7262\u6293\u4f4f\uff0c\u6bd4\u5982\u5916\u5356\u3002\u8fd9\u4e2a\u4e5f\u662f\u4e3a\u4ec0\u4e48\u73b0\u5728\u5f88\u591a\u4eba\u770b\u597d\u7f8e\u56e2\u7684\u539f\u56e0\u3002`ToB`\u7684\u53ef\u62d3\u5c55\u6027\u504f\u5f31\uff0c\u53ea\u80fd\u5b9e\u73b0\u4ee5\u70b9\u5e26\u70b9\uff0c\u9762\u4e0a\u7684\u6a2a\u5411\u62d3\u5c55\u96be\u5ea6\u5f88\u5927\uff0c\u7eb5\u5411\u4ea7\u4e1a\u94fe\u8fd8\u6709\u4e9b\u53ef\u80fd\u3002\n\n### \u4e09\u3001\u9677\u9631\n\n\u4e2a\u4eba\u5ba2\u6237\u662f ToC \u7684\u8303\u7574\uff0c\u4f46\u662f\u673a\u6784\u5ba2\u6237\u5c31\u4e00\u5b9a\u5c5e\u4e8e ToB \u4e48\uff1f\u5176\u5b9e\u672a\u5fc5\u3002\u5f88\u591a\u4f60\u6240\u8c13\u7684 B \u7aef\u5ba2\u6237\uff0c\u5176\u5b9e\u53ea\u80fd\u7b97\u4f5c\u662f\u5927 C \u5ba2\u6237\uff01\uff08\u5305\u62ec\u4e00\u90e8\u5206\u4e2d\u5c0f\u578b\u4f01\u4e1a\uff09\n", "answer": "", "biz_type": 1, "qid": 126, "subject": "", "title": "\u600e\u4e48\u7406\u89e3 to B \u548c to C \u7684\u4e1a\u52a1"}, "tech_tag": ["JavaScript"], "uid": 0, "uname": ""}];
1
https://gitee.com/github-26497262/interview.git
git@gitee.com:github-26497262/interview.git
github-26497262
interview
interview
master

搜索帮助