整合營銷服務商

          電腦端+手機端+微信端=數據同步管理

          免費咨詢熱線:

          如何讓別人看不懂你的 JS 代碼?

          如何讓別人看不懂你的 JS 代碼?

          們在上周的文章中一種奇特的 JavaScript 編碼風格:Get 一種可以用來裝逼的 JavaScript 編碼風格,引起了廣大網友的熱議。

          這是實際上屬于一種代碼混淆技術,可以讓們的代碼更難閱讀和逆向,同時也能租網一些惡意爬蟲和自動化分析。天我就帶大家來看看還有哪些其他能讓 JavaScript 代碼變得難以分析的代碼混淆技術。

          我們以下面這段代碼為例:

          <pre class="hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">console.log("ConardLi",666);</pre>
          

          通過一些轉換,它可以變成下面這個樣子:

          怎么做到的呢?我們一起來看一下~

          十六進制字符串編碼

          我們嘗試去 Javascript Obfuscator 這個網站,選中 Encode Strings 復選框,將得到下面的代碼:

          <pre class="prettyprint hljs markdown" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">console["\x6C\x6F\x67"]("\x43\x6F\x6E\x61\x72\x64\x4C\x69\x20"+ 666)</pre>
          

          它的原理很簡單,就是將字符串的每個 ASCII? 字符轉換為十六進制形式(將函數調用改為用括號的形式,例如 console.log? -> console['log'] 在代碼混淆中也是相當常見的做法),這就是最簡單的混淆了,但是只能騙騙小白,我們可以輕易的反解:

          這種技術還有一些其他變體,比如用 unicode 編碼替換字符。

          https://javascriptobfuscator.com/Javascript-Obfuscator.aspx

          字符串數組映射

          還是在上面的網站,我們選中 Move Strings 這個選項,得到的代碼是下面這樣的:

          <pre class="prettyprint hljs markdown" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">var _0x8925=["\x43\x6F\x6E\x61\x72\x64\x4C\x69\x20","\x6C\x6F\x67"];
          console[_0x8925[1]](_0x8925[0]+ 666)</pre>
          

          多了個字符串數組,通過在不同索引處引入數組來間接使用這些字符串。

          死代碼注入

          死代碼其實指的就是一些無法訪問的代碼,我們可以在原本的代碼上額外注入一些永遠無法訪問的代碼來讓代碼難以閱讀,但是同時也會讓代碼變得更大。這次我們嘗試一下 defendjs:

          安裝:

          <pre class="hljs ruby" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">$ npm install -g https://github.com/alexhorn/defendjs.git</pre>
          

          我們嘗試創建一個 conardli.js 并且將上面的代碼放入這個文件,執行下面的命令:

          <pre class="hljs verilog" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">$ defendjs --input conardli.js --features dead_code --output .</pre>
          

          得到了下面這一大坨代碼:

          <pre class="prettyprint hljs javascript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">(function () {
              function a(a, d) {
                  var b=new Array(0);;
                  var c=arguments;
                  while (true)
                      try {
                          switch (a) {
                          case 21309:
                              return;
                          case 792:
                              function e(a, b) {
                                  return Array.prototype.slice.call(a).concat(Array.prototype.slice.call(b));
                              }
                              function f() {
                                  var a=arguments[0], c=Array.prototype.slice.call(arguments, 1);
                                  var b=function () {
                                      return a.apply(this, c.concat(Array.prototype.slice.call(arguments)));
                                  };
                                  b.prototype=a.prototype;
                                  return b;
                              }
                              function g(a, b) {
                                  return Array.prototype.slice.call(a, b);
                              }
                              function h(b) {
                                  var c={};
                                  for (var a=0; a < b.length; a +=2) {
                                      c[b[a]]=b[a + 1];
                                  }
                                  return c;
                              }
                              function i(a) {
                                  return a.map(function (a) {
                                      return String.fromCharCode(a & ~0 >>> 16) + String.fromCharCode(a >> 16);
                                  }).join('');
                              }
                              function j() {
                                  return String.fromCharCode.apply(null, arguments);
                              }
                              console.log('ConardLi', 666);
                              a=21309;
                              break;
                          }
                      } catch (b) {
                          $defendjs$tobethrown=null;
                          switch (a) {
                          default:
                              throw b;
                          }
                      }
              }
              a(792, {});
          }())</pre>
          

          代碼很大,其實仔細分析就會發現其余插入的代碼都是無法運行的:

          最頂層包了一個 IIFE?,然后有一個 a? 函數,a、b? 兩個參數。調用 a? 函數時只傳入了第一個參數 792,然后就會發現 a 函數里有個 switch? 語句,只會執行到第二個 case,里面是這樣的語句:

          e、f、g、h、j、i 這幾個函數都是沒有調用的,所以只會執行最后的 console.log('ConardLi', 666); 語句 ...

          https://github.com/alexhorn/defendjs

          作用域混淆

          我們將代碼還原回去,重新執行 defendjs? 的 scope 能力:

          <pre class="hljs verilog" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">$ defendjs --input conardli.js --features scope --output .</pre>
          
          <pre class="prettyprint hljs clojure" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">(function () {
              {
                  {
                      function b(a, b) {
                          return Array.prototype.slice.call(a).concat(Array.prototype.slice.call(b));
                      }
                      function c() {
                          var a=arguments[0], c=Array.prototype.slice.call(arguments, 1);
                          var b=function () {
                              return a.apply(this, c.concat(Array.prototype.slice.call(arguments)));
                          };
                          b.prototype=a.prototype;
                          return b;
                      }
                      function d(a, b) {
                          return Array.prototype.slice.call(a, b);
                      }
                      function e(b) {
                          var c={};
                          for (var a=0; a < b.length; a +=2) {
                              c[b[a]]=b[a + 1];
                          }
                          return c;
                      }
                      function f(a) {
                          return a.map(function (a) {
                              return String.fromCharCode(a & ~0 >>> 16) + String.fromCharCode(a >> 16);
                          }).join('');
                      }
                      function g() {
                          return String.fromCharCode.apply(null, arguments);
                      }
                  }
                  var a=[];
                  console.log('ConardLi', 666);
              }
          }())</pre>
          

          這個可能看起來像是前面的一個簡單版本,但是有一個關鍵的區別:它引入了多個具有重復標識符的詞法作用域。例如,a? 可能是最內層作用域中第一個函數的參數,也可以是第二個函數中的變量,甚至可以是與我們的 conaole.log 語句相同作用域中的變量。在這個簡單的示例中,很容易看穿,因為最內層范圍內的任何函數都不會在任何地方被調用,但是,現實的業務代碼往往是很復雜的,混淆后就不那么容易看穿了。

          字符編碼

          還是使用 defendjs ,對我們的代碼執行下面的命令:

          <pre class="hljs verilog" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">$ defendjs --input conardli.js --features literals --output .</pre>
          

          得到下面的代碼:

          <pre class="prettyprint hljs clojure" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">(function () {
              function c() {
                  var c=arguments;
                  var b=[];
                  b[1]='';
                  b[1] +=a(67, 111, 110);
                  b[1] +=a(97);
                  b[1] +=a(114, 100);
                  b[1] +=a(76, 105);
                  return b[1];
              }
              {
                  {
                      function e(a, b) {
                          return Array.prototype.slice.call(a).concat(Array.prototype.slice.call(b));
                      }
                      function d() {
                          var a=arguments[0], c=Array.prototype.slice.call(arguments, 1);
                          var b=function () {
                              return a.apply(this, c.concat(Array.prototype.slice.call(arguments)));
                          };
                          b.prototype=a.prototype;
                          return b;
                      }
                      function f(a, b) {
                          return Array.prototype.slice.call(a, b);
                      }
                      function g(b) {
                          var c={};
                          for (var a=0; a < b.length; a +=2) {
                              c[b[a]]=b[a + 1];
                          }
                          return c;
                      }
                      function h(a) {
                          return a.map(function (a) {
                              return String.fromCharCode(a & ~0 >>> 16) + String.fromCharCode(a >> 16);
                          }).join('');
                      }
                      function a() {
                          return String.fromCharCode.apply(null, arguments);
                      }
                  }
                  var b=[];
                  console.log(d(c, b)(), 666);
              }
          }())</pre>
          

          在這種情況下,硬編碼會被轉換成 Unicode 然后重新計算,這樣直接閱讀代碼就很難再直接看穿硬編碼的字符串了。

          變量縮短

          Mangling 是一種為了優化和混淆目的而縮短變量和屬性名稱的轉換。比如下面的代碼:

          <pre class="prettyprint hljs vbscript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">let sixSixSix=666;
          let name="ConardLi ";
          console.log(name + sixSixSix);</pre>
          

          我們使用 DefendJS? 的 mangling 功能:

          <pre class="hljs verilog" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">$ defendjs --input conardli.js --features mangle --output .</pre>
          

          得到的代碼是:

          <pre class="prettyprint hljs scheme" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">(function () {
              var a=666;
              var b='ConardLi! ';
              console.log(b + a);
          }())</pre>
          

          兩個變量都被重新命名了,在這個簡單的例子下還是很好分析的。但是如果是龐大的業務代碼,這會讓我們的代碼變得非常難以閱讀。

          代碼壓縮

          下面,綜合利用一下幾種技術,執行:

          <pre class="prettyprint hljs verilog" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">defendjs --input conardli.js --output . --features=control_flow,literals,mangle,compress</pre>
          

          得到下面的代碼:

          <pre class="prettyprint hljs javascript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">(function(){function a(d,g){var b=new Array(1);;var e=arguments;while(true)t</pre>
          

          來源: https://developer.51cto.com/article/714813.html

          avaScript不斷發展壯大,
          因為它是最容易上手的語言之一,因此為市場上的新
          成為技術怪才打開了大門。(問號臉?)

          的確,JavaScript可以做很多出色的事情!還有很多東西要學習。

          而且,無論你是JavaScript的新手還是更多的專業開發人員,學習新知識總是一件好事。

          本文整理了一些非常有用的單行代碼(20+),這些單行代碼可以幫助你提高工作效率并可以幫助調試代碼。

          什么是單行代碼?

          單行代碼是一種代碼實踐,其中我們僅用一行代碼執行某些功能。

          01-隨機獲取布爾值

          此函數將使用Math.random()方法返回布爾值(真或假)。
          Math.random創建一個介于0和1之間的隨機數,然后我們檢查它是否大于或小于0.5。
          這意味著有50/50的機會會得到對或錯。

          const getRandomBoolean = () => Math.random() >= 0.5;
          
          console.log(getRandomBoolean());
          // a 50/50 chance of returning true or false
          

          02-檢查日期是否為周末

          通過此功能,你將能夠檢查提供的日期是工作日還是周末。

          const isWeekend = (date) => [0, 6].indexOf(date.getDay()) !== -1;
          
          console.log(isWeekend(new Date(2021, 4, 14)));
          // false (Friday)
          console.log(isWeekend(new Date(2021, 4, 15)));
          // true (Saturday)
          

          03-檢查數字是偶數還是奇數

          簡單的實用程序功能,用于檢查數字是偶數還是奇數。

          const isEven = (num) => num % 2 === 0;
          
          console.log(isEven(5));
          // false
          console.log(isEven(4));
          // true
          

          04-獲取數組中的唯一值(數組去重)

          從數組中刪除所有重復值的非常簡單的方法。此函數將數組轉換為Set,然后返回數組。

          const uniqueArr = (arr) => [...new Set(arr)];
          
          console.log(uniqueArr([1, 2, 3, 1, 2, 3, 4, 5]));
          // [1, 2, 3, 4, 5]
          

          05-檢查變量是否為數組

          一種檢查變量是否為數組的干凈簡便的方法。

          當然,也可以有其他方法

          const isArray = (arr) => Array.isArray(arr);
          
          console.log(isArray([1, 2, 3]));
          // true
          console.log(isArray({ name: 'Ovi' }));
          // false
          console.log(isArray('Hello World'));
          // false
          

          06-在兩個數字之間生成一個隨機數

          這將以兩個數字為參數,并將在這兩個數字之間生成一個隨機數!

          const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);
          
          console.log(random(1, 50));
          // could be anything from 1 - 50
          

          07-生成隨機字符串(唯一ID?)

          也許你需要臨時的唯一ID,這是一個技巧,你可以使用它在旅途中生成隨機字符串。

          const randomString = () => Math.random().toString(36).slice(2);
          
          console.log(randomString());
          // could be anything!!!
          

          08-滾動到頁面頂部

          所述window.scrollTo()方法把一個XY坐標滾動到。
          如果將它們設置為零和零,我們將滾動到頁面頂部。

          image.png

          const scrollToTop = () => window.scrollTo(0, 0);
          
          scrollToTop();
          

          09-切換布爾

          切換布爾值是非常基本的編程問題之一,可以通過許多不同的方法來解決。
          代替使用if語句來確定將布爾值設置為哪個值,你可以使用函數使用!翻轉當前值。
          運算符。

          // bool is stored somewhere in the upperscope
          const toggleBool = () => (bool = !bool);
          //or
          const toggleBool = b => !b;
          

          10-交換兩個變量

          下面的代碼是不使用第三個變量而僅使用一行代碼即可交換兩個變量的更簡單方法之一。

          [foo, bar] = [bar, foo];
          

          11-計算兩個日期之間的天數

          要計算兩個日期之間的天數,
          我們首先找到兩個日期之間的絕對值,然后將其除以86400000(等于一天中的毫秒數),最后將結果四舍五入并返回。

          const daysDiff = (date, date2) => Math.ceil(Math.abs(date - date2) / 86400000);
          
          console.log(daysDiff(new Date('2021-05-10'), new Date('2021-11-25')));
          // 199
          

          12-將文字復制到剪貼板

          PS:你可能需要添加檢查以查看是否存在navigator.clipboard.writeText

          const copyTextToClipboard = async (text) => {
            await navigator.clipboard.writeText(text);
          };
          

          13-合并多個數組的不同方法

          有兩種合并數組的方法。其中之一是使用concat方法。另一個使用擴展運算符()。

          PS:我們也可以使用“設置”對象從最終數組中復制任何內容。

          // Merge but don't remove the duplications
          const merge = (a, b) => a.concat(b);
          // Or
          const merge = (a, b) => [...a, ...b];
          
          // Merge and remove the duplications
          const merge = [...new Set(a.concat(b))];
          // Or
          const merge = [...new Set([...a, ...b])];
          

          14-獲取javascript語言的實際類型

          人們有時會使用庫來查找JavaScript中某些內容的實際類型,這一小技巧可以節省你的時間(和代碼大小)。

          const trueTypeOf = (obj) => {
            return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
          };
          
          console.log(trueTypeOf(''));
          // string
          console.log(trueTypeOf(0));
          // number
          console.log(trueTypeOf());
          // undefined
          console.log(trueTypeOf(null));
          // null
          console.log(trueTypeOf({}));
          // object
          console.log(trueTypeOf([]));
          // array
          console.log(trueTypeOf(0));
          // number
          console.log(trueTypeOf(() => {}));
          // function
          

          15-在結尾處截斷字符串

          需要從頭開始截斷字符串,這不是問題!

          const truncateString = (string, length) => {
            return string.length < length ? string : `${string.slice(0, length - 3)}...`;
          };
          
          console.log(
            truncateString('Hi, I should be truncated because I am too loooong!', 36),
          );
          // Hi, I should be truncated because...
          

          16-從中間截斷字符串

          從中間截斷字符串怎么樣?

          該函數將一個字符串作為第一個參數,然后將我們需要的字符串大小作為第二個參數,然后從第3個和第4個參數開始和結束需要多少個字符

          const truncateStringMiddle = (string, length, start, end) => {
            return `${string.slice(0, start)}...${string.slice(string.length - end)}`;
          };
          
          console.log(
            truncateStringMiddle(
              'A long story goes here but then eventually ends!', // string
              25, // 需要的字符串大小
              13, // 從原始字符串第幾位開始截取
              17, // 從原始字符串第幾位停止截取
            ),
          );
          // A long story ... eventually ends!
          

          17-大寫字符串

          好吧,不幸的是,JavaScript沒有內置函數來大寫字符串,但是這種解決方法可以實現。

          const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);
          
          console.log(capitalize('hello world'));
          

          // Hello world

          18-檢查當前選項卡是否在視圖/焦點內

          此簡單的幫助程序方法根據選項卡是否處于視圖/焦點狀態而返回truefalse

          const isTabInView = () => !document.hidden;  // Not hidden
          
          isTabInView();
          

          // true/false

          19-檢查用戶是否在Apple設備上

          如果用戶使用的是Apple設備,則返回true

          const isAppleDevice = () => /Mac|iPod|iPhone|iPad/.test(navigator.platform);
          
          console.log(isAppleDevice);
          // true/false
          

          20-三元運算符

          當你只想在一行中編寫if..else語句時,這是一個很好的代碼保護程序。

          // Longhand
          const age = 18;
          let greetings;
          
          if (age < 18) {
            greetings = 'You are not old enough';
          } else {
            greetings = 'You are young!';
          }
          
          // Shorthand
          const greetings = age < 18 ? 'You are not old enough' : 'You are young!';
          

          21-短路評估速記

          在將變量值分配給另一個變量時,可能要確保源變量不為null,未定義或為空。
          可以編寫帶有多個條件的long if語句,也可以使用短路評估。

          家好呀!今天小紅書上來分享一篇關于友情鏈接HTML代碼的教程,讓我們一起來學習如何在網頁上添加友情鏈接吧!

          我們需要了解什么是友情鏈接。友情鏈接,顧名思義,就是在網頁上相互分享鏈接,以增加網站之間的互相推薦和訪問。通過添加友情鏈接,我們可以促進多個網站之間的互動和合作,共同提高網站的流量和知名度。

          我們該如何在網頁中添加友情鏈接呢?首先,我們需要使用HTML代碼來實現這一功能。下面將給大家介紹一種簡單的方法。

          首先打開你要添加友情鏈接的網頁,然后找到你想要添加友情鏈接的位置。通常,我們將這些鏈接放置在頁面底部的“友情鏈接”或“合作伙伴”欄目中比較合適。

          我們需要在HTML代碼中插入一個“”標簽,表示一個無序列表。在這個列表中,每一個鏈接都將被表示為一個“”標簽,這樣我們就可以依次添加多個鏈接了。

          下面是一個示例代碼,供大家參考:


          主站蜘蛛池模板: 农村人乱弄一区二区 | 无码精品黑人一区二区三区| 久久精品免费一区二区三区| 男人的天堂精品国产一区| 无码乱码av天堂一区二区| 一区二区三区观看| 韩国一区二区三区| 中字幕一区二区三区乱码| 视频在线一区二区| 无码人妻一区二区三区免费视频| 一区二区免费电影| 精品无码人妻一区二区三区品| 亚洲av色香蕉一区二区三区| 高清一区二区三区| 一本AV高清一区二区三区| 亚洲综合av一区二区三区不卡| 夜精品a一区二区三区| 福利在线一区二区| 国产精品视频一区二区噜噜| 国产成人精品一区二三区熟女| 亚洲国产综合无码一区二区二三区| AV无码精品一区二区三区宅噜噜| 色偷偷久久一区二区三区| 成人精品一区二区三区电影| 国产在线精品一区免费香蕉| 久久毛片一区二区| 大伊香蕉精品一区视频在线| 麻豆果冻传媒2021精品传媒一区下载| 精品无码一区二区三区在线 | 亚洲高清美女一区二区三区| 国产一区二区三区在线观看免费| 久久人妻内射无码一区三区| 国产主播福利精品一区二区| 无码av免费一区二区三区| 日本免费精品一区二区三区| 日韩精品一区二区三区影院| 99国产精品欧美一区二区三区| 无码人妻一区二区三区免费| 变态调教一区二区三区| 色老头在线一区二区三区| 日韩高清一区二区三区不卡|