-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1013-this-intro.html
101 lines (89 loc) · 2.75 KB
/
day1013-this-intro.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>this,instanceof,</title>
</head>
<body>
<script>
var a;
// new Object 法
function createRect1(w, h) {
var self = new Object();
self.width = w;
self.height = h;
self.getArea = function () {
return self.width * self.height;
};
return self;
}
a = createRect1(100, 100);
console.log(a.getArea());
// object 字面量创建法
function createRect2(w, h) {
return {
width: w,
height: h,
getArea: function () {
return this.width * this.height;
}
};
}
a = createRect2(100, 200);
console.log(a.getArea(), "a instanceof Object", a instanceof Object);
// 构造函数法
function Rect(w, h) {
this.width = w;
this.height = h;
this.getArea = function () {
return this.width * this.height;
};
}
a = new Rect(100, 300);
console.log(a.getArea(), "a instanceof Rect", a instanceof Rect);
// instance of 运算符
var CLASSES = [Number, String, Boolean, Array, Function, Object];
var VALUES = [null, NaN, 999, new Number(NaN), "", new String(""), false, new Boolean(), [], function () { }, {}, Object, Function, Number];
var VALUES_EXPR = ["null", "NaN", "999", "new Number(NaN)", "\"\"", "new String(\"\")", "false", "new Boolean()", "[]", "function () { }", "{}", "Object", "Function", "Number"];
var buildTableRow = function (value) {
var ans = {};
for (var klass of CLASSES) {
ans[String(klass)] = (value instanceof klass);
}
return ans;
};
var data = {};
for (var i = 0; i < VALUES.length; i++) {
data[VALUES_EXPR[i]] = buildTableRow(VALUES[i]);
}
console.table(data);
console.info("这说明除了 Function 和 Object,字面量都不能 instanceof 得到 true,要用new Xxxx() 才能得到 Number 的 instanceof 得 true");
console.info("并且函数同时能 true Function 和 Object");
function padArray(target, padding, len = 7) {
return target.concat(padding.slice(0, len - target.length));
}
function f() {
console.log(this.x);
}
console.info("直接调用得undefined");
f();
(function () {
this.x = "执行上下文里面的this.x 订正:此处 this 指向了 window 这其实是一个全局变量了 跟执行上下文没啥关系";
f();
})();
({
x: "Object 里面的属性方法",
f: f
}).f();
function F() {
this.x = "构造函数中的this.x";
this.f = f;
}
(new F()).f();
var fa = new F();
fa.x = "F的一个实例的x";
fa.f();
</script>
</body>
</html>