Tidy up some of the JavaScript some of the practical skills, maybe you can save a lot of development time, quickly collect it!
- Five-point scoring system
/* Five-point scoring system */ function getrating ( Rating) { if (Rating > 5 | | rating < 0) th Row new Error ("number is not in range" return "★★★★★☆☆☆☆☆". Substring (5-rating, 10- rating);} // call method and result of execution getrating (3); // result: ★★★☆☆
- Produces a string of n identical characters stitched together
/* generate index of the same STR string */ function Create_str (index,str) { returnnew Array (index + 1). Join (str);} // call method and execution result create_str (2, "abc")// result: Abcabc
- Multi-line string
/* The first is a newline through a backslash */var str = "console.log (123) Console.log (123) Console.log (123) Console.log (123) Console.log (123)";
/* the second way to pass the function */ var str = (function ( ) {/* * Console.log (123) Console.log (123) Console.log (123) Console.log (123) **/ }). toString (). Split ("* *") [1];
/* Third Kind */ var str = "Console.log (123)" + "Console.log (123)" + "Console.log (123)" + " Console.log (123) ";
- Quickly generate an array of values that increment sequentially
Array.apply (nullnew Array). Map (function(item, index) { return Index + 1;}); // ES6 Syntax Array.from (Array (+), (v, i) = = i);
- Hijacking function
function (s) { if (Confirm ("Do you want to play the box, the content is" + S + "?) ") { alert (s); }};
- Quickly convert other data types to number types
Console.log (typeof + "1"); // number Console.log (typeof +new Date ())// number
- Rounding and turning into numerical
/* directly after the decimal point is removed, cannot be rounded */ ("10.567890" | |) // Ten number
- Get random Codes
// 14-bit random code // 11-bit random code
- Gets the maximum minimum value in the array
Math.min.apply (math,[1,2,3,4,5,6])// Get minimum value Math.max.apply (math,[1,2,3,4,5,6])// Get minimum Value
Original source: https://www.meetqy.com/article?article_id=44
What you don't know about JS usage tips