JS of front-end three piece set -- JavaScript built-in method

1,Number

1-1 properties

  • MAX_VALUE JS is the largest number that can be represented
  • MIN_VALUE JS is the smallest number that can be represented

1-2 method

  • toFixed(length) specifies the decimal of the reserved length
  • toExponential() is represented by scientific counting
  • toPrecision(length) requires the number to display integer + decimal according to the specified length
  • toString(number) converts a number into a string, which can be returned according to the specified base

2,String

methodexplain
.lengthReturn length
.trim()Remove blank
.trimLeft()Remove left margin
.trimRight()Remove the blank on the right
.charAt(n)Returns the nth character
.concat(value, ...)Splicing
.indexOf(substring, start)Subsequence position
.substring(from, to)Get subsequence by index (negative numbers are not supported)
.slice(start, end)Slice (use it directly)
.toLowerCase()a lowercase letter
.toUpperCase()Capitalize
.split(delimiter, limit)division

2-1 properties

  • Length string length

2-2 method

  • charAt(index) returns the character at the specified position
  • concat(string) connection string
  • indexOf(str) returns the first occurrence of a small string in a string object. Position - 1 indicates that it does not exist
  • lastIndexOf() returns the position of the last occurrence of a small character in a string
  • substr(start, length) intercepts the string, omitting the length to the end
  • substring(start, end) intercepts the string, omitting the end position until the end
  • As like as two peas (slice, start, end) are substring.
  • split(char) splits strings into arrays
  • toUpperCase() converts the string to uppercase
  • toLowerCase() converts the string to lowercase
  • match() the matching string can be regular
  • search() finds the available regular strings
  • replace() replaces the string with a regular string
  • charCodeAt() returns the Unicode encoding of the character at the specified position.
  • String.formCharCode() creates a string from the character encoding.

2-3 code

var name = 'egondsb'
undefined

name.length
7

var name1 = '  egonDSB  '
undefined
name1
"  egonDSB  "
name1.trim()
"egonDSB"
name1.trimLeft()
"egonDSB  "
name1.trimRight()
"  egonDSB"

var name2 = '$$jason$$'
undefined
name2.trim('$')  # You cannot specify what to remove with parentheses
"$$jason$$"

name2.charAt(0)
"$"
name2.indexOf('as')
3

name2.substring(0,5)
"$$jas"
name2.slice(0,5)
"$$jas"
name2.substring(0,-1)  # Negative numbers are not recognized
""
name2.slice(0,-1)  # slice is recommended later
"$$jason$"


var name3 = 'eGoNDsb123666HahA'
undefined
name3.toLowerCase()
"egondsb123666haha"
name3.toUpperCase()
"EGONDSB123666HAHA"
var name = 'tank|hecha|liaomei|mengsao|...'
undefined

name.split('|')
(5) ["tank", "hecha", "liaomei", "mengsao", "..."]
name.split('|',2)
(2) ["tank", "hecha"]0: "tank"1: "hecha"length: 2__proto__: Array(0)
name.split('|',10)  # The second parameter is not to limit the number of cut characters or to obtain the number of elements after cutting
(5) ["tank", "hecha", "liaomei", "mengsao", "..."]


name.concat(name1,name2)
"tank|hecha|liaomei|mengsao|...  egonDSB  $$jason$$"
var p = 1111
undefined
name.concat(p)  # js is a weak type (it will be automatically converted into the same data type for operation)
"tank|hecha|liaomei|mengsao|...1111"

3Array

3-1 creating arrays

  • Use direct quantity []
  • Constructor method new Array()

3-2 array features

  • Indexes must be continuous
  • If the indexes are not continuous, a sparse array will be generated

3-3 traversal of array (iteration)

  • for loop traversal
  • for... in loop
  • for... of loop

34 addition and deletion of array elements

add to

  • Assign a value to the new index
  • Using the length of the array, insert a new element at the end of the array
  • push()
  • unshift()
  • splice()

delete

  • Change array length
  • pop()
  • shift()
  • splice()
  • Operator delete

3-5 array object properties

  • Length the number of elements in the length array

3-6 array object method

  • splice()

