What you don't know about JS usage tips

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.