-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_1_fields.js
84 lines (68 loc) · 2.07 KB
/
2_1_fields.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
75
76
77
78
79
80
81
82
83
84
"use strict";
lecture("2. Objects and Closures");
chapter("Fields");
section("Objects as associative arrays");
let point = new Object();
point["x"] = 1;
point["y"] = 2;
dumpObject("initial point", point);
example('point["x"]');
example('point["y"]');
example('point["x"] === point.x && point["y"] === point.y', "shorthand syntax");
println();
point["x"] = 10;
point["y"] = 20;
dumpObject("modified point", point);
println();
point = {"x": 100, z: 200};
dumpObject("point with default values", point);
println();
println("Undefined property: " + point.qqq + " === " + point['qqq']);
println();
const strangeObject = {
"hello world": "zzz",
1: "qqq"
};
strangeObject["1 2"] = false;
dumpObject("strangeObject", strangeObject);
example('strangeObject[1] === strangeObject["1"]', "keys are strings");
section("Properties testing");
example("'x' in point", " point has property 'x'");
example("'a' in point", " point has property 'a'");
section("Properties dumping");
println("Properties:");
for (const property in point) {
println(" property");
}
println("Property values:");
for (const property in point) {
println(" point['" + property + "'] = " + point[property]);
}
section("Inheritance");
point = {x: 10, y: 20};
let shiftedPoint = Object.create(point);
shiftedPoint.dx = 1;
shiftedPoint.dy = 2;
dumpObject("point", point);
dumpObject("shiftedPoint", shiftedPoint);
println();
println("point is prototype of shiftedPoint: " + (Object.getPrototypeOf(shiftedPoint) === point));
println();
shiftedPoint.dx = -1;
dumpObject("shiftedPoint with modified dx", shiftedPoint);
println();
shiftedPoint.x = 1;
dumpObject("shiftedPoint with modified x", shiftedPoint);
dumpObject("point remains intact", point);
println();
point.y = 1000;
dumpObject("point with modified y", point);
dumpObject("shiftedPoint with propagated y", shiftedPoint);
println();
point.x = 1000;
dumpObject("point with modified x", point);
dumpObject("shiftedPoint without propagated x", shiftedPoint);
println();
delete shiftedPoint.x;
dumpObject("shiftedPoint with deleted local x", shiftedPoint);
println();