Small front-end knowledge often encountered ----- js articles

A js

1. The width and height of the visible region

①  width document.documentElement.clientWidth // visible area

    document.documentElement.clientHeight // highly visible area

width 100 vw // visible area

    Highly visible area of ​​100 vh //

     vw: 1vw equal to 1% of the width of the viewport

     vh: 1vh equal to 1% of the height of the viewport

2. Regular Expressions

Regular expression describes a set of strings, a string can be used to check if they contain a certain substring matching string will replace or remove the string meet a certain criteria from a string. ,

Access: 1. As long as the rule-fuzzy match multiple keyword 2. As long as according to the rules for compliance verification string

Use: 1. directly only by positive amount: var reg = / regular / ig; 2. with new: var reg = new RegExp ( "regular", "ig");

String commonly used in regular API:

Search:

1. Find keywords appear in a fixed position

var i = str.indexOf ( "keyword", fromi)     

 var i = str.lastIndexOf ( "keyword", fromi)

 2. determine whether that comply with the rules Keywords:

  var i = str.search (/ 正 则 / in)

3. Obtain all the keywords contents:

  var i = str.match (/ regular / ig); if not g: match the equivalent of search, except that Image content can be obtained: [0: "keyword", index: i]

4. That content is obtained for each keyword, and obtaining the position of each keyword: reg.exec (str)

replace:

Abbreviations: All words will be replaced with a uniform replacement value

str = str, replace (/ regular / ig, 'substitute value')

Advanced: According to different keywords, substitute different values

str = str.replace (/ regular / ig,

function(kw,$1,$2,...){

      return value is determined according to different alternatives return kw

)

Delete: Replace the keyword ''

Cutting: var subStrs = str.split (/ regular /);

3.isNaN()

 isNaN () function is used to check whether the non-numeric parameter.

 isNaN(233);  false

 isNaN (Ni); true

4. rounding to two decimal places or

rounding:

1. and num = 2.3362412322;

num = num.toFixed (2); // output 2.34

No rounding:

2.Math.floor (15.66842340 * 100) / 100 // output 15.66

3.Number (. 15.66842340.toString () match (/ ^ \ d + (:?.? \ \ D {0,2}) /)) // output 15.66

4. Share the classic solution:

 
Figure I

ToSei

parseInt(2.56789)   //2

Math.ceil(2.567)    //2

Math.ceil(2.56789)   //3

Math.round(2.5678)   //3

m^0  

m|0

m>>>0

 

connection:

var newArr = arr1.concat (arr2); // after the spliced ​​arr2 arr1, returns a new array

Note: concat authority to modify the original array, only to return a new array, you must catch the result of splicing with variables.

 concat may be any type of parameter can be an array, number, string

If .concat (value 1, value 2, arr2, ....) ;, then .concat array type parameters may be broken down to a single value.

Interception sub-array:

 var subArr = arr.slice (starti, endi + 1); // arr taken in starti subarray position to the end position endi.

 Note: containing free end of the head, to the end position +1

 Support negative argument: the reciprocal of the n-th position, in fact, executed or length-n

If the second parameter is omitted, showing the position taken directly from starti to the end.

concat and slice are not directly modify the original array, you must use the new variable reception

Delete / insert / replace the array

splice: delete, insert, replace

Delete: arr.splice (starti, n)

Delete arr starting position in starti n elements

Note: n is the number, irrespective containing free end of the head

splice can return the removed element consisting of a temporary array.

 was delete = arr.splice (Startie, n)

Insert: arr.splice (starti, 0, the value 1, value 2, ...)

In the insertion position starti arr value 1, value 2, ..., and the original value after starti position, are shifted backward cis

Note: 0 means do not delete, insert a new value only

Does not support the break array type parameter, if passed in the array, it will form a sub-array

Alternatively: var deletes = arr.splice (starti, n, value 1, value 2, ...)

  Starti delete start position of n, then insert the value 1, value 2 starti position.

  Note: The number of elements to delete, and insert the number of new worth, do not necessarily coincide.

conca and splicet:

        the concat: Excellent: break up the array type parameters

                Missing: only at the beginning or end of stitching

        splice: Excellent: the insertion position can be specified

                 Missing: break up the array parameter is not supported

Guess you like

Origin www.cnblogs.com/IDPUBLIC/p/11203459.html