js函数相关

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 函数声明

function f() {
console.log("f")
}

f()

// 不定长函数参数
function f1(...args) {
console.log(args)
}

f1(1, 2, 3)


// 函数返回值
function f2() {

}

console.log(f2()) // 返回undefined




function test01() {
return 100
}

function test02() {
return 200
}

function bar() {
// return [100, 200, 300]
// return {"name": "yuan", "age": 23}
return x = test01(), y = test02() + x++, z = ++y // 返回:最后一个运算结果

}

console.log(bar())



// 匿名函数
var f = function () {
console.log("匿名函数")
}


!function () {
console.log("立即执行函数")
}()



// 闭包函数
function outer() {
var a = 1
function inner() {
a++
console.log(a)
}
return inner
}

const o1 = outer()
const o2 = outer()

o1() //2
o1() //3

o2() //2 o2 有自己的闭包环境

全局污染

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
// js代码块
var n = 0

function counter() {
n++
console.log("n:", n)
return n
}

// console.log(counter())
// console.log(counter())
// console.log(counter())


</script>

<script>
// js代码块
var n = 10 // 将n 赋值为10

</script>
</head>
<body>

<button onclick="counter()">计数器</button>

</body>
</html>

闭包的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
// js代码块

// 构建子作用域

/* (function () {
console.log("执行...")
// 计数器功能
var n = 0
function counter() {
n++
console.log("n:", n)
return n
}
// counter()
// counter()
// counter()
})()*/

// 版本2
function get_counter() {
console.log("执行...")
// 计数器功能
var n = 0
var y =20
function counter() {
n++
console.log("n:", n)
return n

}
return counter

}

c1 = get_counter()
c2 = get_counter()

</script>

<script>
// js代码块
var n = 10

</script>
</head>
<body>

<button onclick="c1()">计数器</button>
<button onclick="c2()">计数器2</button>

</body>
</html>

js函数相关
https://tsy244.github.io/2025/06/04/js逆向/js函数相关/
Author
August Rosenberg
Posted on
June 4, 2025
Licensed under