Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
萝茗 authored and 萝茗 committed Mar 13, 2024
1 parent 212370d commit 12047e8
Show file tree
Hide file tree
Showing 5 changed files with 1,616 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
21 changes: 21 additions & 0 deletions js-demo/01作用域.html
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>
31 changes: 31 additions & 0 deletions js-demo/02 闭包.html
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>
116 changes: 116 additions & 0 deletions js-demo/03 函数.html
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>
Loading

0 comments on commit 12047e8

Please sign in to comment.