- 时间:2022-07-06 02:42 编辑: 来源: 阅读:277
- 扫一扫,手机访问
摘要:JavaScript代码技巧
{源代码交易}
1.多表达式多if判断我们可以在数组中存储多个值,可以使用数组include方法。 //long if(x = = ' ABC ' | | x = = ' def ' | | x = = ' GHI ' | | x = = ' JKL '){//logic }//short if([' ABC ',' def ',' GHI ',) //long let test:boolean;if(x & gt;100){ test = true;} else { test = false}//short lettest = (x >: 10)?真:假;//也可以直接让test = x >:10;3.合并变量声明当我们声明同类型的多个变量时,可以缩写如下 //long let test1;设test 2 = 1;//短let test1,test2 = 1;4.合并变量赋值这个方法在我们求解多个变量,给不同的变量赋不同的值的时候非常有用。 //long let test1,test2,test3test1 = 1;test2 = 2test3 = 3;//short let [test1,test2,test3] = [1,2,3];5.& amp& amp如果运算符仅在变量值为真时调用函数,则可以使用& & amp运算符 //long if(test1){ call method();}//简短测试1 & amp;调用方法();6.箭头函数//长函数add(a,b){ return a+b;}//short const add = (a,b)= > >;a+b;7.短函数调用可以使用三元运算符来实现这些功能。 const fun 1 =()= & gt;console . log(' fun 1 ');const fun 2 =()= & gt;console . log(' fun 2 ');//long let test = 1;if(test = = 1){ fun 1();} else { fun 2();}//short (test === 1?fun 1:fun 2)();8.Switch notation我们可以把条件保存在键值对象中,根据条件来使用。 //long switch(data){ case 1:test1();打破;案例二:test 2();打破;案例三:测试();打破;//以此类推...}// short constdata = {1: test1,2: test2,3:test };数据某物& amp& amp数据某物();9.默认参数值//长函数add (test1,test2){ if(test1 = = undefined)test1 = 1;if (test2 ===未定义)test2 = 2返回test1+test2;}//short const add = (test1 = 1,test2 = 2)= > >;(test1+test2);10.扩展运算符//长合并数组常量数据= [1,2,3];const test = [4,5,6]。concat(数据);//短合并数组常量数据= [1,2,3];常量测试= [4,5,6,...数据];//长副本数组const test1 = [1,2,3];Const test2 = test1.slice()//短复制数组const test1 = [1,2,3];常量测试2 = [...test1];1.'模板字符串//long const welcome = ' hi '+test1+' '+test2+'。'//short const welcome = ` hi $ { test1 } $ { test2 } `;12.速记对象let test1 = ' a设test2 = ' b//long letobj = {test1: test1,test2:test2 };//短字母obj = {test1,test2 };13.求数组中的最大最小值const arr = [1,2,3];math . max(…arr);//3 math . min(…arr);// 1个原始链接
![](http://bm.damiseo.cn/15914/www.php-asp.net/dami/0560659001641364747tp64-1.jpg)