Delete the specified number of elements at the specified position, replace the specified number of elements at the specified position, add the elements at the specified position, and return the array composed of deleted elements

  • reverse() flip array
  • sort() array sort
  • push() and pop() add or remove elements at the end of the array
  • unshift() and shift() add or remove elements at the front of the array
  • toString() and toLocalString() convert arrays to strings
  • join() concatenates the elements of the array into a string
  • slice() intercepts part of the array and returns a new array slice(start, end)
  • concat() merges multiple arrays
  • indexOf() searches for an element in an array and returns its location.
  • lastIndexOf() returns the last position of a specified string value, and searches from back to front at the specified position in a string.
  • forEach() traverses the loop
  • map() processes each element of the array through the specified function and returns the processed array.
  • filter() detects numeric elements and returns an array of all elements that meet the criteria.
  • every() checks whether each element of a numeric element meets the criteria.
  • some() detects whether any element in the array element meets the specified conditions.
  • reduce() combines the index values of array elements from low to high reduceRight() combines the index values of array elements from high to low
methodexplain
.lengthSize of array
.push(ele)Tail append element
.pop()Gets the element of the tail
.unshift(ele)Insert element in header
.shift()Remove element from header
.slice(start, end)section
.reverse()reversal
.join(seq)Concatenate array elements into strings
.concat(val, ...)Join array
.sort()sort
.forEach()Pass each element of the array to the callback function
.splice()Delete the element and add a new element to the array.
.map()Returns a new array of values processed by the array element calling the function

4,Math

4-1 properties

  • PI returns the PI (approximately equal to 3.14159).

4-2 method

  • abs(x) returns the absolute value of the number.
  • sqrt(x) returns the square root of a number.
  • pow(x,y) returns the y-power of X.
  • ceil(x) is rounded up to the logarithm.
  • floor(x) rounds down the logarithm.
  • round(x) rounds the number to the nearest integer.
  • max(x,y) returns the highest of X and y.
  • min(x,y) returns the lowest of X and y.
  • random() returns a random number between 0 and 1.

5,Date

5-1 method

  • getYear() please use getFullYear() method instead.
  • getFullYear() returns the year in four digits from the Date object.
  • getMonth() returns the month (0 ~ 11) from the Date object.
  • getDate() returns a day of the month (1 ~ 31) from the Date object.
  • getDay() returns a day of the week (0 ~ 6) from the Date object.
  • getHours() returns the hour (0 ~ 23) of the Date object.
  • getMinutes() returns the minutes (0 ~ 59) of the Date object.
  • getSeconds() returns the number of seconds (0 ~ 59) of the Date object.
  • getMilliseconds() returns the milliseconds (0 ~ 999) of the Date object.
  • getTime() returns the number of milliseconds since January 1, 1970.
  • getTimezoneOffset() returns the minute difference between local time and Greenwich mean time (GMT).
  • getUTC…. time zone
  • set...
  • setUTC...
  • toTimeString() converts the time part of the Date object into a string.
  • toDateString() converts the Date part of the Date object into a string.
  • toUTCString() converts the Date object into a string according to universal time.
  • toLocaleString() converts the Date object into a string according to the local time format.
  • toLocaleTimeString() converts the time part of the Date object into a string according to the local time format.
  • toLocaleDateString() converts the Date part of the Date object into a string according to the local time format.

6,RegExp

6-1 properties

  • Whether the global RegExp object has a flag g.
  • Whether the ignoreCase RegExp object has flag i.
  • lastIndex is an integer indicating the character position to start the next match.
  • Whether the multiline RegExp object has a flag m.
  • Source the source text of the regular expression.

6-2 method

  • exec() retrieves the value specified in the string. Returns the value found and determines its location.
  • test() retrieves the value specified in the string. Returns true or false.

7,JSON

Method 7-1

  • JSON.parse() parses strings in json format
  • JSON.stringify() serializes an array of objects or raw values

8,Global

10-1 properties

  • NaN
  • InFinity

10-2 method

  • escape() Unicode encodes the string.
  • unescape() decodes the string encoded by escape().
  • encodeURI() encodes a string as a URI. Some symbols of "@",: + "; / &" do not have special meanings in other URLs
  • decodeURI() decodes an encoded URI.
  • encodeURIComponent() encodes a string as a URI component
  • decodeURIComponent() decodes an encoded URI component.
  • eval() evaluates the JavaScript string and executes it as script code.
  • Isfinish() checks whether a value is a finite number.
  • isNaN() checks whether a value is a number.
  • parseInt() parses a string and returns an integer.
  • parseFloat() parses a string and returns a floating-point number.
  • Number() converts the value of an object to a number.
  • String() converts the value of an object to a string.
  • All built-in constructors are properties of global objects

