-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs__myblog__js__05.md.js
More file actions
1 lines (1 loc) · 11.1 KB
/
docs__myblog__js__05.md.js
File metadata and controls
1 lines (1 loc) · 11.1 KB
1
(window["webpackJsonp"]=window["webpackJsonp"]||[]).push([[75],{ZmrX:function(e,l,n){e.exports=n.p+"static/compare.c001187b.png"},fFSZ:function(e,l,n){"use strict";n.r(l);var a=n("q1tI"),t=n.n(a),c=n("dEAq"),r=n("H1Ra"),o=t.a.memo((e=>{e.demos;return t.a.createElement(t.a.Fragment,null,t.a.createElement("div",{className:"markdown"},t.a.createElement("h2",{id:"js-\u7c7b\u578b\u6bd4\u8f83"},t.a.createElement(c["AnchorLink"],{to:"#js-\u7c7b\u578b\u6bd4\u8f83","aria-hidden":"true",tabIndex:-1},t.a.createElement("span",{className:"icon icon-link"})),"JS \u7c7b\u578b\u6bd4\u8f83"),t.a.createElement("p",null,"\u5bf9\u4e8e ",t.a.createElement("code",null,"==")," \u6765\u8bf4\uff0c\u5982\u679c\u5bf9\u6bd4\u53cc\u65b9\u7684\u7c7b\u578b\u4e0d\u4e00\u6837\u7684\u8bdd\uff0c\u5c31\u4f1a\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u8fd9\u4e5f\u5c31\u7528\u5230\u4e86\u6211\u4eec\u4e0a\u4e00\u7ae0\u8282\u8bb2\u7684\u5185\u5bb9"),t.a.createElement("ol",null,t.a.createElement("li",null,t.a.createElement("p",null,"\u9996\u5148\u4f1a\u5224\u65ad\u4e24\u8005\u7c7b\u578b\u662f\u5426\u76f8\u540c\u3002\u76f8\u540c\u7684\u8bdd\u5c31\u662f\u6bd4\u5927\u5c0f\u4e86")),t.a.createElement("li",null,t.a.createElement("p",null,"\u7c7b\u578b\u4e0d\u76f8\u540c\u7684\u8bdd\uff0c\u90a3\u4e48\u5c31\u4f1a\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362")),t.a.createElement("li",null,t.a.createElement("p",null,"\u4f1a\u5148\u5224\u65ad\u662f\u5426\u5728\u5bf9\u6bd4 ",t.a.createElement("code",null,"null")," \u548c ",t.a.createElement("code",null,"undefined"),"\uff0c\u662f\u7684\u8bdd\u5c31\u4f1a\u8fd4\u56de ",t.a.createElement("code",null,"true"))),t.a.createElement("li",null,t.a.createElement("p",null,"\u5224\u65ad\u4e24\u8005\u7c7b\u578b\u662f\u5426\u4e3a ",t.a.createElement("code",null,"string")," \u548c ",t.a.createElement("code",null,"number"),"\uff0c\u662f\u7684\u8bdd\u5c31\u4f1a\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a ",t.a.createElement("code",null,"number")))),t.a.createElement(r["a"],{code:"1=='1'\n \u2193\n1==1",lang:"js"}),t.a.createElement("ol",{start:5},t.a.createElement("li",null,"\u5224\u65ad\u5176\u4e2d\u4e00\u65b9\u662f\u5426\u4e3a ",t.a.createElement("code",null,"boolean"),"\uff0c\u662f\u7684\u8bdd\u5c31\u4f1a\u628a ",t.a.createElement("code",null,"boolean")," \u8f6c\u4e3a ",t.a.createElement("code",null,"number")," \u518d\u8fdb\u884c\u5224\u65ad")),t.a.createElement(r["a"],{code:"'1' == true\n \u2193\n'1' == 1\n \u2193\n1 == 1",lang:"js"}),t.a.createElement("ol",{start:6},t.a.createElement("li",null,"\u5224\u65ad\u5176\u4e2d\u4e00\u65b9\u662f\u5426\u4e3a ",t.a.createElement("code",null,"object")," \u4e14\u53e6\u4e00\u65b9\u4e3a ",t.a.createElement("code",null,"string"),"\u3001",t.a.createElement("code",null,"number")," \u6216\u8005 ",t.a.createElement("code",null,"symbol"),"\uff0c\u662f\u7684\u8bdd\u5c31\u4f1a\u628a ",t.a.createElement("code",null,"object")," \u8f6c\u4e3a\u539f\u59cb\u7c7b\u578b\u518d\u8fdb\u884c\u5224\u65ad")),t.a.createElement(r["a"],{code:"'1' == { name: 'yck' }\n \u2193\n'1' == '[object Object]'",lang:"js"}),t.a.createElement("h2",{id:"\u8bf7\u5199\u51fa\u7684\u7ed3\u679c"},t.a.createElement(c["AnchorLink"],{to:"#\u8bf7\u5199\u51fa\u7684\u7ed3\u679c","aria-hidden":"true",tabIndex:-1},t.a.createElement("span",{className:"icon icon-link"})),"\u8bf7\u5199\u51fa",t.a.createElement("code",null,"[]==![]"),"\u7684\u7ed3\u679c\uff1f"),t.a.createElement("p",null,"\u7b54\u6848\u662f",t.a.createElement("code",null,"true"),",\u4e3a\u4ec0\u4e48\u5462\uff1f"),t.a.createElement("ol",null,t.a.createElement("li",null,"\u56e0\u4e3a",t.a.createElement("code",null,"!"),"\u8fd0\u7b97\u7684\u4f18\u5148\u7ea7\u5927\u4e8e",t.a.createElement("code",null,"=="),",\u6240\u4ee5\u5148\u6267\u884c",t.a.createElement("code",null,"![]"),",")),t.a.createElement("p",null,t.a.createElement("code",null,"\uff01"),"\u53ef\u5c06\u53d8\u91cf\u8f6c\u6362\u6210",t.a.createElement("code",null,"boolean"),"\u7c7b\u578b\uff0c",t.a.createElement("code",null,"null"),"\u3001",t.a.createElement("code",null,"undefined"),"\u3001",t.a.createElement("code",null,"NaN"),"\u4ee5\u53ca\u7a7a\u5b57\u7b26\u4e32",t.a.createElement("code",null,"('')"),"\u53d6\u53cd\u90fd\u4e3a",t.a.createElement("code",null,"true"),"\uff0c\u5176\u4f59\u90fd\u4e3a",t.a.createElement("code",null,"false")),t.a.createElement("p",null,"\u6240\u4ee5 ",t.a.createElement("code",null,"![]")," \u8fd0\u7b97\u540e\u7684\u7ed3\u679c\u5c31\u662f ",t.a.createElement("code",null,"false"),"\uff0c\u4e5f\u5c31\u662f ",t.a.createElement("code",null,"[] == ![]")," \u76f8\u5f53\u4e8e ",t.a.createElement("code",null,"[] == false")),t.a.createElement("ol",{start:2},t.a.createElement("li",null,t.a.createElement("p",null,"\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\u2014",t.a.createElement("code",null,"false"),"\u8f6c\u6362\u4e3a",t.a.createElement("code",null,"0"),"\uff0c\u800c",t.a.createElement("code",null,"true"),"\u8f6c\u6362\u4e3a",t.a.createElement("code",null,"1"),"\uff09\uff0c\u5219\u9700\u8981\u628a ",t.a.createElement("code",null,"false")," \u8f6c\u6210 ",t.a.createElement("code",null,"0"),"\uff0c\u4e5f\u5c31\u662f ",t.a.createElement("code",null,"[] == ![]")," \u76f8\u5f53\u4e8e ",t.a.createElement("code",null,"[] == false")," \u76f8\u5f53\u4e8e ",t.a.createElement("code",null,"[] == 0"))),t.a.createElement("li",null,t.a.createElement("p",null,"\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\u7684",t.a.createElement("code",null,"valueOf()"),"\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\u6709",t.a.createElement("code",null,"valueOf()"),"\u65b9\u6cd5\uff0c\u5219\u8c03\u7528 ",t.a.createElement("code",null,"toString()\uff09"),"\uff0c\u4e5f\u5c31\u662f ",t.a.createElement("code",null,"[] == 0")," \u76f8\u5f53\u4e8e ",t.a.createElement("code",null,"'' == 0"))),t.a.createElement("li",null,t.a.createElement("p",null,"\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"))),t.a.createElement("p",null,t.a.createElement("code",null,"Number('')")," -> \u8fd4\u56de\u7684\u662f 0,\u76f8\u5f53\u4e8e",t.a.createElement("code",null,"0 == 0")," \u81ea\u7136\u5c31\u8fd4\u56de ",t.a.createElement("code",null,"true"),"\u4e86"),t.a.createElement("p",null,t.a.createElement("strong",null,"\u603b\u7ed3")),t.a.createElement("p",null,t.a.createElement("code",null,"[] == ![]")," -> ",t.a.createElement("code",null,"[] == false")," -> ",t.a.createElement("code",null,"[] == 0")," -> ",t.a.createElement("code",null,"'' == 0")," -> ",t.a.createElement("code",null,"0 == 0")," -> true"),t.a.createElement("p",null,"\u90a3\u4e48\u5bf9\u4e8e ",t.a.createElement("code",null,"{","}"," == !","{","}")," \u4e5f\u662f\u540c\u7406\u7684"),t.a.createElement("p",null,"\u5173\u952e\u5728\u4e8e ",t.a.createElement("code",null,"{","}",".toString() -> NaN"),"(\u8fd4\u56de\u7684\u662f NaN)"),t.a.createElement("p",null,"\u6839\u636e\u4e0a\u9762\u7684\u89c4\u5219\uff08\u5982\u679c\u6709\u4e00\u4e2a\u64cd\u4f5c\u6570\u662f NaN\uff0c\u5219\u76f8\u7b49\u64cd\u4f5c\u7b26\u8fd4\u56de false\uff09"),t.a.createElement("p",null,"\u603b\u7ed3\u4e00\u4e0b\uff1a"),t.a.createElement("p",null,t.a.createElement("code",null,"{","}"," == ! ","{","}")," -> ",t.a.createElement("code",null,"{","}"," == false")," -> ",t.a.createElement("code",null,"{","}"," == 0")," -> ",t.a.createElement("code",null,"NaN == 0")," -> false"),t.a.createElement("p",null,t.a.createElement("strong",null,"if \u7684\u5224\u65ad\u539f\u7406")),t.a.createElement("p",null,t.a.createElement("code",null,"if"),"\u7684\u5224\u65ad\u539f\u7406\u548c",t.a.createElement("code",null,"Boolean()"),"\u8fd9\u4e2a\u51fd\u6570\u6709\u5173\u7684\uff0c\u6240\u4ee5",t.a.createElement("code",null,"if(***)")," \u5c31\u76f8\u5f53\u4e8e",t.a.createElement("code",null,"Boolean(***)")),t.a.createElement("p",null,t.a.createElement("strong",null,"Boolean \u7684\u5224\u65ad\u89c4\u5219\uff1a")),t.a.createElement("ul",null,t.a.createElement("li",null,"\u6570\u5b57\u7c7b\u578b\uff1a",t.a.createElement("code",null,"NAN"),"\u3001",t.a.createElement("code",null,"0"),"\u7684\u7ed3\u679c\u4e3a",t.a.createElement("code",null,"false,"),"\u5176\u4ed6\u60c5\u51b5\u4e3a",t.a.createElement("code",null,"true")),t.a.createElement("li",null,"\u5b57\u7b26\u4e32\u7c7b\u578b\uff1a\u7a7a\u5b57\u7b26\u4e32\u662f",t.a.createElement("code",null,"false"),",\u5176\u4ed6\u60c5\u51b5\u4e3a",t.a.createElement("code",null,"true")),t.a.createElement("li",null,"Boolean \u7c7b\u578b\uff1a",t.a.createElement("code",null,"false"),"\u4e3a",t.a.createElement("code",null,"false"),",",t.a.createElement("code",null,"true"),"\u4e3a",t.a.createElement("code",null,"true")),t.a.createElement("li",null,"\u5bf9\u8c61\u7c7b\u578b",t.a.createElement("code",null,"undefined"),"\u3001",t.a.createElement("code",null,"null"),"\u4e3a",t.a.createElement("code",null,"false"),",\u5176\u4ed6\u4e3a",t.a.createElement("code",null,"true"))),t.a.createElement("img",{src:n("ZmrX")}),t.a.createElement(r["a"],{code:"//1\u3001\u591a\u4e86\u62ec\u53f7\uff0c\u6240\u4ee5\u662f\u8868\u8fbe\u5f0f\nconsole.log({}+[]) //[object Object]\n\n//2\u3001\u524d\u9762\u662f\u4ee3\u7801\u5757 \u6240\u4ee5\u76f4\u63a5\u6267\u884c+[]\n{}+[] // 0\n\n//3\u3001\u56e0\u4e3a[]\u4f1a\u88ab\u5f3a\u5236\u8f6c\u6362\u4e3a\"\", \u7136\u540e+\u8fd0\u7b97\u7b26 \u94fe\u63a5\u4e00\u4e2a{ }, { }\u5f3a\u5236\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5c31\u662f\"[object Object]\"\n[]+{} //[object Object]\n\n//4\u3001\u4e0d\u540c\u6d4f\u89c8\u5668\u4e0d\u540c\uff0c\u9ed8\u8ba4\u6309\u8c37\u6b4c,\u540c\u7b49\u7c7b\u578b\u6c42\u503c\n{}+{} //[object Object][object Object]\n\n//5\u3001[]=='0' -> 0=='0' -> 0==false\nconsole.log([]==false) //true\n\n//6\u3001\u6ca1\u6709\u53ef\u6bd4\u6027 {}\u80af\u5b9a\u662f\u6709\u503c\u7684\nconsole.log({}==false) //false\n\n//7\u3001\u6570\u7ec4\u662f\u771f\u5b9e\u5b58\u5728\u7684\uff0c\u6240\u4ee5\u8fdb\u5165if\nif([]){\n console.log([]==false) //true\n}\n\n//8\u3001+'a'\u5f3a\u5236\u8f6cnumber\n('b'+'a'+ +'a'+'a').toLocaleLowerCase() //banana\n\n//9\u3001\u76f4\u63a5\u62ff\u539f\u59cb\u503c\u6bd4\n0=='0' //true\n\n//10\u3001Boolean('0')\u662f\u6709\u771f\u5b9e\u7684\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5\u662ftrue\nBoolean(0)==Boolean('0') //false\n\n//11\u3001NaN\u4e0e\u4efb\u4f55\u503c\u90fd\u4e0d\u76f8\u7b49\nNaN==0 //false\nNaN<=0 //false\n\n//12\u3001Number(0,10)==0 null\xa0\u5c1d\u8bd5\u8f6c\u4e3anumber\nnull<=0 //true",lang:"js"})))}));l["default"]=e=>{var l=t.a.useContext(c["context"]),n=l.demos;return t.a.useEffect((()=>{var l;null!==e&&void 0!==e&&null!==(l=e.location)&&void 0!==l&&l.hash&&c["AnchorLink"].scrollToAnchor(decodeURIComponent(e.location.hash.slice(1)))}),[]),t.a.createElement(o,{demos:n})}}}]);