年 | let year = date.getFullYear() |
月 | let mon = date.getMonth() |
日 | let day= date.getDate() |
小时 | let hour= date.getHours() |
分钟 | let min= date.getMinutes() |
秒 | let ss= date.getSeconds() |
星期几 | let week = date.getDay() |
毫秒 | let ms = date.getMilliseconds() |
获取时间戳 | let ts = date.getTime() |
1、date
2、字符串对象
使用String()
let str1 = new String("hello")
console.log(typeof (str1))
3、方法
连接字符串
let str1 = "nanhang"
let str2 = "jincheng1234"
和+的作用类似
let str3 = str1.concat(str2)
使用正则替换原来的内容
console.log(str3.replace(/a/g, "######"))
slice()
console.log(str3.slice(1, 4))
分割 split()
转大写
console.log(str3.toUpperCase())
转小写
console.log(str3.toLowerCase())
4、函数
函数:一段独立功能的代码的集合
格式:
function 函数名(参数){
代码
}
函数调用 函数名()
一个函数没有return的情况下,默认返回的是undefined
5、值传递和引用传递
引用传递
let a = 1
let b = 2
let arr = [1, 2, 3]
function change(a) {
a += 10
b += 20
a.push(100)
}
6、作用域
全局作用域、函数作用域
let a = 1
function fn() {
let num = 1
console.log(a)
}
fn()
console.log(num)
7、函数作为参数进行传递
function fn1() {
console.log("我是第一个函数")
}
function fn2(a) {
a()
console.log("我是函数2")
}
fn2(fn1)
8、默认参数
function area(r, PI = 3.14) {
return PI * r * r
}
console.log(area(1))
let a = [17, "aadd", 34, 1]
a.push(10)
alert(a)
9、argumen
function getSum() {
return a + b
console.log(arguments)
let sum = 0
for (let i in arguments) {
sum += arguments[i]
}
return sum
}
console.log(getSum(1, 2, 3, 4))
10、匿名函数、函数表达式
let a = function () {
console.log("666");
}
a()
11、箭头函数
function fn(x) {
return x + 5
}
let a = fn(4)
console.log(a)
let fn = () => alert("hello")
let fn = x => x + 5
console.log(fn(4))
let fn1 = (x, y) => {
let f = 6
return x + y + z
}
12、递归
递归:函数自身调用自身
// 9! =1!*2*3*4*5*6*7*8*9
function jC(n) {
if (n === 1) {
return 1
} else {
return jC(n - 1) * n
}
}