麻豆黑色丝袜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
開班信息
北京校區
  • 北京校區
  • 大連校區
  • 廣州校區
  • 成都校區
  • 杭州校區
  • 長沙校區
  • 合肥校區
  • 南京校區
  • 上海校區
  • 深圳校區
  • 武漢校區
  • 鄭州校區
  • 西安校區
  • 青島校區
  • 重慶校區
  • 太原校區
  • 沈陽校區
  • 南昌校區
  • 哈爾濱校區
主站蜘蛛池模板: 高清欧美性暴力猛交| 性生活大片免费看| 大胸校花被老头粗暴在线观看| 第一福利官方航导航| 欧美激情一区二区三区| 99久久精品免费观看国产| 午夜三级黄色片| 被女同桌调教成鞋袜奴脚奴| x8x8在线观看| 玉蒲团之偷情宝鉴电影| 国产亚洲欧美日韩俺去了| 2018av男人天堂| 久久九九国产精品怡红院| 全彩里番acg海贼王同人本子| 一级毛片视频在线| 黄色a级片电影| 天天摸天天摸色综合舒服网| 公啊灬啊灬啊灬快灬深用| 四虎1515hh丶com| 久久久久亚洲精品影视| 麻豆国产精品va在线观看不卡 | 国产线路中文字幕| 国产精品国产三级国产普通话| 亚洲区小说区激情区图片区| 含羞草实验研所入口| 亚洲欧美一区二区三区| 奶特别大的三级日本电影| 国产性生交xxxxx免费| 亚洲高清二区| 国产痴汉系列| 国产捆绑调教| 波多野结衣av高清一区二区三区| 波多野结衣潜入搜查官| 日本巨黄视频| 黑人一个接一个上来糟蹋| 日韩精品视频在线观看免费| 黄色一级电影免费| 日韩日韩日韩日韩日韩| 亚欧洲乱码专区视频| 国产视频一区二| 韩国出轨的女人|