9 function object

9-1 custom objects

# You can regard it as our dictionary in python, but the custom object in js is more convenient to operate than the dictionary in python

# Create custom object {}
"""The first way to create custom objects"""
var d1 = {'name':'jason','age':18}


var d = {'name':'jason','age':18}
typeof d
"object"

d['name']
"jason"
d.name  # It is more convenient than python to get values from the dictionary
"jason"
d.age
18

for(let i in d){
  console.log(i,d[i])
}  # The for loop is exposed to the outside world, and the key can be obtained directly


"""The second way to create custom objects requires keywords new"""
var d2 = new Object()  # {}

d2.name = 'jason'
{name: "jason"}

d2['age'] = 18
{name: "jason", age: 18}

9-2 Date object

let d3 = new Date()
Fri May 15 2020 14:41:06 GMT+0800 (China standard time)
   
d3.toLocaleString()
"2020/5/15 2 pm:41:06"

# You can also enter the time manually
let d4 = new Date('2200/11/11 11:11:11')
d4.toLocaleString()

let d5 = new Date(1111,11,11,11,11,11)
d5.toLocaleString()  # Month from 0 to November
"1111/12/11 11 a.m:11:11"

# Time object specific method
let d6 = new Date();
d6.getDate()  Acquisition day
d6.getDay()		Get week
d6.getMonth()		Get month(0-11)
d6.getFullYear()		Get full year
d6.getHours()			Get hours
d6.getMinutes()		Get minutes
d6.getSeconds()		Get seconds
d6.getMilliseconds()  Get milliseconds
d6.getTime()					time stamp

9-3 JSON object

"""
stay python Serialization and deserialization in
	dumps 		serialize
	loads			Deserialization

stay js There is also serialization and deserialization in
	JSON.stringify()								dumps
	JSON.parse()										loads			
"""
let d7 = {'name':'jason','age':18}
let res666 = JSON.stringify(d7)
"{"name":"jason","age":18}"

JSON.parse(res666)
{name: "jason", age: 18}

9-4 RegExp object

"""
stay python If you need to use regular in, you need to use re modular
 stay js You need to create regular objects in
"""
# The first one is a little troublesome
let reg1 = new RegExp('^[a-zA-Z][a-zA-Z0-9]{5,11}')
# The second kind of personal recommendation
let reg2 = /^[a-zA-Z][a-zA-Z0-9]{5,11}/

# Matching content
reg1.test('egondsb')
reg2.test('egondsb')

# Title get all the letters s in the string
let sss = 'egondsb dsb dsb'
sss.match(/s/)  # Get one and stop
sss.match(/s/g)	# Global matching g means global pattern

sss.match(/s/)
["s", index: 5, input: "egondsb dsb dsb", groups: undefined]
sss.match(/s/g)
(3) ["s", "s", "s"]

# Make complaints about global matching mode
let reg3 = /^[a-zA-Z][a-zA-Z0-9]{5,11}/g
reg2.test('egondsb')

reg3.test('egondsb')  # The global schema has a lastIndex attribute
true
reg3.test('egondsb')
false
reg3.test('egondsb')
true
reg3.test('egondsb')
false

reg3.lastIndex
0
reg3.test('egondsb')
true
reg3.lastIndex
7

# Make complaints about two 
let reg4 = /^[a-zA-Z][a-zA-Z0-9]{5,11}/
reg4.test()

reg4.test()  # Nothing is passed. The default is undefined
true
reg4.test()
true

reg4.test(undefined)
true
let reg5 = /undefined/
undefined
reg5.test('jason')
false
reg5.test()
true

"""
Summarize what you are using js When writing regular, you must pay attention to the above problems
 Normally, you won't be relieved in the future
"""

9-5 Math object (understand)

abs(x)      Returns the absolute number of.
exp(x)      return e Index of.
floor(x)    The logarithm is rounded down.
log(x)      Returns the natural logarithm of a number (base = 0) e). 
max(x,y)    return x and y The highest value in.
min(x,y)    return x and y The lowest value in.
pow(x,y)    return x of y Power.
random()    Return 0 ~ 1 Random number between.
round(x)    Round the number to the nearest whole number.
sin(x)      Returns the sine of a number.
sqrt(x)     Returns the square root of a number.
tan(x)      Returns the tangent of the angle.

Keywords: Javascript Front-end

Added by cty on Thu, 10 Mar 2022 14:28:18 +0200