普通函数 对象方法 构造函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>this 与 函数的关系</title>
</head>
<body></body>
<script>
function fn1() {
console.log(this);
}
fn1();
const fn2 = function () {
console.log(this);
};
fn2();
const obj = {
name: "对象",
fn3: function () {
console.log(this);
},
};
obj.fn3();
function Student(name, age) {
console.log(this);
}
new Student("钟馗", 200);
let arr = [1, 2, 3];
arr.forEach(function () {
console.log(this);
});
function fn4(fn) {
fn();
}
fn4(function () {
console.log(this);
});
</script>
</html>
借调模式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>this 与 函数的关系 - 借调关系</title>
</head>
<body></body>
<script>
const richWoman = {
name: "富婆",
getChild: function () {
console.log(this.name + "重金求子");
},
};
richWoman.getChild();
const ds = {
name: "屌丝",
};
richWoman.getChild.call(ds);
richWoman.getChild.apply(ds);
richWoman.getChild.bind(ds);
console.log(richWoman.getChild.bind(ds));
richWoman.getChild.bind(ds)();
const newGetChild = richWoman.getChild.bind(ds);
newGetChild();
newGetChild();
newGetChild();
newGetChild();
</script>
</html>
借调应用
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>借调 - 应用</title>
</head>
<body>
<script>
let obj = { 0: "安琪拉", 1: 18, 2: "女", 3: "扔个球", length: 4, other: "其他" };
Array.prototype.forEach.call(obj, function (item, index) {
console.log(item, index);
});
let arr = [];
for (let i = 0; i < obj.length; i++) {
arr.push(obj[i]);
}
arr.forEach(function (item, index) {
console.log(item, index);
});
let newArr = Array.prototype.slice.call(obj);
newArr.forEach(function (item, index) {
console.log(item, index);
});
let str =
"家用电器,手机,运营商,数码,电脑,办公,家居,家具,家装,厨具,男装,女装,童装,内衣,美妆,个护清洁,宠物,女鞋,箱包,钟表,珠宝,男鞋,运动,户外,房产,汽车,汽车用品,母婴,玩具乐器,食品,酒类,生鲜,特产,艺术,礼品鲜花,农资绿植,医药保健,计生情趣,图书,文娱,教育,电子书,机票,酒店,旅游,生活,理财,众筹,白条,保险,安装,维修,清洗,二手,工业品";
let str1 =
'[{"href":"https://jiadian.jd.com/","text":"家用电器"},{"href":"https://shouji.jd.com/","text":"手机"},{"href":"https://wt.jd.com/","text":"运营商"},{"href":"https://shuma.jd.com/","text":"数码"},{"href":"https://diannao.jd.com/","text":"电脑"},{"href":"https://bg.jd.com/","text":"办公"},{"href":"https://channel.jd.com/home.html","text":"家居"},{"href":"https://channel.jd.com/furniture.html","text":"家具"},{"href":"https://jzjc.jd.com/","text":"家装"},{"href":"https://channel.jd.com/kitchenware.html","text":"厨具"},{"href":"https://phat.jd.com/10-603.html","text":"男装"},{"href":"https://phat.jd.com/10-507.html","text":"女装"},{"href":"https://phat.jd.com/10-156.html","text":"童装"},{"href":"https://channel.jd.com/1315-1345.html","text":"内衣"},{"href":"https://beauty.jd.com/","text":"美妆"},{"href":"https://channel.jd.com/beauty.html","text":"个护清洁"},{"href":"https://channel.jd.com/pet.html","text":"宠物"},{"href":"https://phat.jd.com/10-184.html","text":"女鞋"},{"href":"https://phat.jd.com/10-183.html","text":"箱包"},{"href":"https://channel.jd.com/watch.html","text":"钟表"},{"href":"https://channel.jd.com/jewellery.html","text":"珠宝"},{"href":"https://phat.jd.com/10-185.html","text":"男鞋"},{"href":"https://phat.jd.com/10-109.html","text":"运动"},{"href":"https://phat.jd.com/10-272.html","text":"户外"},{"href":"https://xinfang.jd.com/","text":"房产"},{"href":"https://car.jd.com/","text":"汽车"},{"href":"https://che.jd.com/","text":"汽车用品"},{"href":"https://baby.jd.com/","text":"母婴"},{"href":"https://toy.jd.com/","text":"玩具乐器"},{"href":"https://food.jd.com/","text":"食品"},{"href":"https://jiu.jd.com/","text":"酒类"},{"href":"https://fresh.jd.com/","text":"生鲜"},{"href":"https://china.jd.com/","text":"特产"},{"href":"https://art.jd.com/","text":"艺术"},{"href":"https://channel.jd.com/1672-2599.html","text":"礼品鲜花"},{"href":"https://nong.jd.com/","text":"农资绿植"},{"href":"https://health.jd.com/","text":"医药保健"},{"href":"https://channel.jd.com/9192-9196.html","text":"计生情趣"},{"href":"https://book.jd.com/","text":"图书"},{"href":"https://mvd.jd.com/","text":"文娱"},{"href":"https://education.jd.com/","text":"教育"},{"href":"https://e.jd.com/ebook.html","text":"电子书"},{"href":"https://jipiao.jd.com/","text":"机票"},{"href":"https://hotel.jd.com/","text":"酒店"},{"href":"https://trip.jd.com/","text":"旅游"},{"href":"https://ish.jd.com/","text":"生活"},{"href":"https://licai.jd.com/","text":"理财"},{"href":"https://pro.jd.com/mall/active/4EYjXQ6xjnM9TgeSjuMRE8ACEk6q/index.html","text":"众筹"},{"href":"https://baitiao.jd.com/","text":"白条"},{"href":"https://bao.jd.com/","text":"保险"},{"href":"https://anzhuang.jd.com/","text":"安装"},{"href":"https://jdwx.jd.com/","text":"维修"},{"href":"https://cleanclean.jd.com/","text":"清洗"},{"href":"https://2.jd.com/","text":"二手"},{"href":"https://mro.jd.com/","text":"工业品"}]';
console.log(JSON.parse(str1));
</script>
</body>
</html>
借调 - 区别 - 传参区别
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>借调 - 区别 - 传参区别</title>
</head>
<body>
<script>
function add(a, b) {
console.log(a + b)
}
add.call(null, 1, 2)
add.bind(null, 3, 4)()
add.apply(null, [2, 3])
let obj = { 0: '安琪拉', 1: 18, 2: '女', 3: '扔个球', length: 4, other: '其他' }
Array.prototype.forEach.call(obj, function (item, index) {
console.log(item, index)
})
Array.prototype.forEach.apply(obj, [function (item, index) {
console.log(item, index)
}])
</script>
</body>
</html>