-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
萝茗
authored and
萝茗
committed
Mar 13, 2024
1 parent
212370d
commit 12047e8
Showing
5 changed files
with
1,616 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<html> | ||
<head> | ||
<title>01作用域</title> | ||
</head> | ||
<body> | ||
<script> | ||
function a() { | ||
console.log(1); | ||
} | ||
(function () { | ||
var a = undefined; | ||
if (false) { | ||
a = function () { | ||
console.log(2); | ||
}; | ||
} | ||
a(); | ||
})(); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<html> | ||
<head> | ||
<title>02闭包</title> | ||
</head> | ||
<body> | ||
<script> | ||
// let i = 0; | ||
// function fn() { | ||
// i++; | ||
// console.log(i); | ||
// } | ||
// fn(); | ||
// fn(); | ||
// fn(); | ||
// fn(); | ||
|
||
function fn() { | ||
let i = 0; | ||
return () => { | ||
i++; | ||
console.log(i); | ||
}; | ||
} | ||
let fn0 = fn(); | ||
fn0(); // 1 | ||
fn0(); // 2 | ||
fn0(); // 3 | ||
fn0(); // 4 | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<html> | ||
<head> | ||
<title>03 函数</title> | ||
</head> | ||
<body> | ||
<script> | ||
// 动态参数:arguments | ||
function getSum() { | ||
var sum = 0; | ||
console.log("动态参数:", arguments); // 伪数组 [Arguments][1, 2, 3, 4, 5] | ||
for (var i = 0; i < arguments.length; i++) { | ||
sum += arguments[i]; | ||
} | ||
return sum; | ||
} | ||
console.log(getSum(1, 2, 3, 4, 5)); // 15 | ||
|
||
// 剩余参数 | ||
function getSum2(...args) { | ||
var sum = 0; | ||
console.log("剩余参数:", args); // [1, 2, 3, 4, 5] | ||
for (var i = 0; i < args.length; i++) { | ||
sum += args[i]; | ||
} | ||
return sum; | ||
} | ||
console.log(getSum2(1, 2, 3, 4, 5)); // 15 | ||
|
||
// 剩余参数 | ||
function getSum3(a, b, ...args) { | ||
var sum = 0; | ||
console.log("剩余参数:", args); // [3, 4, 5] | ||
for (var i = 0; i < args.length; i++) { | ||
sum += args[i]; | ||
} | ||
return sum; | ||
} | ||
console.log(getSum3(1, 2, 3, 4, 5)); // 12 | ||
|
||
// 箭头函数; | ||
const fn1 = () => { | ||
return 2; | ||
}; | ||
const fn2 = (a) => { | ||
return a; | ||
}; | ||
const fn5 = (d) => d; | ||
|
||
function getNum() { | ||
return (() => { | ||
console.log(arguments.length); | ||
})(); | ||
} | ||
console.log(getNum(5, 5, 5)); // 3 | ||
const fn6 = (username) => ({ | ||
name: username, | ||
}); | ||
console.log(fn6("小明")); | ||
|
||
// this | ||
console.log(this); // window | ||
function fn7() { | ||
console.log(this); | ||
} | ||
fn7(); // window | ||
|
||
const obj = { | ||
num: 11, | ||
fn: function () { | ||
console.log(this); // obj | ||
let num = 22; | ||
const obj2 = { | ||
num: 33, | ||
fn2: () => { | ||
console.log(this); // obj | ||
}, | ||
}; | ||
obj2.fn2(); | ||
}, | ||
}; | ||
obj.fn(); | ||
|
||
const obj1 = { | ||
num: 11, | ||
fn: () => { | ||
console.log(this); // window | ||
let num = 22; | ||
const obj2 = { | ||
num: 33, | ||
fn2: () => { | ||
console.log(this); // window | ||
}, | ||
}; | ||
obj2.fn2(); | ||
}, | ||
}; | ||
obj1.fn(); | ||
|
||
const obj3 = { | ||
num: 11, | ||
fn: () => { | ||
console.log(this); // window | ||
let num = 22; | ||
const obj2 = { | ||
num: 33, | ||
fn2: function () { | ||
console.log(this); // obj2 | ||
}, | ||
}; | ||
obj2.fn2(); | ||
}, | ||
}; | ||
obj3.fn(); | ||
</script> | ||
</body> | ||
</html> |
Oops, something went wrong.