1. Mathematical objects
Math objects provide basic mathematical functions and constants
Mathematical objects do not need to use the new operator
Attributes | illustrate |
LN10 | Returns the natural logarithm of 10 |
LN2 | Returns the natural logarithm of 2 |
LOG10E | Returns the base 10 logarithm of e |
LOG2E | Returns the base 2 logarithm of e |
PI | Returns pi, about 3.141592653... |
SQRT1_2 | Returns the square root of 0.5 |
SQRT2 | Returns the square root of 0.2 |
AND | Returns the natural constant E, approximately 2.718 |
method | illustrate |
abs(x) | Returns the absolute value of x |
cos(x)/acos(x) | Returns the cosine/arccosine of x |
sin(x)/asin(x) | Returns the sine/arcsine of x |
expectation(x) | Returns the arctangent of x |
ceil(x)/floor(x) | Round numbers up/down |
exp(x) | Returns the exponent of e |
log(x) | Returns the natural logarithm of a number (base e) |
max(x,y)/min(x,y) | Returns the maximum/minimum value in x and y |
pow(x,y) | Returns x raised to the y power |
random() | Returns a random number between 0-1. (0,1) |
round(x) | round numbers |
sqrt(x) | Returns the square root of a number |
Example:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
console.log(Math.max(1,20,3,50))//最大值输出结果为:50
console.log(Math.pow(2,3))//2的3次方
console.log(Math.random())
//随机0-10以内的任意数
//[0,1]*10 [0,11]
console.log(Math.floor(12.6))//小于这个数的最大整数
console.log(Math.ceil(12.3))//大于这个数的最小整数
console.log(Math.round(12.6))//把数进行四舍五入
console.log(Math.floor(Math.random()*11))
</script>
</body>
</html>
Dynamic clock implementation code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<!-- innerHTML:我们可以通过 document.getElementById(‘aa’).innerHTML
来获取id为aa的对象的内嵌内容;
也可以对某对象插入内容,如 document.getElementById(‘abc’).innerHTML=’
这是被插入的内容’;这样就能向id为abc的对象插入内容。-->
<span id="time"></span>
<script type="text/javascript">
setInterval(function() {
var date = new Date()
console.log(date)
var year = date.getFullYear() //获取年份
var month = date.getMonth() + 1 //0-11 获取月份
console.log(year, month)
var day = date.getDate() //获取日
console.log(day)
var hours = date.getHours() //获取小时
var minutes = date.getMinutes() //获取的是分钟
var seconds = date.getSeconds() //获取的是秒数
month = month > 9 ? month : "0" + month
day = day > 9 ? day : "0" + day
hours = hours > 9 ? hours : "0" + hours
minutes = minutes > 9 ? minutes : "0" + minutes
seconds = seconds > 9 ? seconds : "0" + seconds
var result = year + "年" + month + "月" + day + "日" + hours + ":" + minutes + ":" + seconds
document.getElementById("time").innerHTML=result
}, 1000)
</script>
</body>
</html>
2. Array objects
An array object is a series of ordered collections of values that can store any type of data. It is recommended to store only one type of data.
method | illustrate |
concat() | Concatenate two arrays into a new array |
join() | Concatenate all elements in an array into a string |
pop() | Remove last element of array |
push() | Add an element to the end of the array |
unshift() | Add an element to the beginning of the array |
reverse() | Reverse array elements, flashback |
shift() | Remove the first element of the array |
strong() | Get subarray |
splice() | Insert, delete or replace elements in an array |
sort() | Sort array |
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
//数组对象
var arr_1=new Array()
arr_1[0]="张三"
arr_1[1]="李四"
console.log(arr_1.length)
var arr_2=new Array(5)
arr_2[5]="移动5班"
console.log(arr_2.length)
var arr_3=["张三","李四","王五","赵六"]
for(var i=0;i<arr_3.length;i++){
console.log(arr_3[i])
}
for(var i in arr_3){
console.log(arr_3[i])
}
var str=""
for(var i=0;i<arr_3.length;i++){
str+=arr_3[i]+"*"
}
console.log(str)
var result=arr_3.join("*")//使用*号分隔符进行分割
console.log(result)
arr_3.push("张里")//在数组最后添加一个元素
for(var i in arr_3){
console.log(arr_3[i])
}
console.log("------------------------------")
var arr_4=["张三","李四","王五","赵六"]
arr_4.push("张里")//在数组最后添加一个元素
arr_4.unshift("张硕")
arr_4.pop()
arr_4.shift()
for(var i in arr_4){
console.log(arr_4[i])
}
var result_1=arr_4.splice(1,3)
console.log(result_1)
var result_2=arr_4.reverse()
console.log(result_2)
</script>
</body>
</html>
JavaScript does not support multidimensional arrays, but you can use, for example:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
//数组对象
var citys=new Array()
//citys["河南"]=["郑州","开封","焦作","商丘"]
citys[0]=["郑州","开封","焦作","商丘"]
//citys[0][0]
citys[1]=["武汉","天门","黄石","赤壁"]
for(var i=0;i<citys.length;i++){
for(var j=0;j<citys[i].length;j++){
console.log(citys[i] [j])
}
}
</script>
</body>
</html>
3. Regular expressions
regular expression |
|
Chinese characters | /^[\u4e00-\u9fa5]{2,4}$/ |
/^[0-9a-zA-Z_]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z_]+$/ | |
Telephone | /^(\d{3,4}-)?\d{7,8}$/ |
month | /^((0?[1,9])|1[0-2])$/ |
days | /^((0?[1-9])|(1|2)[0-9])|30|31)$/ |
Regular expressions are mainly used for form verification, such as mobile phone number, email address, ID card, etc.
Matcher | illustrate |
\b | Match word boundaries |
\d | Matches a single numeric character |
\f | Matches a single form feed character |
\n | Matches a single newline character |
\r | Matches a single carriage return |
\s | matches any whitespace character |
\t | Match a single tab character |
\in | Match a single vertical tab |
\In | Matches anything containing an underscore |
^、$ | Match the beginning and end of a string |
* | Match the previous subexpression 0-more times |
+ | Matches the preceding subexpression 1-multiple times |
? | Match the previous subexpression 0-1 times |
{n,m} | Match the previous subexpression n-m times |
. | Matches any character except "\n" |
(x|y) | match x or y |
[] | Matches any character contained in |
[^] | Matches any character not contained in |
[-] | Match any character within the specified range |
1. Before using regular expressions, you must first create a regular expression object. JavaScript provides two construction methods:
1. Use regular identifier strings: var reg=/pattern/[flags]
2. Use the built-in regular expression object: var reg=new RegExp("pattern",["flags"])
Pattern represents the regular expression pattern to be used, which is an expression composed of special characters or ordinary characters.
flags flag, course option, there are three types: g (full-text search), i (ignore case), m (multi-line search)
2. Method
compile() method: Compile the regular expression into an internal format to execute faster. Use the new regular expression to replace the old regular expression. It is mainly used to improve the performance of more complex and time-consuming processing. It is rarely used in general.
exec() method: uses a regular expression to search in a string and returns an array containing the results. Not only is it used to determine whether a given string matches, but it also returns detailed information about the matching result.
test() method: returns a bool value: it indicates whether the string being searched matches the given pattern. Used frequently, mainly for matching testing. Returns true if the match is successful, otherwise returns false.
Usage of test() method:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<input type="text" name="pwd" id="pwd" value="" />
<input type="button" name="checkpwd" id="checkpwd" value="验证" />
<script type="text/javascript">
//正则表达式
//密码长度至少为6位,且只能是数字、字母、下划线
document.getElementById("checkpwd").onclick=function(){
//实现密码的验证
//编写正则表达式
var reg=/^[0-9a-zA-Z_]{6,}$/
var pwd=document.getElementById("pwd").value
var isPwd=reg.test(pwd)
if(isPwd){
alert("密码格式正确")
}else{
alert("密码格式不正确")
}
}
</script>
</body>
</html>