026TheswitchStatement 视频加载中。。。 本将主要学习switch分支选择语句,break语句。 switch(day){casemonday:daymondayconsole。log(Plancoursestructure);console。log(Gotocodingmeetup);casetuesday:console。log(Preparetheoryvideos);casewednesday:casethursday:console。log(Writecodeexamples);casefriday:console。log(Recordvideos);casesaturday:casesunday:console。log(Enjoytheweekend:D);default:console。log(Notavalidday!);} 因为day变量的值为monday,case‘monday’相当于判断daymonday是否为真,为真则执行下面代码块,不为真则执行default下代码块。 在控制台输出:PlancoursestructureGotocodingmeetup switch(day){casemonday:daymondayconsole。log(Plancoursestructure);console。log(Gotocodingmeetup);casetuesday:console。log(Preparetheoryvideos);casewednesday:casethursday:console。log(Writecodeexamples);casefriday:console。log(Recordvideos);casesaturday:casesunday:console。log(Enjoytheweekend:D);default:console。log(Notavalidday!);} 修改day变量的值为wednesday,匹配case‘wednesday’,无执行代码块,继续往下执行casethursday,在控制台输出:Writecodeexamples switch(day){casemonday:daymondayconsole。log(Plancoursestructure);console。log(Gotocodingmeetup);casetuesday:console。log(Preparetheoryvideos);casewednesday:casethursday:console。log(Writecodeexamples);casefriday:console。log(Recordvideos);casesaturday:casesunday:console。log(Enjoytheweekend:D);default:console。log(Notavalidday!);} 如果在casemonday中不使用break,初始化day变量为monday,查看控制台输出:PlancoursestructureGotocodingmeetupPreparetheoryvideos 在执行完casemonday后,继续执行casetuesday。所以,break语句要根据实际情况来使用。 TheswitchSswitch(day){casemonday:daymondayconsole。log(Plancoursestructure);console。log(Gotocodingmeetup);casetuesday:console。log(Preparetheoryvideos);casewednesday:casethursday:console。log(Writecodeexamples);casefriday:console。log(Recordvideos);casesaturday:casesunday:console。log(Enjoytheweekend:D);default:console。log(Notavalidday!);}if(daymonday){console。log(Plancoursestructure);console。log(Gotocodingmeetup);}elseif(daytuesday){console。log(Preparetheoryvideos);}elseif(daywednesdaydaythursday){console。log(Writecodeexamples);}elseif(dayfriday){console。log(Recordvideos);}elseif(daysaturdaydaysunday){console。log(Enjoytheweekend:D);}else{console。log(Notavalidday!);} 使用if、elseif、else,并且配合逻辑运算,来改写switch、case代码。 在控制台输出:PlancoursestructureGotocodingmeetup 027StatementsandExpressions 视频加载中。。。 本讲主要讲语句和表达式。 在控制台输出:Im46yearsold。34 这是一个表达式,因为它有值。1991 对于任意数字都是表达式。truefalse!false 这也是一个表达式。它会产生布尔值。if(2310){conststr23} 这是语句。语句是代码块。它本身不会产生值。语句可以包含表达式。 字符串23isbigger是表达式,但是conststr23就是语句。 constmeJconsole。log(Im{20371991}yearsold{me}); 在控制台输出:Im46yearsoldJonas 028TheConditional(Ternary)Operator 视频加载中。。。 本讲主要学习条件运算符。 constage23;age18?console。log(Iliketodrinkwine):console。log(Iliketodrinkwater); 在控制台输出:Iliketodrinkwine 条件运算符由?和:组合完成。 问号前表达式判断逻辑真假,判断为真,则执行冒号前的代码。 constage15;age18?console。log(Iliketodrinkwine):console。log(Iliketodrinkwater); 在控制台输出:Iliketodrinkwater 问号前表达式判断逻辑真假,判断为假,则执行冒号后的代码。 constage23;constdrinkage18?wine:console。log(drink); 在控制台输出:wine 条件运算符,需要三部分内容,也叫三元运算符。 条件运算符也是表达式,表达式有值,可以赋值给变量。 letdrink2;if(age18){drink2}else{drink2}console。log(drink2); 在控制台输出:winewine 如果没有条件运算符,使用ifelse语句来实现,没有条件运算符一行代码简洁。 constage15;console。log(Iliketodrink{age18?wine:water}); 在控制台输出:Iliketodrinkwater 在模板字面量中可以使用条件运算符表达式。 029CodingChallenge4 视频加载中。。。 本讲主要学习使用已学方法按照需求编写代码。 CodingChallenge4 Stevenwantstobuildaverysimpletipcalculatorforwheneverhegoeseatinginaresturant。Inhiscountry,itsusualtotip15ifthebillvalueisbetween50and300。Ifthevalueisdifferent,thetipis20。 史蒂文想为他去餐馆吃饭的时候建立一个非常简单的小费计算器。在他的国家,如果账单价值在50到300之间,通常给15的小费。如果价值不同,小费是20。 1。Yourtaskistocaluclatethetip,dependingonthebillvalue。Createavariablecalledtipforthis。Itsnotallowedtouseanifelsestatement(Ifitseasierforyou,youcanstartwithanifelsestatement,andthentrytoconvertittoaternaryoperator!) 您的任务是根据账单计算小费。为此创建一个名为小费的变量。不允许使用ifelse语句(如果对您来说更容易,您可以从ifelse语句开始,然后尝试将其转换为三元运算符!) 2。Printastringtotheconsolecontainingthebillvalue,thetip,andthefinalvalue(billtip)。Example:Thebillwas275,thetipwas41。25,andthetotalvalue316。25 将包含账单、小费和总价(账单小费)的字符串打印到控制台。示例:账单是275,小费是41。25,总价是316。25 TESTDATA:Testforbillvalues275,40and430 测试数据:测试票据价值275、40和430 HINT:Tocalculate20ofavalue,simplymultiplyitby201000。2 HINT:ValueXisbetween50and300,ifits50300 提示:要计算值的20,只需将其乘以201000。2 提示:值X在50和300之间,如果50300 GOODLUCK constbill430;consttipbill300bill50?bill0。15:bill0。2;console。log(Thebillwas{bill},thetipwas{tip},andthetotalvalue{billtip}); 在控制台输出:Thebillwas275,thetipwas41。25,andthetotalvalue316。25 constbill40;consttipbill300bill50?bill0。15:bill0。2;console。log(Thebillwas{bill},thetipwas{tip},andthetotalvalue{billtip}); 在控制台输出:Thebillwas40,thetipwas8,andthetotalvalue48 030JavaScriptReleasesES5,ES6andESNext 视频加载中。。。 本讲内容主要学习有关JavaScript语言的历史和了解版本情况。 1995年,BrendanEich创建了JavaScript的第一个版本,仅用了10天时间完成。当初叫做Mocha。 1996年,Mocha更改为LiveScript,然后才是JavaScript。JavaScript这个名字是为了吸引Java开发者的营销策略而定义的,实际上JavaScript与Java无关,它是一门独立的语言。 1996年,微软仿照Netscape浏览器发布了IE浏览器,它把JavaScript称为JScript。 1997年,ECMA发布了ECMAScript1(ES1)标准,第一个官方的JavaScript标准。 2005年,ES5发布,此版本拥有很多重要的特性更新。 2015年,ES6发布,也称为ES2015,此版本进行了大量更新。 之后一直延续按年份来发布版本。 使用JavaScript写代码的时候,建议使用最新版本浏览器。 为了使JavaScript适配更多的浏览器版本,需要用Babel的工具转译代码。