前言:
最近看了一些简化js代码的文章,其中有一篇觉得还不错,但是是英文的,也看了一些中文翻译,一个是一字一句翻译太生硬,没有变成自己的东西,另外就是后面作者有新增没有及时更新,于是我按照自己的语言翻译整理成此文,本文特点以言简意赅为主
//longhandlet x;let y = 20; //shorthandlet x, y = 20;
//longhandlet a, b, c;a = 5;b = 8;c = 12;//shorthandlet [a, b, c] = [5, 8, 12];
//longhand let marks = 26; let result; if (marks >= 30) { result = 'pass'; } el { result = 'fail'; } //shorthand let result = marks >= 30 ? 'pass' : 'fail';
本质是利用了||运算符的特点,当前面的表达式的结果转成布尔值为fal
时,则值为后面表达式的结果
//longhandlet imagepath;let path = getimagepath();if (path !== null && path !== undefined && path !== '') { imagepath = path;} el { imagepath = 'default.jpg';}//shorthandlet imagepath = getimagepath() || 'default.jpg';
例如某个函数在某个条件为真时才调用,可简写
//longhandif (isloggedin) { gotohomepage(); }//shorthandisloggedin && gotohomepage();
let x = 'hello', y = 55;//longhandconst temp = x;x = y;y = temp;//shorthand[x, y] = [y, x];
//longhandfunction add(num1, num2) { return num1 + num2;}//shorthandconst add = (num1, num2) => num1 + num2;
需要注意箭头函数和普通函数的区别
使用模板字符串代替原始的字符串拼接
//longhandconsole.log('you got a misd call from ' + number + ' at ' + time);//shorthandconsole.log(`you got a misd call from ${number} at ${time}`);kravmaga
//longhandconsole.log('javascript, often abbreviated as js, is a\n' + 'programming language that conforms to the \n' + 'ecmascript specification. javascript is high-level,\n' + 'often just-in-time compiled, and multi-paradigm.' 上海交通大学校历 );//shorthandconsole.log(`javascript, often abbreviated as js, is a programming language that conforms to the ecmascript specification. javascript is high-level, o人民日报时评精选30篇ften just-in-time compiled, and multi-paradigm.` );
//longhandif (value === 1 || value === 'one' || value === 2 || value === 'two') { // execute some code}// shorthand 1if ([1, 'one', 2, 'two'].indexof(value) >= 0) { // execute some code}// shorthand 2if ([1, 'one', 2, 'two'].includes(value)) { // execute some code }
例如:当属性名和变量名相同时,可直接缩写为一个
let firstname = 'amitav';let lastname = 'mishra';//longhandlet obj = {firstname: firstname, lastname: lastname};//shorthandlet obj = {firstname, lastname};
//longhandlet total = parint('453');let average = parfloat('42.6');//shorthandlet total = +'453';let average = +'42.6';
//longhandlet str = '';for(let i = 0; i < 5; i ++) { str += 'hello ';}console.log(str); // hello hello hello hello hello// shorthand'hello '.repeat(5);// 想跟你说100声抱歉!'sorry\n'.repeat(100);
//longhandconst power = math.pow(4, 3); // 64// shorthandconst power = 4**3; // 64
//longhandconst floor = math.floor(6.8); // 6// shorthandconst floor = ~~6.8; // 6
需要注意,~~仅适用于小于2147483647的数字
let arr1 = [20, 30];//longhandlet arr2 = arr1.concat([60, 80]); // [20, 30, 60, 80]//shorthandlet arr2 = [...arr1, 60, 80]; // [20, 30, 60, 80]
let obj = {x: 20, y: {z: 30}};//longhandconst makedeepclone = (obj) => { let newobject = {}; object.keys(obj).map(key => { if(typeof obj[key] === 'object'){ newobject[key] = makedeepclone(obj[key]); } el { newobject[key] = obj[key]; }});return newobject;}const cloneobj = makedeepclone(obj);//shorthandconst cloneobj = json.par(json.stringify(obj));//shorthand for single level objectlet obj = {x: 20, y: 'hello'};const cloneobj = {...obj};
// shorthandconst arr = [2, 8, 15, 4];math.max(...arr); // 15math.min(...arr); // 2
let arr = [10, 20, 30, 40];//longhandfor时尚家居布置 (let i = 0; i < arr.length; i++) { console.log(arr[i]);}//shorthand//for of loopfor (const val of arr) { console.log(val);}//for in loopfor (const index in arr) { console.log(arr[index]);}
let str = 'jscurious.com';//longhandstr.charat(2); // c//shorthandstr[2]; // c
let obj = {x: 45, y: 72, z: 68, p: 98};// longhanddelete obj.x;delete obj.p;console.log(obj); // {y: 72, z: 68}// shorthandlet {x, p, ...newobj} = obj;console.log(newobj); // {y: 72, z: 68}
let arr = [12, null, 0, 'xyz', null, -25, nan, '', undefined, 0.5, fal];//longhandlet filterarray = arr.filter(function(value) { if(value) return value;});// filterarray = [12, "xyz", -25, 0.5]// shorthandlet filterarray = arr.filter(boolean);// filterarray = [12, "xyz", -25, 0.5]
到此这篇关于20个js简写技巧提升工作效率的文章就介绍到这了,更多相关js简写技巧内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 12:00:49,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/b2598f580e7b026085b411d2a063d75a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:20个JS简写技巧提升工作效率.doc
本文 PDF 下载地址:20个JS简写技巧提升工作效率.pdf
留言与评论(共有 0 条评论) |