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

Embed Size (px)

DESCRIPTION

PDF версия самоучителя JavaScript

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

  • 1. 2. 3. ,4. 5. 6. :7. 8. 9. :10. 11. 12. 13. :14. JavaScript15. DocumentObjectModel16. 17. :-18. 19. HTTP20. 21. :Paint22. Node.js23. :-

    Javascript

    2

  • 2-

    :MarijnHaverbeke:

    CreativeCommonsAttribution-Noncommercial.MIT.

    Gitbook:

    Javascript

    Javascript

    3

  • ,,.,,.,.

    ,,,.,,,..

    ,.,,,:.

    ,,.,.

    Javascript

    4

  • 20,,(-).,.,JavaScript,-,.

    ,,.

    Javascript

    5

  • ,,,.,.

    JavaScript.,..,,,.,.

    ,.,.,.

    .,..,,.,-,,.

    ,..

    ,

    .,,,-,,

    Javascript

    6

  • .,.-.,,,.

    ,,..,.,.

    ..,,.

    ,-,,.:...,.

    ,,.(),.,,.

    -,!,,.,,,..

    Javascript

    7

  • ,,,,.

    ,,.:

    001100010000000000000000001100010000000100000001001100110000000100000010010100010000101100000010001000100000001000001000010000110000000100000000010000010000000100000001000100000000001000000000011000100000000000000000

    ,110,(1+2++10=55)..,.,,...

    ,().-.

    .:

    1. 002. 113. 124. 1125. 20,9.

    Javascript

    8

  • 6. 107. 118. 3.9. 0

    ,,..

    total0count1[loop]comparecount11comparecompare,[end]counttotal1count[loop][end]total

    ,.?.total,count,.,compare,,.,count11,.,,,.compare,count11.countcount1,,11.

    JavaScript:

    vartotal=0,count=1;while(count

  • count+=1;}console.log(total);//55

    ..while.,,(count

  • -NetscapeNavigator..---,,..

    ,JavaScriptJava..JavaScript,Java.-,..

    ,Netscape,,,,,.ECMAScriptECMA.ECMAScriptJavaScript.

    JavaScript..JavaScript,,,,.-,,,:JavaScript..,,.

    .,.,,.,,JavaScript.

    JavaScript.ECMAScript3,,20002010.4-,

    Javascript

    11

  • .,4-2008.5-2009.5-,.

    JavaScript.MongoDBandCouchDB.,Node.js,.

    ,....,..,,.

    ,,,,.

    Node.js..12,HTML-.jsbin.com,..

    ,

    Javascript

    12

  • .,.,..

    -,

    .,,.,.

    ,.,,CD.,.

    ,13.,10,..,13,:

    000011011286432168421

    00001101,8+4+1,13.

    .30.().

    ,

    .

    Javascript

    13,

  • ,,.JavaScript.,.,.:,,,,.

    ...-,.-,,.,.,,.

    JavaScript,.

    ,.JavaScript

    13

    ,

    Javascript

    14,

  • ,13.

    JavaScript(64).,64,.N,,10N.,64264..

    ,816.,,.,64,,.

    ,2^64JavaScript.,.,..10^15,.

    .

    9.81

    e(exponent),:

    2.998e8

    2.99810^8=299800000.

    (integer),,10^15,..,()

    Javascript

    15,

  • ,,64.,..

    ,..JavaScript:

    100+4*11

    +*.,..

    410011?,.,:

    (100+4)*11

    -,-/

    ,.*/,,+-,.:

    1-2+1

    (1-2)+1

    .

    Javascript

    16,

  • .

    ,.%.X%YXY.314%10014,144%120.,.,.

    JavaScript,,.

    Infinity-Infinity,.Infinity-1=Infinity,.,.

    :NaN.notanumber(),.0/0,InfinityInfinity,,.

    ..:

    ","','

    ,...,..

    .

    Javascript

    17,

  • ,,,(escapecharacter).\.\n,\t.

    \n:

    ,:\.\n:

    "\"\\n\""

    ,.+,.:

    ""+""+""+""

    ,4.

    .typeof,,.

    console.log(typeof4.5)//number

    console.log(typeof"x")//string

    Javascript

    18,

  • console.log,.,.

    ,typeof.,,,.(),.

    console.log(-(10-2))//-8

    ,,.JavaScriptBoolean,truefalse().

    :

    console.log(3>2)//trueconsole.log(350

    .:

    console.log(true?1:2);//1console.log(false?1:2);//2

    ,,.true,.false,.

    ,nullundefined,..

    ,,undefined,-.undefinednull,.

    Javascript

    21,

  • ,JavaScript,.:

    console.log(8*null)//0console.log("5"-1)//4console.log("5"+1)//51console.log(""*2)//NaNconsole.log(false==0)//true

    ,JavaScript,,.(coercion).null0,55().+(),-11().

    -(,undefined),NaN.NaNNaN.,,.

    ==,,true,(NaN).,JavaScript..nullundefined,true,nullundefined.

    console.log(null==undefined);//trueconsole.log(null==0);//false

    Javascript

    22,

  • .,nullundefined,null==!=.

    ,?,0,NaNfalse,true.0==false==false.,,:===!==.,,.===falsefalse.

    ,.,,.

    &&||.,,,,.

    ,||,true.

    console.log(null||"user")//userconsole.log("Karl"||"user")//Karl

    ||.,,

    Javascript

    23,

  • .

    &&,.false,,.

    .true||X,X.-.trueX.false&&XX..

    .,,.

    JavaScript:,,.

    ,(true,null)(13,)..(+,-,*,/,and%),(+),(==,!=,===,!==,,=),(&&,||),,(-,!,typeof).

    JavaScript,.,.

    Javascript

    24,

  • -,,JavaScript,().!

    _why,Why's(Poignant)GuidetoRuby

    ,.JavaScript-.

    ,.,.

    ,,.,(,22).,,,,.

    .,..

    ,..

    .:

    Javascript

    25

  • 1;!false;

    ,.,,.-.-(),,..1true,...

    JavaScript.,.,,.,,.

    ??,,,.,JavaScript.

    varcaught=5*5;

    .(keyword)var,.,,=.

    Javascript

    26

  • caught,55.

    .,.:

    varten=10;console.log(ten*ten);//100

    ,(var)..,.,$_.

    .=,.

    varmood="";console.log(mood);//mood="";console.log(mood);//

    ,...,.-,,,.

    Javascript

    27

  • .,,.,,.

    varvasyaDebt=140;vasyaDebt=vasyaDebt-35;console.log(vasyaDebt);//105

    ,,.,undefined.

    var..

    varone=1,two=2;console.log(one+two);//3

    ,var.

    Javascript

    28

  • .,JavaScript.,..

    breakcasecatchcontinuedebuggerdefaultdeletedoelsefalsefinallyforfunctionifimplementsininstanceofinterfaceletnewnullpackageprivateprotectedpublicreturnstaticswitchthrowtruetrytypeofvarvoidwhilewithyieldthis

    ,,,,.

    ,,.,.,,,.,-,.

    function().,.,alert,.:

    alert("!");

    Javascript

    29

  • .,,..,,.,alert.,,.alert,.

    alert,.console.log.JavaScript(Node.js)console.log,-.JavaScript.F12,Command-Option-I.,webconsoledevelopertools.

    :

    varx=30;console.log("thevalueofxis",x);//thevalueofxis30

    ,,console.log.,console.log.,logconsole.4.

    alert

    console.log

    Javascript

    30

  • .,.,,.,Math.max:

    console.log(Math.max(2,4));//4

    ,,.,,.,Math.min(Math.max):

    console.log(Math.min(2,4)+100);//102

    ,.

    ,alert,.OK/Cancelconfirm.true,OK,false,Cancel.

    confirm(",?");

    promptconfirm

    Javascript

    31

  • prompt,.,,.,.

    prompt(",.","...");

    ,.

    ,..,,,.

    vartheNumber=Number(prompt("",""));alert(""+theNumber*theNumber);

    Number.,prompt.StringBoolean,.

    :

    .,

    Javascript

    32

  • ,:

    if.,,.,,.

    vartheNumber=prompt("","");if(!isNaN(theNumber))alert(""+theNumber*theNumber);

    ,,.

    if,.if,.

    isNaNJavaScript,true,NaN().NumberNaN,,.,:,theNumber-.

    ,,,..elseif.

    vartheNumber=Number(prompt("",""));if(!isNaN(theNumber))alert(""+theNumber*theNumber);elsealert("-?");

    ,

    Javascript

    33

  • if/else.

    varnum=Number(prompt("","0"));

    if(num(x,5)),print(""),print(""))

    ,,JS,,.,do,.

    ,,,type,,.

    value.value,.word().name,.,apply.operator,,args.

    >(x,5):

    {

    Javascript

    210:

  • type:"apply",operator:{type:"word",name:">"},args:[{type:"word",name:"x"},{type:"value",value:5}]}

    .,,.,,,,.

    ,9,:.,.

    .,.-.,,.

    parseExpression,,,,.(,),,.,,,,.

    :

    functionparseExpression(program){program=skipSpace(program);varmatch,expr;

    Javascript

    211:

  • 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);

    returnparseApply(expr,program.slice(match[0].length));}

    functionskipSpace(string){varfirst=string.search(/\S/);if(first==-1)return"";returnstring.slice(first);}

    Egg,.skipSpace.

    ,parseExpression(),:,..,,.SyntaxError,JavaScript.

    ,,,parseApply,,.,.

    functionparseApply(expr,program){program=skipSpace(program);if(program[0]!="(")return{expr:expr,rest:program};

    program=skipSpace(program.slice(1));expr={type:"apply",operator:expr,args:[]};while(program[0]!=")"){vararg=parseExpression(program);

    Javascript

    212:

  • 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));}

    ,,parseApply.

    ,.parseExpression,.,parseApplyparseExpression.

    (multiplier(2)(1)),parseApply,,,,.

    ,Egg.parse,,(Egg),.

    functionparse(program){varresult=parseExpression(program);if(skipSpace(result.rest).length>0)thrownewSyntaxError("");returnresult.expr;}

    console.log(parse("+(a,10)"));//{type:"apply",//operator:{type:"word",name:"+"},//args:[{type:"word",name:"a"},//{type:"value",value:10}]}

    !,,,

    Javascript

    213:

  • .

    ?!.,,,,.

    functionevaluate(expr,env){switch(expr.type){case"value":returnexpr.value;

    case"word":if(expr.nameinenv)returnenv[expr.name];elsethrownewReferenceError(":"+expr.name);case"apply":if(expr.operator.type=="word"&&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);}));}}

    varspecialForms=Object.create(null);

    ..,100100.,,.

    .if,,

    Javascript

    214:

  • ,.,,,.

    EggJavaScript.,fun.

    ..,.

    ,Egg..,.

    specialFormsEgg.,...

    specialForms["if"]=function(args,env){if(args.length!=3)thrownewSyntaxError("if");

    if(evaluate(args[0],env)!==false)returnevaluate(args[1],env);elsereturnevaluate(args[2],env);};

    ifEgg.,false,..if?:.,,,,.

    Javascript

    215:

  • EggJavaScript,if.false.

    if,,if,.

    while.