JavaScriptの開発スキルの開発の効率を改善する10の概要。
1.ランダムUIDを生成します。
CONST genUid =()=> { VARの長さ= 20。 VAR soupLength = genUid.soup_.length。 VaRの ID = []; ため(VAR i = 0; iは長さ<; iは++ ){ ID [I] = genUid.soup_.charAt(Math.random()* soupLength)。 } を返す( '' id.join )。 } genUid.soup_ = 'の#$%()* +、 - /:;!。?= @ [] ^ _ `{|}〜ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; genUid(); // ;} l`yCPc9A8IuK N6、%}?
2.指定されたサイクル長の配列を生成しません。
constの一覧= LEN => [... 新しいアレイ(LEN).keys()]; constのリスト =リスト(10)。// [0、1、2、3、4、5、6、7、8、9]
3.配列にコードの太線。
CONSTリスト= [1、1、2、3、6、45、8、5、4、6、5 ]。 constのuniqueList = [... 新しいセット(リスト)]; // [1、2、3、6、45、8、5、4]
カラー値を生成4.RGB 16進数のカラー値。
CONST rgb2Hex =(R、G、B)=> { R = Math.max(Math.min(番号(R)、100)、0)* 2.55 。 G = Math.max(Math.min(番号(G)、100)、0)* 2.55 。 B = Math.max(Math.min(番号(B)、100)、0)* 2.55 。 R =( '0' +(恐らくMath.round(R)|| 0).toString(16))スライス(-2 )。 G =( '0' +(恐らくMath.round(G)|| 0).toString(16))スライス(-2 )。 B =( '0' +(恐らくMath.round(B)|| 0).toString(16))スライス(-2 )。 リターン '#' + R + G + B。 } rgb2Hex( 100、50、0); // "#のff7f00"
混色。
CONST colourBlend =(C1、C2、比)=> { 比 = Math.max(Math.min(数(比)、1)、0 ); せR1 =のparseInt(c1.substring(1、3)、16 )。 せG1 =のparseInt(c1.substring(3、5)、16 )。 せB1 =のparseInt(c1.substring(5,7)、16 )。 せR2 =のparseInt(c2.substring(1、3)、16 )。 せG2 =のparseInt(c2.substring(3、5)、16 )。 せB2 =のparseInt(c2.substring(5,7)、16 )。 せR =恐らくMath.round(R1×(1 -比)+ R2 * 比)。 ましょうG =恐らくMath.round(G1×(1 -比)+ G2 *比率); せ、B =恐らくMath.round(B1×(1 -比)+ B2 * 比)。 R =( '0' +(R || 0).toString(16))スライス(-2 )。 G =( '0' +(G || 0).toString(16))スライス(-2 )。 B =( '0' +(B || 0).toString(16))スライス(-2 )。 リターン '#' + R + G + B。 } colourBlend( '#FF0000の'、 '#1 3333ff'、0.5)。// "#1 991a80"
6.整数が素数であるか否かを判断します。
CONST mathIsPrime = N => { 場合(N === 2 || N === 3 ){ 戻り 真。 } であれば(ますisNaN(N)|| N <= 1 || N%1 = 0 || N%2 == 0 || N%3 == 0] ){ 戻り 偽。 } ため(LET X = 6であり、x <= Math.sqrt(N)+ 1、X + = 6 ){ もし、(N%(X - 1)== 0 || N%(X + 1)== 0 ){ 戻り 偽。 } } 戻り 真。 } mathIsPrime( 0)。// 真
7.クラス・オブジェクト・トラバーサルのアレイ。
CONST要素= document.querySelectorAll(セレクタ)。 [] .prototype.forEach.call(要素(EL、IDX、リスト) => { にconsole.log(EL); // 元素节点 })
8.オブジェクトのタイプを分析します。
const型=データ=> Object.prototype.toString.call(データ).replace(/ ^ \ [オブジェクト(+)\] $ /、 '$ 1' ).toLowerCase() タイプ({})。// オブジェクト
9.多層決意条件の最適化。
スコア= getScore =定数> { CONST scoreData = 新しい新しいアレイ(101).fill(0 ) .MAP((データ、IDX)?=>([IDX、()=>(IDX <60 '失敗':「パス' )])) のconst scoreMap = 新新マップ(scoreData); リターン(scoreMap.get(スコア) ?scoreMap.get(スコア)() : '未知スコア" ); } getScore( 30); // 失敗
10時間形式。
CONSTたDateFormatter =(フォーマッタ、日付)=> { 日付 =(日付?新しい日付(日付):新しい日付) CONST Y = date.getFullYear()+ '' 、 M = date.getMonth()+ 1 、 D = 日.getDate()、 H = date.getHours()、 M = date.getMinutes() のS =のdate.getSeconds()。 戻り formatter.replaceを(/ YYYY | YYYY / gであり、Y) .replace( / YY | YY / gで、Y.substr(2、2 )) .replace(/ MM / gであり、(M <10 '0': '?')+ M) .replace( / DD / gであり、(D <10? '0': '')+ D) .replace( / HH | HH / gで、(H <10 '0'? '')+ H) .replace( / MM / gであり、(M <10? '0': '')+ M) .replace( / SS / gで、( S <10 '0':? '')+ S)。 } たDateFormatter( 'YYYY-MM-DD HH:MM'、 '2019年8月15日13時55分')。// 2019年8月15日13時55分
「そういえば、人々は信じていない人に聞いて、信じていません。」