麻豆黑色丝袜jk制服福利网站-麻豆精品传媒视频观看-麻豆精品传媒一二三区在线视频-麻豆精选传媒4区2021-在线视频99-在线视频a

千鋒教育-做有情懷、有良心、有品質的職業教育機構

手機站
千鋒教育

千鋒學習站 | 隨時隨地免費學

千鋒教育

掃一掃進入千鋒手機站

領取全套視頻
千鋒教育

關注千鋒學習站小程序
隨時隨地免費學習課程

當前位置:首頁  >  技術干貨  > 21個面向Web開發人員的JavaScript技巧匯總

21個面向Web開發人員的JavaScript技巧匯總

來源:千鋒教育
發布人:wjy
時間: 2022-06-01 13:26:00 1654061160

  作為程序員,編寫代碼也需要大量的技巧。好的代碼可以讓人耳目一新、通俗易懂、舒適自然,同時又充滿成就感。因此,整理了一些經常使用的JavaScript開發技巧,希望能讓大家寫出耳目一新、通俗易懂、舒適自然的代碼。

21個面向Web開發人員的JavaScript技巧匯總

  **字符串技巧**

  **1、比較時間**

  ```text

  const time1 = "2022-03-02 09:00:00";

  const time2 = "2022-03-02 09:00:01";

  const overtime = time1 < time2;

  // overtime => true

  ```

  **2、格式化money**

  ```text

  const ThousandNum = num => num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

  const money = ThousandNum(1000000);

  // money => '1,000,000'

  ```

  **3、生成隨機ID**

  ```text

  const RandomId = len => Math.random().toString(36).substr(3, len);

  const id = RandomId(10);

  // id => "xdeguewg1f"

  ```

  **4、生成隨機 HEX 顏色值**

  ```text

  const RandomColor = () => "#" + Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, "0");

  const color = RandomColor();

  // color => "#2cbf89"

  ```

  **5、Generate star ratings**

  ```text

  const StartScore = rate => "★★★★★☆☆☆☆☆".slice(5 - rate, 10 - rate);

  const start = StartScore(3);

  // start => '★★★☆☆'

  ```

  **6、網址查詢參數**

  ```text

  const params = new URLSearchParams(location.search.replace(/\?/ig, "")); // location.search = "?name=test&sex=man"

  params.has("test"); // true

  params.get("sex"); // "man"

  ```

  **數字技能**

  **7、Arrangement**

  ```text

  用 Math.floor() 代替正數,用 Math.ceil() 代替負數

  const num1 = ~~ 1.19;

  const num2 = 2.29 | 0;

  const num3 = 3.09 >> 0;

  // num1 num2 num3 => 1 2 3

  ```

  **8、零填充**

  ```text

  const FillZero = (num, len) => num.toString().padStart(len, "0");

  const num = FillZero(1234, 5);

  // num => "01234"

  ```

  **9、轉數**

  ```text

  僅對 null、“”、false、數字字符串有效

  const num1 = +null;

  const num2 = +"";

  const num3 = +false;

  const num4 = +"169";

  // num1 num2 num3 num4 => 0 0 0 169

  ```

  **10、時間戳**

  ```text

  const timestamp = +new Date("2022-03-22");

  // timestamp => 1647907200000

  ```

  **11、精確小數**

  ```text

  const RoundNum = (num, decimal) => Math.round(num * 10 ** decimal) / 10 ** decimal;

  const num = RoundNum(1.2345, 2);

  // num => 1.23

  ```

  **12、平價**

  ```text

  const OddEven = num => !!(num & 1) ? "odd" : "even";

  const num = OddEven(2);

  // num => "even"

  ```

  **13、取最小值最大值**

  ```text

  const arr = [0, 1, 2, 3];

  const min = Math.min(...arr);

  const max = Math.max(...arr);

  // min max => 0 3

  ```

  **14、生成范圍隨機數**

  ```text

  const RandomNum = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;

  const num = RandomNum(1, 10); // 5

  布爾技能

  ```

  **布爾技能**

  **15、短路運算符**

  ```text

  const a = d && 1; // Fake operation, judge from left to right, return a false value when encountering a false value, and no longer execute it later, otherwise return the last true value

  const b = d || 1; // Take the true operation, judge from left to right, return the true value when encountering the true value, and do not execute it later, otherwise return the last false value

  const c = !d; // Returns false if a single expression converts to true, otherwise returns true

  ```

  **16、確定數據類型**

  ```text

  可確定的類型:undefined、null、string、number、boolean、array、object、symbol、date、regexp、function、asyncfunction、arguments、set、map、weakset、weakmap

  function DataType(tgt, type) {

  const dataType = Object.prototype.toString.call(tgt).replace(/\[object (\w+)\]/, "$1").toLowerCase();

  return type ? dataType === type : dataType;

  }

  DataType("test"); // "string"

  DataType(20220314); // "number"

  DataType(true); // "boolean"

  DataType([], "array"); // true

  DataType({}, "array"); // false

  ```

  **17、檢查數組是否為空**

  ```text

  const arr = [];

  const flag = Array.isArray(arr) && !arr.length;

  // flag => true

  18、滿足條件時執行

  const flagA = true; // Condition A

  const flagB = false; // Condition B

  (flagA || flagB) && Func(); // Execute when A or B is satisfied

  (flagA || !flagB) && Func(); // Execute when A is satisfied or B is not satisfied

  flagA && flagB && Func(); // Execute when both A and B are satisfied

  flagA && !flagB && Func(); // Execute when A is satisfied and B is not satisfied

  19、如果非假則執行

  const flag = false; // undefined、null、""、0、false、NaN

  !flag && Func();

  ```

  **20、數組不為空時執行**

  ```text

  const arr = [0, 1, 2];

  arr.length && Func();

  ```

  **21、對象不為空時執行**

  ```text

  const obj = { a: 0, b: 1, c: 2 };

  Object.keys(obj).length && Func();

  ```

  **- End -**

  更多關于“html5培訓”的問題,歡迎咨詢千鋒教育在線名師。千鋒已有十余年的培訓經驗,課程大綱更科學更專業,有針對零基礎的就業班,有針對想提升技術的提升班,高品質課程助理你實現夢想。

