Marijn Haverbeke - Выразительный Javascript, 2-е Издание - 2015

  • Published on
    04-Dec-2015

  • View
    284

  • Download
    21

Embed Size (px)

DESCRIPTION

PDF JavaScript

Transcript

<ul><li><p>1. 2. 3. ,4. 5. 6. :7. 8. 9. :10. 11. 12. 13. :14. JavaScript15. DocumentObjectModel16. 17. :-18. 19. HTTP20. 21. :Paint22. Node.js23. :-</p><p>Javascript</p><p>2</p></li><li><p>2-</p><p>:MarijnHaverbeke:</p><p>CreativeCommonsAttribution-Noncommercial.MIT.</p><p>Gitbook:</p><p>Javascript</p><p>Javascript</p><p>3</p></li><li><p>,,.,,.,.</p><p>,,,.,,,..</p><p>,.,,,:.</p><p>,,.,.</p><p>Javascript</p><p>4</p></li><li><p>20,,(-).,.,JavaScript,-,.</p><p>,,.</p><p>Javascript</p><p>5</p></li><li><p>,,,.,.</p><p>JavaScript.,..,,,.,.</p><p>,.,.,.</p><p>.,..,,.,-,,.</p><p>,..</p><p>,</p><p>.,,,-,,</p><p>Javascript</p><p>6</p></li><li><p>.,.-.,,,.</p><p>,,..,.,.</p><p>..,,.</p><p>,-,,.:...,.</p><p>,,.(),.,,.</p><p>-,!,,.,,,..</p><p>Javascript</p><p>7</p></li><li><p>,,,,.</p><p>,,.:</p><p>001100010000000000000000001100010000000100000001001100110000000100000010010100010000101100000010001000100000001000001000010000110000000100000000010000010000000100000001000100000000001000000000011000100000000000000000</p><p>,110,(1+2++10=55)..,.,,...</p><p>,().-.</p><p>.:</p><p>1. 002. 113. 124. 1125. 20,9.</p><p>Javascript</p><p>8</p></li><li><p>6. 107. 118. 3.9. 0</p><p>,,..</p><p>total0count1[loop]comparecount11comparecompare,[end]counttotal1count[loop][end]total</p><p>,.?.total,count,.,compare,,.,count11,.,,,.compare,count11.countcount1,,11.</p><p>JavaScript:</p><p>vartotal=0,count=1;while(count</p></li><li><p>count+=1;}console.log(total);//55</p><p>..while.,,(count</p></li><li><p>-NetscapeNavigator..---,,..</p><p>,JavaScriptJava..JavaScript,Java.-,..</p><p>,Netscape,,,,,.ECMAScriptECMA.ECMAScriptJavaScript.</p><p>JavaScript..JavaScript,,,,.-,,,:JavaScript..,,.</p><p>.,.,,.,,JavaScript.</p><p>JavaScript.ECMAScript3,,20002010.4-,</p><p>Javascript</p><p>11</p></li><li><p>.,4-2008.5-2009.5-,.</p><p>JavaScript.MongoDBandCouchDB.,Node.js,.</p><p>,....,..,,.</p><p>,,,,.</p><p>Node.js..12,HTML-.jsbin.com,..</p><p>,</p><p>Javascript</p><p>12</p></li><li><p>.,.,..</p><p>-,</p><p>.,,.,.</p><p>,.,,CD.,.</p><p>,13.,10,..,13,:</p><p>000011011286432168421</p><p>00001101,8+4+1,13.</p><p>.30.().</p><p>,</p><p>.</p><p>Javascript</p><p>13,</p></li><li><p>,,.JavaScript.,.,.:,,,,.</p><p>...-,.-,,.,.,,.</p><p>JavaScript,.</p><p>,.JavaScript</p><p>13</p><p>,</p><p>Javascript</p><p>14,</p></li><li><p>,13.</p><p>JavaScript(64).,64,.N,,10N.,64264..</p><p>,816.,,.,64,,.</p><p>,2^64JavaScript.,.,..10^15,.</p><p>.</p><p>9.81</p><p>e(exponent),:</p><p>2.998e8</p><p>2.99810^8=299800000.</p><p>(integer),,10^15,..,()</p><p>Javascript</p><p>15,</p></li><li><p>,,64.,..</p><p>,..JavaScript:</p><p>100+4*11</p><p>+*.,..</p><p>410011?,.,:</p><p>(100+4)*11</p><p>-,-/</p><p>,.*/,,+-,.:</p><p>1-2+1</p><p>(1-2)+1</p><p>.</p><p>Javascript</p><p>16,</p></li><li><p>.</p><p>,.%.X%YXY.314%10014,144%120.,.,.</p><p>JavaScript,,.</p><p>Infinity-Infinity,.Infinity-1=Infinity,.,.</p><p>:NaN.notanumber(),.0/0,InfinityInfinity,,.</p><p>..:</p><p>","','</p><p>,...,..</p><p>.</p><p>Javascript</p><p>17,</p></li><li><p>,,,(escapecharacter).\.\n,\t.</p><p>\n:</p><p>,:\.\n:</p><p>"\"\\n\""</p><p>,.+,.:</p><p>""+""+""+""</p><p>,4.</p><p>.typeof,,.</p><p>console.log(typeof4.5)//number</p><p>console.log(typeof"x")//string</p><p>Javascript</p><p>18,</p></li><li><p>console.log,.,.</p><p>,typeof.,,,.(),.</p><p>console.log(-(10-2))//-8</p><p>,,.JavaScriptBoolean,truefalse().</p><p>:</p><p>console.log(3&gt;2)//trueconsole.log(350</p><p>.:</p><p>console.log(true?1:2);//1console.log(false?1:2);//2</p><p>,,.true,.false,.</p><p>,nullundefined,..</p><p>,,undefined,-.undefinednull,.</p><p>Javascript</p><p>21,</p></li><li><p>,JavaScript,.:</p><p>console.log(8*null)//0console.log("5"-1)//4console.log("5"+1)//51console.log(""*2)//NaNconsole.log(false==0)//true</p><p>,JavaScript,,.(coercion).null0,55().+(),-11().</p><p>-(,undefined),NaN.NaNNaN.,,.</p><p>==,,true,(NaN).,JavaScript..nullundefined,true,nullundefined.</p><p>console.log(null==undefined);//trueconsole.log(null==0);//false</p><p>Javascript</p><p>22,</p></li><li><p>.,nullundefined,null==!=.</p><p>,?,0,NaNfalse,true.0==false==false.,,:===!==.,,.===falsefalse.</p><p>,.,,.</p><p>&amp;&amp;||.,,,,.</p><p>,||,true.</p><p>console.log(null||"user")//userconsole.log("Karl"||"user")//Karl</p><p>||.,,</p><p>Javascript</p><p>23,</p></li><li><p>.</p><p>&amp;&amp;,.false,,.</p><p>.true||X,X.-.trueX.false&amp;&amp;XX..</p><p>.,,.</p><p>JavaScript:,,.</p><p>,(true,null)(13,)..(+,-,*,/,and%),(+),(==,!=,===,!==,,=),(&amp;&amp;,||),,(-,!,typeof).</p><p>JavaScript,.,.</p><p>Javascript</p><p>24,</p></li><li><p>-,,JavaScript,().!</p><p>_why,Why's(Poignant)GuidetoRuby</p><p>,.JavaScript-.</p><p>,.,.</p><p>,,.,(,22).,,,,.</p><p>.,..</p><p>,..</p><p>.:</p><p>Javascript</p><p>25</p></li><li><p>1;!false;</p><p>,.,,.-.-(),,..1true,...</p><p>JavaScript.,.,,.,,.</p><p>??,,,.,JavaScript.</p><p>varcaught=5*5;</p><p>.(keyword)var,.,,=.</p><p>Javascript</p><p>26</p></li><li><p>caught,55.</p><p>.,.:</p><p>varten=10;console.log(ten*ten);//100</p><p>,(var)..,.,$_.</p><p>.=,.</p><p>varmood="";console.log(mood);//mood="";console.log(mood);//</p><p>,...,.-,,,.</p><p>Javascript</p><p>27</p></li><li><p>.,,.,,.</p><p>varvasyaDebt=140;vasyaDebt=vasyaDebt-35;console.log(vasyaDebt);//105</p><p>,,.,undefined.</p><p>var..</p><p>varone=1,two=2;console.log(one+two);//3</p><p>,var.</p><p>Javascript</p><p>28</p></li><li><p>.,JavaScript.,..</p><p>breakcasecatchcontinuedebuggerdefaultdeletedoelsefalsefinallyforfunctionifimplementsininstanceofinterfaceletnewnullpackageprivateprotectedpublicreturnstaticswitchthrowtruetrytypeofvarvoidwhilewithyieldthis</p><p>,,,,.</p><p>,,.,.,,,.,-,.</p><p>function().,.,alert,.:</p><p>alert("!");</p><p>Javascript</p><p>29</p></li><li><p>.,,..,,.,alert.,,.alert,.</p><p>alert,.console.log.JavaScript(Node.js)console.log,-.JavaScript.F12,Command-Option-I.,webconsoledevelopertools.</p><p>:</p><p>varx=30;console.log("thevalueofxis",x);//thevalueofxis30</p><p>,,console.log.,console.log.,logconsole.4.</p><p>alert</p><p>console.log</p><p>Javascript</p><p>30</p></li><li><p>.,.,,.,Math.max:</p><p>console.log(Math.max(2,4));//4</p><p>,,.,,.,Math.min(Math.max):</p><p>console.log(Math.min(2,4)+100);//102</p><p>,.</p><p>,alert,.OK/Cancelconfirm.true,OK,false,Cancel.</p><p>confirm(",?");</p><p>promptconfirm</p><p>Javascript</p><p>31</p></li><li><p>prompt,.,,.,.</p><p>prompt(",.","...");</p><p>,.</p><p>,..,,,.</p><p>vartheNumber=Number(prompt("",""));alert(""+theNumber*theNumber);</p><p>Number.,prompt.StringBoolean,.</p><p>:</p><p>.,</p><p>Javascript</p><p>32</p></li><li><p>,:</p><p>if.,,.,,.</p><p>vartheNumber=prompt("","");if(!isNaN(theNumber))alert(""+theNumber*theNumber);</p><p>,,.</p><p>if,.if,.</p><p>isNaNJavaScript,true,NaN().NumberNaN,,.,:,theNumber-.</p><p>,,,..elseif.</p><p>vartheNumber=Number(prompt("",""));if(!isNaN(theNumber))alert(""+theNumber*theNumber);elsealert("-?");</p><p>,</p><p>Javascript</p><p>33</p></li><li><p>if/else.</p><p>varnum=Number(prompt("","0"));</p><p>if(num(x,5)),print(""),print(""))</p><p>,,JS,,.,do,.</p><p>,,,type,,.</p><p>value.value,.word().name,.,apply.operator,,args.</p><p>&gt;(x,5):</p><p>{</p><p>Javascript</p><p>210:</p></li><li><p>type:"apply",operator:{type:"word",name:"&gt;"},args:[{type:"word",name:"x"},{type:"value",value:5}]}</p><p>.,,.,,,,.</p><p>,9,:.,.</p><p>.,.-.,,.</p><p>parseExpression,,,,.(,),,.,,,,.</p><p>:</p><p>functionparseExpression(program){program=skipSpace(program);varmatch,expr;</p><p>Javascript</p><p>211:</p></li><li><p>if(match=/^"([^"]*)"/.exec(program))expr={type:"value",value:match[1]};elseif(match=/^\d+\b/.exec(program))expr={type:"value",value:Number(match[0])};elseif(match=/^[^\s(),"]+/.exec(program))expr={type:"word",name:match[0]};elsethrownewSyntaxError(":"+program);</p><p>returnparseApply(expr,program.slice(match[0].length));}</p><p>functionskipSpace(string){varfirst=string.search(/\S/);if(first==-1)return"";returnstring.slice(first);}</p><p>Egg,.skipSpace.</p><p>,parseExpression(),:,..,,.SyntaxError,JavaScript.</p><p>,,,parseApply,,.,.</p><p>functionparseApply(expr,program){program=skipSpace(program);if(program[0]!="(")return{expr:expr,rest:program};</p><p>program=skipSpace(program.slice(1));expr={type:"apply",operator:expr,args:[]};while(program[0]!=")"){vararg=parseExpression(program);</p><p>Javascript</p><p>212:</p></li><li><p>expr.args.push(arg.expr);program=skipSpace(arg.rest);if(program[0]==",")program=skipSpace(program.slice(1));elseif(program[0]!=")")thrownewSyntaxError("','or')'");}returnparseApply(expr,program.slice(1));}</p><p>,,parseApply.</p><p>,.parseExpression,.,parseApplyparseExpression.</p><p>(multiplier(2)(1)),parseApply,,,,.</p><p>,Egg.parse,,(Egg),.</p><p>functionparse(program){varresult=parseExpression(program);if(skipSpace(result.rest).length&gt;0)thrownewSyntaxError("");returnresult.expr;}</p><p>console.log(parse("+(a,10)"));//{type:"apply",//operator:{type:"word",name:"+"},//args:[{type:"word",name:"a"},//{type:"value",value:10}]}</p><p>!,,,</p><p>Javascript</p><p>213:</p></li><li><p>.</p><p>?!.,,,,.</p><p>functionevaluate(expr,env){switch(expr.type){case"value":returnexpr.value;</p><p>case"word":if(expr.nameinenv)returnenv[expr.name];elsethrownewReferenceError(":"+expr.name);case"apply":if(expr.operator.type=="word"&amp;&amp;expr.operator.nameinspecialForms)returnspecialForms[expr.operator.name](expr.args,env);varop=evaluate(expr.operator,env);if(typeofop!="function")thrownewTypeError(".");returnop.apply(null,expr.args.map(function(arg){returnevaluate(arg,env);}));}}</p><p>varspecialForms=Object.create(null);</p><p>..,100100.,,.</p><p>.if,,</p><p>Javascript</p><p>214:</p></li><li><p>,.,,,.</p><p>EggJavaScript.,fun.</p><p>..,.</p><p>,Egg..,.</p><p>specialFormsEgg.,...</p><p>specialForms["if"]=function(args,env){if(args.length!=3)thrownewSyntaxError("if");</p><p>if(evaluate(args[0],env)!==false)returnevaluate(args[1],env);elsereturnevaluate(args[2],env);};</p><p>ifEgg.,false,..if?:.,,,,.</p><p>Javascript</p><p>215:</p></li><li><p>EggJavaScript,if.false.</p><p>if,,if,.</p><p>while.</p><p>specialForms["while"]=function(args,env){if(args.length!=2)thrownewSyntaxError("while");</p><p>while(evaluate(args[0],env)!==false)evaluate(args[1],env);</p><p>//undefinedEgg,//falsereturnfalse;};</p><p>do,.,.</p><p>specialForms["do"]=function(args,env){varvalue=false;args.forEach(function(arg){value=evaluate(arg,env);});returnvalue;};</p><p>,define.word,,,.define,,,.(=JavaScript).</p><p>specialForms["define"]=function(args,env){if(args.length!=2||args[0].type!="word")</p><p>Javascript</p><p>216:</p></li><li><p>thrownewSyntaxError("Baduseofdefine");varvalue=evaluate(args[1],env);env[args[0].name]=value;returnvalue;};</p><p>,,,.,.</p><p>if.,.truefalse.</p><p>vartopEnv=Object.create(null);</p><p>topEnv["true"]=true;topEnv["false"]=false;</p><p>,.</p><p>varprog=parse("if(true,false,true)");console.log(evaluate(prog,topEnv));//false</p><p>.newFunction-,.</p><p>["+","-","*","/","==",""].forEach(function(op){topEnv[op]=newFunction("a,b","returna"+op+"b;");});</p><p>,console.log</p><p>Javascript</p><p>217:</p></li><li><p>print.</p><p>topEnv["print"]=function(value){console.log(value);returnvalue;};</p><p>.run.,,,,.</p><p>functionrun(){varenv=Object.create(topEnv);varprogram=Array.prototype.slice.call(arguments,0).join("\n");returnevaluate(parse(program),env);}</p><p>Array.prototype.slice.call,,,,join.,run,,.</p><p>run("do(define(total,0),","define(count,1),","while(</p></li><li><p>.</p><p>,fun,,.</p><p>specialForms["fun"]=function(args,env){if(!args.length)thrownewSyntaxError("");functionname(expr){if(expr.type!="word")thrownewSyntaxError("word");returnexpr.name;}varargNames=args.slice(0,args.length-1).map(name);varbody=args[args.length-1];</p><p>returnfunction(){if(arguments.length!=argNames.length)thrownewTypeError("");varlocalEnv=Object.create(env);for(vari=0;i=0;i--){varimage=images[i];if(image.alt){vartext=document.createTextNode(image.alt);image.parentNode.replaceChild(text,image);}}}</p><p>Javascript</p><p>238DocumentObjectModel</p></li><li><p>,createTextNode3DOM(),,.</p><p>.,,getElementsByTagName(childNodes).,,,i1,,1.</p><p>,slice.</p><p>vararrayish={0:"",1:"",length:2};varreal=Array.prototype.slice.call(arrayish,0);real.forEach(function(elt){console.log(elt);});////</p><p>-(1)document.createElement..elt,-..</p><p>.,,.</p><p>functionelt(type){varnode=document.createElement(type);for(vari=1;i</p></li></ul>

Recommended

View more >