作者:xor译者:前端小智来源:Medium点赞再看,微信搜索【大招天下】关注这个没有背景的a大厂,却有一个分享向上积极态度的人。本文已收录到GitHubhttps://github.com/qq44924588...,文章已分类,也整理了很多我的文档和教程资料。大家都说简历里没项目可写,所以给大家找了一个项目,还给了一个【搭建教程】。废话不多说,本文罗列一些常用或者实用的JavaScript代码片段,希望对大家有所帮助。1.三元运算符handleTrue():handleFalse()2.短路或操作constdefaultValue="SomeDefaultValue"letsomeValueNotSureOfItsExistance=nullletexpectingSomeValue=someValueNotSureOfItsExistance||defaultValueconsole.log(expectingSomeValue)//SomeDefaultValue3。conditionisestablishedletsomeValue=trueif(someValue){'console.log(conditionisestablished)')}4.forloopfor(leti=0;i<1e2;i++){//替换是不是很酷i<100}letsomeValues=[1,2,4]for(letvalinsomeValues){console.log(val)}letobj={'key1':'value1','key2':'value2','key3':'value3'}for(letkeyinobj){console.log(key)}5.值到对象的映射letx='x',y='y'letobj={x,y}console.log(obj)//{x:"x",y:"y"}6.Object.entries()constcredits={producer:'大千世界',name:'前端小智',rating:9}constarr=Object.entries(credits)console.log(arr)***output***[['制作人','走向世界的伟大举动'],['姓名','前端小智'],['rating',9]]7.Object.values()constcredits={producer:'GreatMovetotheWorld',name:'前端小智',rating:9}constarr=Object.values(credits)console.log(arr)***输出***['大千世界','前端小智',9]8.模板字面量letname='前端小智'letage=20varsomeStringConcatenateSomeVariable=`我是${name},今年是${age}`console.log(someStringConcatenateSomeVariable)9.解构赋值import{observable,action,runInAction}从'mobx';10.多行字符串letmultiLineString=`somestring\nwithmulti-lineof\ncharacters\n`console.log(multiLineString)11.Array.findshorthandconstpets=[{type:'Dog',name:'Max'},{type:'Cat',name:'Karl'},{type:'Dog',name:'Tommy'}]pet=pets.find(pet=>pet.type==='Dog'&&pet.name==='Tommy')console.log(pet)//{type:'Dog',name:'Tommy'}12.早期练习函数的默认参数值area(h,w){if(!h){h=1;}如果(!w){w=1;}returnh*w}ES6及以后练习functionarea(h=1,w=1){returnh*w}13.箭头函数的简写letsayHello=(name)=>{return`Hello,${name}`}console.log(sayHello('前端小智'))简写如下:letsayHello=name=>`Hello,${name}`console.log(sayHello('前端小智'))14.隐式返回letsomeFuncThatReturnSomeValue=(value)=>{returnvalue+value}console.log(someFuncThatReturnSomeValue('前端小智'))简写如下:letsomeFuncThatReturnSomeValue=(value)=>(value+value)console.log(someFuncThatReturnSomeValue('FrontendXiaozhi'))15.函数必须有一个参数值functionmustHavePatamMethod(param){if(param===undefined){thrownewError('嘿,你必须输入一些参数!');}returnparam;}改写成这样:mustHaveCheck=()=>{thrownewError('Missingparameter!')}methodShoudHaveParam=(param=mustHaveCheck())=>{returnparam}everyone据说有简历里没有项目可写,所以给大家找了一个项目,还附带了一份【搭建教程】16.charAt()shorthandfor'SampleString'.charAt(0)//S//shorthandfor'SampleString'[0]17.条件函数调用functionfn1(){console.log('IamFunction1')}functionfn2(){console.log('IamFunction2')}/*长写*/letcheckValue=3;if(checkValue===3){fn1()}else{fn2()}short写法:(checkValue===3?fn1:fn2)()17.Math.Floorshorthandletval='123.95'console.log(Math.floor(val))//常规写法console.log(~~val)//Shorthand18.Math.powShorthandMath.pow(2,3)//8//Shorthand2**3//819.将字符串转换为数字constnum1=parseInt('100')//Shorthandconsole.log(+"100")console.log(+"100.2")20.&&operationletvalue=1;if(value===1)console.log('Valueisone')//简而言之value&&console.log('Valueisone')21.toStringshorthandletsomeNumber=123console.log(someNumber.toString())//"123"//简写console.log(`${someNumber}`)//"123"22.可选链运算符(即将推出?)ECMAScript中现在有一项值得了解的新提议。letsomeUser={name:'Jack'}letzip=someUser?.address?.zip//可选链接,如果zip未定义,Swift不会引发错误。该语法还支持函数和构造函数调用letaddress=getAddressByZip.?(12345)如果getAddressByZip是调用它的函数,否则,表达式将被评估为未定义。23、用对象方法代替switch语法letfruit='banana';letdrink;switch(fruit){case'banana':drink='bananajuice';休息;case'木瓜':drink='木瓜汁';休息;default:drink='Unknownjuice!'}console.log(drink)//香蕉汁代码部署后可能存在的bug无法实时获知。之后为了解决这些BUG,很多时间都花在了日志调试上,顺便推荐一个好用的BUG监控工具Fundebug。原文:https://medium.com/javascript...交流文章每周更新,可以微信搜索【大千世界】第一时间阅读,回复【福利】前面还有很多-结束视频等着你。本文在GitHub上https://github.com/qq449245884/xiaozhi已收录,欢迎Star。
