{"id":2006,"date":"2018-08-23T16:02:11","date_gmt":"2018-08-23T08:02:11","guid":{"rendered":"https:\/\/feizhaojun.com\/?p=2006"},"modified":"2018-08-23T18:25:03","modified_gmt":"2018-08-23T10:25:03","slug":"2006","status":"publish","type":"post","link":"https:\/\/feizhaojun.com\/?p=2006","title":{"rendered":"JavaScript\u7684api\u8bbe\u8ba1\u539f\u5219"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<p>\u672c\u7bc7\u535a\u6587\u6765\u81ea\u4e00\u6b21\u516c\u53f8\u5185\u90e8\u7684\u524d\u7aef\u5206\u4eab\uff0c\u4ece\u591a\u4e2a\u65b9\u9762\u8ba8\u8bba\u4e86\u5728\u8bbe\u8ba1\u63a5\u53e3\u65f6\u9075\u5faa\u7684\u539f\u5219\uff0c\u603b\u5171\u5305\u542b\u4e86\u4e03\u4e2a\u5927\u5757\u3002\u7cfb\u5364\u716e\u81ea\u5df1\u603b\u7ed3\u7684\u4e00\u4e9b\u7ecf\u9a8c\u548c\u6559\u8bad\u3002\u672c\u7bc7\u535a\u6587\u540c\u65f6\u4e5f\u53c2\u8003\u4e86\u5176\u4ed6\u4e00\u4e9b\u6587\u7ae0\uff0c\u76f8\u5173\u5730\u5740\u4f1a\u5728\u540e\u9762\u8d34\u51fa\u6765\u3002\u5f88\u96be\u505a\u5230\u8be6\u5c3d\u5145\u5b9e\uff0c\u5982\u679c\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u4e0d\u5bf9\u7684\u5730\u65b9\uff0c\u8fd8\u671b\u4e0d\u541d\u8d50\u6559\u65a7\u6b63\u3002<\/p>\n<h2>\u4e00 \u63a5\u53e3\u7684\u6d41\u7545\u6027<\/h2>\n<p>\u597d\u7684\u63a5\u53e3\u662f\u6d41\u7545\u6613\u61c2\u7684\uff0c\u4ed6\u4e3b\u8981\u4f53\u73b0\u5982\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n<h3>1 \u7b80\u5355<\/h3>\n<p>\u64cd\u4f5c\u67d0\u4e2a\u5143\u7d20\u7684css\u5c5e\u6027\uff0c\u4e0b\u9762\u662f\u539f\u751f\u7684\u65b9\u6cd5:<\/p>\n<pre><code class=\"language-js\">document.querySelector('#id').style.color = 'red';\n<\/code><\/pre>\n<p>\u5c01\u88c5\u4e4b\u540e<\/p>\n<pre><code class=\"language-js\">function a(selector, color) {\n  document.querySelector(selector).style.color = color\n}\na('#a', 'red');\n<\/code><\/pre>\n<p>\u4ece\u51e0\u5341\u4e2a\u5b57\u6bcd\u957f\u957f\u7684\u4e00\u884c\u5230\u7b80\u7b80\u5355\u5355\u7684\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\uff0c\u4f53\u73b0\u4e86api\u8bbe\u8ba1\u539f\u5219\u4e4b\u4e00\uff1a\u7b80\u5355\u6613\u7528\u3002<\/p>\n<h3>2 \u53ef\u9605\u8bfb\u6027<\/h3>\n<p>a('#a', 'red')\u662f\u4e2a\u597d\u51fd\u6570\uff0c\u5e2e\u52a9\u6211\u4eec\u7b80\u5355\u5b9e\u7528\u5730\u6539\u53d8\u67d0\u4e2a\u5143\u7d20\uff0c\u4f46\u95ee\u9898\u6765\u4e86\uff0c\u5982\u679c\u7b2c\u4e00\u6b21\u4f7f\u7528\u8be5\u51fd\u6570\u7684\u4eba\u6765\u8bf4\u4f1a\u6bd4\u8f83\u56f0\u60d1\uff0ca\u51fd\u6570\u662f\u5565\u51fd\u6570\uff0c\u6ca1\u6709\u4eba\u544a\u8bc9\u4ed6\u3002\u5f00\u53d1\u63a5\u53e3\u6709\u5fc5\u8981\u77e5\u9053\u4e00\u70b9\uff0c\u5927\u591a\u6570\u4eba\u90fd\u662f\u61d2\u60f0\u7684\uff08\u5305\u62ec\u5364\u716e\u81ea\u5df1\uff09\uff0c\u4ece\u989c\u8272\u8d4b\u503c\u8fd9\u4e2a\u51fd\u6570\u6765\u8bf4\uff0c\u867d\u7136\u5c11\u5199\u4e86\u4ee3\u7801\uff0c\u4f46\u662f\u589e\u52a0\u4e86\u5355\u8bcd\u5b57\u6bcd\u7684\u4e2a\u6570\uff0c\u4f7f\u5f97\u5b83\u4e0d\u518d\u597d\u8bb0\u3002\u6bcf\u6b21\u505a\u8fd9\u4ef6\u4e8b\u60c5\u7684\u65f6\u5019\u90fd\u9700\u8981\u6709\u6620\u5c04\u5173\u7cfb\uff1a a---->color. \u5982\u679c\u662f\u7b80\u5355\u7684\u51e0\u4e2aapi\u5012\u662f\u65e0\u6240\u8c13\uff0c\u4f46\u662f\u901a\u5e38\u4e00\u5957\u6846\u67b6\u90fd\u6709\u51e0\u5341\u751a\u81f3\u4e0a\u767e\u7684api\uff0c\u6620\u5c04\u6210\u672c\u589e\u52a0\u4f1a\u4f7f\u5f97\u7a0b\u5e8f\u5458\u54e5\u54e5\u5d29\u6e83\u3002 \u6211\u4eec\u9700\u8981\u7684\u5c31\u662f\u4f7f\u5f97\u63a5\u53e3\u540d\u79f0\u6709\u610f\u4e49\uff0c\u4e0b\u9762\u6211\u4eec\u6539\u5199\u4e00\u4e0ba\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-js\">function letSomeElementChangeColor(selector, color) {\n  document.querySelectorAll(selector, color).style.color = color; }\n<\/code><\/pre>\n<p>letSomeElementChangeColor\u76f8\u5bf9\u4e8ea\u6765\u8bf4\u88ab\u8d4b\u4e88\u4e86\u73b0\u5b9e\u8bed\u8a00\u4e0a\u7684\u610f\u4e49\uff0c\u4efb\u4f55\u4eba\u90fd\u4e0d\u9700\u8981\u770b\u8bf4\u660e\u4e5f\u80fd\u77e5\u9053\u5b83\u7684\u529f\u80fd\u3002<\/p>\n<h3>3 \u51cf\u5c11\u8bb0\u5fc6\u6210\u672c<\/h3>\n<p>\u6211\u4eec\u521a\u521a\u7684\u51fd\u6570\u592a\u957f\u4e86\uff0cletSomeElementChangeColor\u867d\u7136\u51cf\u5c11\u4e86\u6620\u5c04\u6210\u672c\uff0c\u6709\u4e86\u8bed\u8a00\u4e0a\u7684\u610f\u4e49\uff0c\u4f46\u662f\u6beb\u65e0\u7591\u95ee\u589e\u52a0\u4e86\u8bb0\u5fc6\u6210\u672c\u3002\u8981\u77e5\u9053\uff0c\u5305\u62ec\u5b66\u9738\u5728\u5185\uff0c\u4efb\u4f55\u4eba\u90fd\u4e0d\u559c\u6b22\u80cc\u5355\u8bcd\u3002\u4e0d\u4ec5\u4ec5\u5728\u6b64\u5904\uff0c\u539f\u751f\u83b7\u53d6dom\u7684api\u4e5f\u540c\u6837\u6709\u8fd9\u4e2a\u95ee\u9898\uff1a document.getElementsByClassName\uff1b document.getElementsByName; document.querySelectorAll;\u8fd9\u4e9bapi\u7ed9\u4eba\u7684\u611f\u89c9\u5c31\u662f\u5355\u8bcd\u592a\u957f\u4e86\uff0c\u867d\u7136\u4ed6\u7ed9\u51fa\u7684\u610f\u4e49\u662f\u5f88\u6e05\u6670\uff0c\u7136\u800c\u8fd9\u79cd\u505a\u6cd5\u662f\u5efa\u7acb\u5728\u727a\u7272\u7b80\u6613\u6027\u548c\u7b80\u5fc6\u6027\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u7684\u3002\u4e8e\u662f\u6211\u4eec\u53c8\u518d\u6b21\u6539\u5199\u8fd9\u4e2a\u4e4b\u524d\u51fd\u6570<\/p>\n<pre><code class=\"language-js\">function setColor(selector, color) {\n xxxxxxxxxxxx\n}\n<\/code><\/pre>\n<p>\u5728\u8bed\u8a00\u610f\u4e49\u4e0d\u505a\u5927\u7684\u53d8\u5316\u524d\u63d0\u4e0b\uff0c\u7f29\u51cf\u51fd\u6570\u540d\u79f0\u3002\u4f7f\u5f97\u5b83\u6613\u8bfb\u6613\u8bb0\u6613\u7528\u3002<\/p>\n<h3>4 \u53ef\u5ef6\u4f38<\/h3>\n<p>\u6240\u8c13\u5ef6\u4f38\u5c31\u662f\u6307\u51fd\u6570\u7684\u4f7f\u7528\u50cf\u6d41\u6c34\u4e00\u6837\u6309\u7167\u4e66\u5199\u7684\u987a\u5e8f\u6267\u884c\u5f62\u6210\u6267\u884c\u94fe\u6761:<\/p>\n<pre><code class=\"language-js\">document.getElementById('id').style.color = 'red';\ndocument.getElementById('id').style.fontSize = '12px';\ndocument.getElementById('id').style.backgourdColor = 'pink';\n<\/code><\/pre>\n<p>\u5982\u679c\u6211\u4eec\u9700\u8981\u5b9e\u73b0\u50cf\u4ee5\u4e0a\u6709\u5f3a\u5173\u8054\u6027\u7684\u4e1a\u52a1\u65f6\uff0c\u7528\u6211\u4eec\u4e4b\u524d\u7684\u4e4b\u524d\u7684\u65b9\u6cd5\u662f\u518d\u6b21\u5c01\u88c5\u4e24\u4e2a\u51fd\u6570 setFontSize, setbackgroundColor; \u7136\u540e\u6267\u884c\u5b83\u4eec setColor('id', 'red');setFontSiez('id', '12px'); setbackgroundColor('id', 'pink'); \u663e\u7136\uff0c\u8fd9\u6837\u7684\u505a\u6cd5\u6ca1\u6709\u61d2\u51fa\u5883\u754c\u6765\uff1bid\u5143\u7d20\u6bcf\u6b21\u90fd\u9700\u8981\u91cd\u65b0\u83b7\u53d6\uff0c\u5f71\u54cd\u6027\u80fd\uff0c\u5931\u8d25\uff1b\u6bcf\u6b21\u90fd\u9700\u8981\u6dfb\u52a0\u65b0\u7684\u65b9\u6cd5\uff0c\u5931\u8d25\uff1b \u6bcf\u6b21\u8fd8\u8981\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u8fd8\u662f\u5931\u8d25\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u5176\u6539\u5199\u4e3a\u53ef\u4ee5\u5ef6\u4f38\u7684\u51fd\u6570 \u9996\u5148\u5c06\u83b7\u53d6id\u65b9\u6cd5\u5c01\u88c5\u6210\u5bf9\u8c61,\u7136\u540e\u518d\u5bf9\u8c61\u7684\u6bcf\u4e2a\u65b9\u6cd5\u4e2d\u8fd4\u56de\u8fd9\u4e2a\u5bf9\u8c61\uff1a<\/p>\n<pre><code class=\"language-js\">function getElement(selector) {\n  this.style = document.querySelecotrAll(selector).style;\n}\n\ngetElement.prototype.color = function(color) {\n  this.style.color = color;\n  return this;\n}\ngetElement.prototype.background = function(bg) {\n  this.style.backgroundColor = bg;\n  return this;\n}\ngetElement.prototype.fontSize = function(size) {\n  this.style.fontSize = size;\n  return this;\n}\n\/\/\u8c03\u7528\nvar el = new getElement('#id')\nel.color('red').background('pink').fontSize('12px');\n<\/code><\/pre>\n<p>\u7b80\u5355\u3001\u6d41\u7545\u3001\u6613\u8bfb\uff0c\u5b83\u4eec\u770b\u8d77\u6765\u5c31\u50cf\u884c\u4e91\u6d41\u6c34\u4e00\u6837\uff0c\u5373\u5728\u4ee3\u7801\u6027\u80fd\u4e0a\u5f97\u5230\u4e86\u63d0\u5347\u4f18\u5316\uff0c\u53c8\u5728\u89c6\u89c9\u4e0a\u60a6\u76ee\u3002\u540e\u9762\u6211\u4eec\u4f1a\u5728\u53c2\u6570\u91cc\u9762\u8bb2\u5230\u5982\u4f55\u7ee7\u7eed\u4f18\u5316\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5927\u5bb6\u90fd\u6bd4\u8f83\u559c\u6b22\u7528jquery\u7684api\uff0c\u867d\u7136\u4e00\u4e2a$\u7b26\u53f7\u5e76\u4e0d\u4ee3\u8868\u4efb\u4f55\u73b0\u5b9e\u610f\u4e49\uff0c\u4f46\u7b80\u5355\u7684\u7b26\u53f7\u6709\u5229\u4e8e\u6211\u4eec\u7684\u4f7f\u7528\u3002\u5b83\u4f53\u73b0\u4e86\u4ee5\u4e0a\u7684\u591a\u79cd\u539f\u5219\uff0c\u7b80\u5355\uff0c\u6613\u8bfb\uff0c\u6613\u8bb0\uff0c\u94fe\u5f0f\u5199\u6cd5\uff0c\u591a\u53c2\u5904\u7406\u3002<\/p>\n<p><strong>nightmare:<\/strong><\/p>\n<pre><code class=\"language-js\">document.getElementById('id').style.color = 'red';\ndocument.getElementById('id').style.fontSize = '12px';\ndocument.getElementById('id').style.backgourdColor = 'pink';\n<\/code><\/pre>\n<p><strong>dream:<\/strong><\/p>\n<pre><code class=\"language-js\">$('id').css({color:'red', fontSize:'12px', backgroundColor:'pink'})\n<\/code><\/pre>\n<h2>\u4e8c \u4e00\u81f4\u6027<\/h2>\n<h3>1 \u63a5\u53e3\u7684\u4e00\u81f4\u6027<\/h3>\n<p>\u76f8\u5173\u7684\u63a5\u53e3\u4fdd\u6301\u4e00\u81f4\u7684\u98ce\u683c\uff0c\u4e00\u6574\u5957 API \u5982\u679c\u4f20\u9012\u4e00\u79cd\u719f\u6089\u548c\u8212\u9002\u7684\u611f\u89c9\uff0c\u4f1a\u5927\u5927\u51cf\u8f7b\u5f00\u53d1\u8005\u5bf9\u65b0\u5de5\u5177\u7684\u9002\u5e94\u6027\u3002 \u547d\u540d\u8fd9\u70b9\u4e8b\uff1a\u65e2\u8981\u77ed\uff0c\u53c8\u8981\u81ea\u63cf\u8ff0\uff0c\u6700\u91cd\u8981\u7684\u662f\u4fdd\u6301\u4e00\u81f4\u6027 \u201c\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u754c\u53ea\u6709\u4e24\u4ef6\u5934\u75bc\u7684\u4e8b\uff1a\u7f13\u5b58\u5931\u6548\u548c\u547d\u540d\u95ee\u9898\u201d \u2014 Phil Karlton \u9009\u62e9\u4e00\u4e2a\u4f60\u559c\u6b22\u7684\u63aa\u8f9e\uff0c\u7136\u540e\u6301\u7eed\u4f7f\u7528\u3002\u9009\u62e9\u4e00\u79cd\u98ce\u683c\uff0c\u7136\u540e\u4fdd\u6301\u8fd9\u79cd\u98ce\u683c\u3002<\/p>\n<p><strong>Nightmare:<\/strong><\/p>\n<pre><code class=\"language-js\">setColor,\nletBackGround\nchangefontSize\nmakedisplay\n<\/code><\/pre>\n<p><strong>dream:<\/strong><\/p>\n<pre><code class=\"language-js\">setColor;\nsetBackground;\nsetFontSize\nset.........\n<\/code><\/pre>\n<p>\u5c3d\u91cf\u5730\u4fdd\u6301\u4ee3\u7801\u98ce\u683c\u548c\u547d\u540d\u98ce\u683c\uff0c\u4f7f\u522b\u4eba\u8bfb\u4f60\u7684\u4ee3\u7801\u50cf\u662f\u9605\u8bfb\u540c\u4e00\u4e2a\u4eba\u5199\u7684\u6587\u7ae0\u4e00\u6837\u3002<\/p>\n<h2>\u4e09 \u53c2\u6570\u7684\u5904\u7406<\/h2>\n<h3>1 \u53c2\u6570\u7684\u7c7b\u578b<\/h3>\n<p>\u5224\u65ad\u53c2\u6570\u7684\u7c7b\u578b\u4e3a\u4f60\u7684\u7a0b\u5e8f\u63d0\u4f9b\u7a33\u5b9a\u7684\u4fdd\u969c<\/p>\n<pre><code class=\"language-js\">\/\/\u6211\u4eec\u89c4\u5b9a\uff0ccolor\u63a5\u53d7\u5b57\u7b26\u4e32\u7c7b\u578b\nfunction setColor(color) {\n  if(typeof color !== 'string') return;\ndosomething\n}\n<\/code><\/pre>\n<h3>2 \u4f7f\u7528json\u65b9\u5f0f\u4f20\u53c2<\/h3>\n<p>\u4f7f\u7528json\u7684\u65b9\u5f0f\u4f20\u503c\u5f88\u591a\u597d\u5904\uff0c\u5b83\u53ef\u4ee5\u7ed9\u53c2\u6570\u547d\u540d\uff0c\u53ef\u4ee5\u5ffd\u7565\u53c2\u6570\u7684\u5177\u4f53\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u7ed9\u53c2\u6570\u9ed8\u8ba4\u503c\u7b49\u7b49 \u6bd4\u5982\u4e0b\u9762\u8fd9\u79cd\u7cdf\u7cd5\u7684\u60c5\u51b5:<\/p>\n<pre><code class=\"language-js\">function fn(param1, param2...............paramN)\n<\/code><\/pre>\n<p>\u4f60\u5fc5\u987b\u5bf9\u5e94\u5730\u628a\u6bcf\u4e00\u4e2a\u53c2\u6570\u6309\u7167\u987a\u5e8f\u4f20\u5165\uff0c\u5426\u5219\u4f60\u7684\u65b9\u6cd5\u5c31\u4f1a\u504f\u79bb\u4f60\u9884\u671f\u53bb\u6267\u884c\uff0c\u6b63\u786e\u7684\u65b9\u6cd5\u662f\u4e0b\u9762\u7684\u505a\u6cd5\u3002<\/p>\n<pre><code class=\"language-js\">function fn(json) {\n\/\/\u4e3a\u5fc5\u987b\u7684\u53c2\u6570\u8bbe\u7f6e\u9ed8\u8ba4\u503c\n   var default = extend({\n    param: 'default',\n    param1: 'default'\n    ......\n   },json)\n}\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u51fd\u6570\u4ee3\u7801\uff0c\u5373\u4fbf\u4f60\u4e0d\u4f20\u4efb\u4f55\u53c2\u6570\u8fdb\u6765\uff0c\u4ed6\u4e5f\u4f1a\u9884\u671f\u8fd0\u884c\u3002\u56e0\u4e3a\u5728\u58f0\u660e\u7684\u65f6\u5019\uff0c\u4f60\u4f1a\u6839\u636e\u5177\u4f53\u7684\u4e1a\u52a1\u9884\u5148\u51b3\u5b9a\u53c2\u6570\u7684\u7f3a\u7701\u503c\u3002<\/p>\n<h2>\u56db \u53ef\u6269\u5c55\u6027<\/h2>\n<p>\u8f6f\u4ef6\u8bbe\u8ba1\u6700\u91cd\u8981\u7684\u539f\u5219\u4e4b\u4e00\uff1a\u6c38\u8fdc\u4e0d\u4fee\u6539\u63a5\u53e3\uff0c\u800c\u662f\u53bb\u6269\u5c55\u5b83\uff01\u53ef\u6269\u5c55\u6027\u540c\u65f6\u4f1a\u8981\u6c42\u63a5\u53e3\u7684\u804c\u8d23\u5355\u4e00\uff0c\u591a\u804c\u8d23\u7684\u63a5\u53e3\u5f88\u96be\u6269\u5c55\u3002 \u4e3e\u4e2a\u6817\u5b50\uff1a<\/p>\n<pre><code class=\"language-js\">\/\/\u9700\u8981\u540c\u65f6\u6539\u53d8\u67d0\u4e2a\u5143\u7d20\u7684\u5b57\u4f53\u548c\u80cc\u666f\n\/\/ Nightmare:\nfunction set(selector, color) {\n  document.querySelectroAll(selector).style.color = color;\n  document.querySelectroAll(selector).style.backgroundColor = color;\n}\n\n\/\/\u65e0\u6cd5\u6269\u5c55\u6539\u51fd\u6570\uff0c\u5982\u679c\u9700\u8981\u518d\u6b21\u6539\u53d8\u5b57\u4f53\u7684\u5927\u5c0f\u7684\u8bdd\uff0c\u53ea\u80fd\u4fee\u6539\u6b64\u51fd\u6570\uff0c\u5728\u51fd\u6570\u540e\u9762\u586b\u52a0\u6539\u53d8\u5b57\u4f53\u5927\u5c0f\u7684\u4ee3\u7801\n\n\/\/Dream\nfunction set(selector, color) {\n  var el = document.querySelectroAll(selector);\n  el.style.color = color;\n  el.style.backgroundColor = color;\n  return el;\n}\n\n\/\/\u9700\u8981\u8bbe\u7f6e\u5b57\u4f53\u3001\u80cc\u666f\u989c\u8272\u548c\u5927\u5c0f\nfunction setAgain (selector, color, px) {\n  var el = set(selector, color)\n  el.style.fontSize = px;\n  return el;\n}\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u53ea\u662f\u7b80\u5355\u7684\u6dfb\u52a0\u989c\u8272\uff0c\u4e1a\u52a1\u590d\u6742\u800c\u4ee3\u7801\u53c8\u4e0d\u662f\u4f60\u5199\u7684\u65f6\u5019\uff0c\u4f60\u5c31\u5fc5\u987b\u53bb\u9605\u8bfb\u4e4b\u524d\u7684\u4ee3\u7801\u518d\u4fee\u6539\u5b83\uff0c\u663e\u7136\u662f\u4e0d\u7b26\u5408\u5f00\u653e-\u5c01\u95ed\u539f\u5219\u7684\u3002\u4fee\u6539\u540e\u7684function\u662f\u8fd4\u56de\u4e86\u5143\u7d20\u5bf9\u8c61\uff0c\u4f7f\u5f97\u4e0b\u6b21\u9700\u8981\u6539\u53d8\u65f6\u518d\u6b21\u5f97\u5230\u8fd4\u56de\u503c\u505a\u5904\u7406\u3002<\/p>\n<h3>2 this\u7684\u8fd0\u7528<\/h3>\n<p>\u53ef\u6269\u5c55\u6027\u8fd8\u5305\u62ec\u5bf9this\u7684\u4ee5\u53cacall\u548capply\u65b9\u6cd5\u7684\u7075\u6d3b\u8fd0\u7528\uff1a<\/p>\n<pre><code class=\"language-js\">function sayBonjour() {\n  alert(this.a)\n}\n\nobj.a = 1;\nobj.say = sayBonjour;\nobj.say();\/\/1\n\/\/or\nsayBonjour.call||apply(obj);\/\/1\n<\/code><\/pre>\n<h2>\u4e94 \u5bf9\u9519\u8bef\u7684\u5904\u7406<\/h2>\n<h3>1 \u9884\u89c1\u9519\u8bef<\/h3>\n<p>\u53ef\u4ee5\u7528 \u7c7b\u578b\u68c0\u6d4b typeof \u6216\u8005try...catch\u3002 typeof \u4f1a\u5f3a\u5236\u68c0\u6d4b\u5bf9\u8c61\u4e0d\u629b\u51fa\u9519\u8bef\uff0c\u5bf9\u4e8e\u672a\u5b9a\u4e49\u7684\u53d8\u91cf\u5c24\u5176\u6709\u7528\u3002<\/p>\n<h3>2 \u629b\u51fa\u9519\u8bef<\/h3>\n<p>\u5927\u591a\u6570\u5f00\u53d1\u8005\u4e0d\u5e0c\u671b\u51fa\u9519\u4e86\u8fd8\u9700\u8981\u81ea\u5df1\u53bb\u627e\u5e26\u5bf9\u5e94\u5f97\u4ee3\u7801\uff0c\u6700\u597d\u65b9\u5f0f\u662f\u76f4\u63a5\u5728console\u4e2d\u8f93\u51fa\uff0c\u544a\u8bc9\u7528\u6237\u53d1\u751f\u4e86\u4ec0\u4e48\u4e8b\u60c5\u3002\u6211\u4eec\u53ef\u4ee5\u7528\u5230\u6d4f\u89c8\u5668\u4e3a\u6211\u4eec\u63d0\u4f9b\u7684api\u8f93\u51fa\u8fd9\u4e9b\u4fe1\u606f:console.log\/warn\/error\u3002\u4f60\u8fd8\u53ef\u4ee5\u4e3a\u81ea\u5df1\u7684\u7a0b\u5e8f\u7559\u4e9b\u540e\u8def: try...catch\u3002<\/p>\n<pre><code class=\"language-js\">function error (a) {\n  if(typeof a !== 'string') {\n    console.error('param a must be type of string')\n  }\n}\n\nfunction error() {\n  try {\n    \/\/ some code excucete here maybe throw wrong\n  }catch(ex) {\n    console.wran(ex);\n  }\n}\n<\/code><\/pre>\n<h2>\u516d \u53ef\u9884\u89c1\u6027<\/h2>\n<p>\u53ef\u9884\u89c1\u6027\u5473\u7a0b\u5e8f\u63a5\u53e3\u63d0\u4f9b\u5065\u58ee\u6027\uff0c\u4e3a\u4fdd\u8bc1\u4f60\u7684\u4ee3\u7801\u987a\u5229\u6267\u884c\uff0c\u5fc5\u987b\u4e3a\u5b83\u8003\u8651\u5230\u975e\u6b63\u5e38\u9884\u671f\u7684\u60c5\u51b5\u3002\u6211\u4eec\u770b\u4e0b\u4e0d\u53ef\u4ee5\u9884\u89c1\u7684\u4ee3\u7801\u548c\u53ef\u9884\u89c1\u7684\u4ee3\u7801\u7684\u533a\u522b\u7528\u4e4b\u524d\u7684setColor<\/p>\n<pre><code class=\"language-js\">\/\/nighware\nfunction set(selector, color) {\n  document.getElementById(selector).style.color = color;\n}\n\n\/\/dream\nzepto.init = function(selector, context) {\n  var dom\n  \/\/ If nothing given, return an empty Zepto collection\n  if (!selector) return zepto.Z()\n  \/\/ Optimize for string selectors\n  else if (typeof selector == 'string') {\n    selector = selector.trim()\n    \/\/ If it's a html fragment, create nodes from it\n    \/\/ Note: In both Chrome 21 and Firefox 15, DOM error 12\n    \/\/ is thrown if the fragment doesn't begin with &lt;\n    if (selector[0] == '&lt;' &amp;&amp; fragmentRE.test(selector))\n      dom = zepto.fragment(selector, RegExp.$1, context), selector = null\n    \/\/ If there's a context, create a collection on that context first, and select\n    \/\/ nodes from there\n    else if (context !== undefined) return $(context).find(selector)\n    \/\/ If it's a CSS selector, use it to select nodes.\n    else dom = zepto.qsa(document, selector)\n  }\n  \/\/ If a function is given, call it when the DOM is ready\n  else if (isFunction(selector)) return $(document).ready(selector)\n  \/\/ If a Zepto collection is given, just return it\n  else if (zepto.isZ(selector)) return selector\n  else {\n    \/\/ normalize array if an array of nodes is given\n    if (isArray(selector)) dom = compact(selector)\n    \/\/ Wrap DOM nodes.\n    else if (isObject(selector))\n      dom = [selector], selector = null\n    \/\/ If it's a html fragment, create nodes from it\n    else if (fragmentRE.test(selector))\n      dom = zepto.fragment(selector.trim(), RegExp.$1, context), selector = null\n    \/\/ If there's a context, create a collection on that context first, and select\n    \/\/ nodes from there\n    else if (context !== undefined) return $(context).find(selector)\n    \/\/ And last but no least, if it's a CSS selector, use it to select nodes.\n    else dom = zepto.qsa(document, selector)\n  }\n  \/\/ create a new Zepto collection from the nodes found\n  return zepto.Z(dom, selector)\n}\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u662fzepto\u7684\u6e90\u7801\uff0c\u53ef\u4ee5\u770b\u89c1\uff0c\u4f5c\u8005\u5728\u9884\u89c1\u4f20\u5165\u7684\u53c2\u6570\u65f6\u505a\u4e86\u5f88\u591a\u7684\u5904\u7406\u3002\u5176\u5b9e\u53ef\u9884\u89c1\u6027\u662f\u4e3a\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u82e5\u5e72\u7684\u5165\u53e3\uff0c\u65e0\u975e\u662f\u4e00\u4e9b\u903b\u8f91\u5224\u65ad\u800c\u5df2\u3002zepto\u5728\u8fd9\u91cc\u4f7f\u7528\u4e86\u5f88\u591a\u7684\u662f\u975e\u5224\u65ad\uff0c\u8fd9\u6837\u505a\u7684\u597d\u5904\u5f53\u7136\u662f\u4ee3\u7801\u6bd4\u4e4b\u524d\u66f4\u5065\u58ee\uff0c\u4f46\u540c\u65f6\u5bfc\u81f4\u4e86\u4ee3\u7801\u7684\u5197\u957f\uff0c\u4e0d\u9002\u5408\u9605\u8bfb\u3002\u603b\u4e4b\uff0c\u53ef\u9884\u89c1\u6027\u771f\u6b63\u9700\u8981\u4f60\u505a\u7684\u4e8b\u591a\u5199\u4e00\u4e9b\u5bf9\u4f4d\u7f6e\u5b9e\u7269\u7684\u53c2\u6570\u3002\u628a\u5916\u90e8\u7684\u68c0\u6d4b\u6539\u4e3a\u5185\u90e8\u68c0\u6d4b\u3002\u662f\u7684\u4f7f\u7528\u7684\u4eba\u7528\u8d77\u6765\u8212\u5fc3\u653e\u5fc3\u5f00\u5fc3\u3002\u5450\uff01\u505a\u4eba\u561b\u6700\u91cd\u8981\u7684\u5c31\u662f\u6d77\u68ee\u5566\u3002<\/p>\n<h2>\u4e03 \u6ce8\u91ca\u548c\u6587\u6863\u7684\u53ef\u8bfb\u6027<\/h2>\n<p>\u4e00\u4e2a\u6700\u597d\u7684\u63a5\u53e3\u662f\u4e0d\u9700\u8981\u6587\u6863\u6211\u4eec\u4e5f\u4f1a\u4f7f\u7528\u5b83\uff0c\u4f46\u662f\u5f80\u5f80\u63a5\u53e3\u91cf\u4e00\u591a\u548c\u4e1a\u52a1\u589e\u52a0\uff0c\u63a5\u53e3\u4f7f\u7528\u8d77\u6765\u4e5f\u4f1a\u6709\u4e9b\u8d39\u52b2\u3002\u6240\u4ee5\u63a5\u53e3\u6587\u6863\u548c\u6ce8\u91ca\u662f\u9700\u8981\u8ba4\u771f\u4e66\u5199\u7684\u3002\u6ce8\u91ca\u9075\u5faa\u7b80\u5355\u627c\u8981\u5730\u539f\u5219\uff0c\u7ed9\u591a\u5e74\u540e\u7684\u81ea\u5df1\u4e5f\u7ed9\u540e\u6765\u8005\u770b\uff1a<\/p>\n<pre><code class=\"language-js\">\/\/\u6ce8\u91ca\u63a5\u53e3\uff0c\u4e3a\u4e86\u6f14\u793aPPT\u7528\nfunction commentary() {\n  \/\/\u5982\u679c\u4f60\u5b9a\u4e49\u4e00\u4e2a\u6ca1\u6709\u5b57\u9762\u610f\u4e49\u7684\u53d8\u91cf\u65f6\uff0c\u6700\u597d\u4e3a\u5b83\u5199\u4e0a\u6ce8\u91ca\uff1aa\uff1a\u6ca1\u7528\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u5220\u9664\n  var a;\n\n  \/\/\u5728\u5173\u952e\u548c\u6709\u6b67\u4e49\u7684\u5730\u65b9\u5199\u4e0a\u6ce8\u91ca\uff0c\u72b9\u5982\u753b\u9f99\u70b9\u775b\uff1a\u8def\u7531\u5230hash\u754c\u9762\u540e\u5c06\u6240\u6709\u7684\u6570\u636e\u6e05\u7a7a\u7ed3\u675f\u51fd\u6570\n  return go.Navigate('hash', function(){\n    data.clear();\n  });\n}\n<\/code><\/pre>\n<h2>\u6700\u540e<\/h2>\n<p>\u63a8\u8350markdown\u8bed\u6cd5\u4e66\u5199API\u6587\u6863\uff0cgithub\u5fa1\u7528\u6587\u6863\u7f16\u5199\u8bed\u6cd5\u3002\u7b80\u5355\u3001\u5feb\u901f\uff0c\u4ee3\u7801\u9ad8\u4eae\u3001\u8bdd\u4e0d\u591a\u8bf4\u4e0a\u56fe<\/p>\n<p>\u5364\u716e\u5728\u6b64\u4e5f\u63a8\u8350\u51e0\u4e2a\u5728\u7ebf\u7f16\u8f91\u7684\u7f51\u7ad9\u3002\u8bf8\u541b\u53ef\u81ea\u884c\u524d\u5f80\u7ec3\u4e60\u4f7f\u7528\u3002<\/p>\n<p>https:\/\/www.zybuluo.com\/mdeditor<\/p>\n<p>http:\/\/mahua.jser.me\/<\/p>\n<p>\u53c2\u8003\u535a\u6587\uff1a<a href=\"http:\/\/top.css88.com\/archives\/814\">JavaScript API \u8bbe\u8ba1\u539f\u5219<\/a><\/p>\n<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"http:\/\/www.cnblogs.com\/constantince\/p\/5580003.html\">javascript\u7684api\u8bbe\u8ba1\u539f\u5219<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u672c\u7bc7\u535a\u6587\u6765\u81ea\u4e00\u6b21\u516c\u53f8\u5185\u90e8\u7684\u524d\u7aef\u5206\u4eab\uff0c\u4ece\u591a\u4e2a\u65b9\u9762\u8ba8\u8bba\u4e86\u5728\u8bbe\u8ba1\u63a5\u53e3\u65f6\u9075\u5faa\u7684\u539f\u5219\uff0c\u603b\u5171\u5305\u542b\u4e86\u4e03\u4e2a\u5927\u5757\u3002\u7cfb\u5364\u716e\u81ea\u5df1\u603b\u7ed3\u7684\u4e00&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,5],"tags":[],"class_list":["post-2006","post","type-post","status-publish","format-standard","hentry","category-fe","category-javascript"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/posts\/2006","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2006"}],"version-history":[{"count":2,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/posts\/2006\/revisions"}],"predecessor-version":[{"id":2014,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/posts\/2006\/revisions\/2014"}],"wp:attachment":[{"href":"https:\/\/feizhaojun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}