tags:
聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。
10年以上業內強師集結,手把手帶你蛻變精英
請您保持通訊暢通,專屬學習老師24小時內將與您1V1溝通
免費領取
今日已有369人領取成功
劉同學 138****2860 剛剛成功領取
王同學 131****2015 剛剛成功領取
張同學 133****4652 剛剛成功領取
李同學 135****8607 剛剛成功領取
楊同學 132****5667 剛剛成功領取
岳同學 134****6652 剛剛成功領取
梁同學 157****2950 剛剛成功領取
劉同學 189****1015 剛剛成功領取
張同學 155****4678 剛剛成功領取
鄒同學 139****2907 剛剛成功領取
董同學 138****2867 剛剛成功領取
周同學 136****3602 剛剛成功領取
相關推薦HOT
開班信息
北京校區
  • 北京校區
  • 大連校區
  • 廣州校區
  • 成都校區
  • 杭州校區
  • 長沙校區
  • 合肥校區
  • 南京校區
  • 上海校區
  • 深圳校區
  • 武漢校區
  • 鄭州校區
  • 西安校區
  • 青島校區
  • 重慶校區
  • 太原校區
  • 沈陽校區
  • 南昌校區
  • 哈爾濱校區
主站蜘蛛池模板: 伊人电影综合网| 午夜电影院理论片8888琪琪 | 波多野结衣中文字幕一区二区三区| 男人的j进入女人的p的动态图| 狠狠色噜噜狠狠狠狠色吗综合| 91在线国内在线播放老师| 在线www| 最近高清国语中文在线观看免费| 国产精品久久久久久一区二区三区| 欧美猛交xxxx免费看| 免费看黄网站在线看| 亚洲欧美日韩综合在线播放| 免费在线亚洲| 怡红院www| 国产日产久久高清欧美一区| 九九久久99综合一区二区| 日本中文字幕一区二区高清在线| 99香蕉国产精品偷在线观看| 欧美婷婷六月丁香综合色| 欧美日韩国产精品| 三个黑人上我一个经过| 国产三级在线免费| 成人爽a毛片在线视频| 动漫美女羞羞网站| 一区二区三区视频| 天堂网欧美| 羞羞的视频在线免费观看| 美国式禁忌3| 新婚之夜性史观看| 免费日韩三级| 久久99精品国产麻豆不卡| 一区二区三区午夜| 国产成人精品cao在线| 性的暴力电影| 女人是男人的未来的人| 小明天天看成人免费看| 尹人久久大香找蕉综合影院| 99在线精品免费视频| 国语自产精品视频在线区| 久久99亚洲网美利坚合众国| 欧美成人免费在线观看|