麻豆黑色丝袜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
開班信息
北京校區
  • 北京校區
  • 大連校區
  • 廣州校區
  • 成都校區
  • 杭州校區
  • 長沙校區
  • 合肥校區
  • 南京校區
  • 上海校區
  • 深圳校區
  • 武漢校區
  • 鄭州校區
  • 西安校區
  • 青島校區
  • 重慶校區
  • 太原校區
  • 沈陽校區
  • 南昌校區
  • 哈爾濱校區
主站蜘蛛池模板: 香蕉视频yy| 精品大臿蕉视频在线观看| 久久国产高清| 你看桌子上都是你流的| 天堂在线中文字幕| 被公侵幕岬奈奈美中文字幕| 久久电影网午夜鲁丝片免费| 经典三级完整版电影在线观看| 国产一区二区三区不卡在线观看 | 亚洲福利二区| 久久99国产精品久久99果冻传媒| 篠田优在线一区中文字幕| 爱搞激情在线| 久久99国产视频| 日本艳妇| 老鸭窝二区三区在线播放| a级精品国产片在线观看| 麻豆精品不卡国产免费看 | 2021日韩麻豆| 台湾swag在线观看| 精品国产精品久久一区免费式 | aaaa欧美高清免费| 动漫无遮挡在线观看| 久久精品成人一区二区三区| 中文字幕亚洲综合久久| 国产亚洲美女精品久久久久| 健身私教弄了我好几次啊| 亚洲国产天堂久久综合2261144| 练瑜伽的时候进入| 波多野结衣伦理视频| 男人天堂网在线视频| sihu国产精品永久免费| hkpic比思特区东方美人| 免费看a级毛片| 国产精品igao视频网网址| 午夜爽爽| 在线播放国产不卡免费视频| 亚洲成av人片在线观看| 午夜电影免费观看| 免费精品99久久国产综合精品| 蜜桃成熟时3之蜜桃仙子电影|