Skip to content

Commit 8afb722

Browse files
committed
feat: add tutorial of reflect
1 parent 319274c commit 8afb722

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@
3030
<!-- <script src="javascript/destructor.js"></script> -->
3131
<!-- <script src="javascript/module.js"></script> -->
3232
<!-- <script src="src/babel.js"></script> -->
33+
<!-- <script src="javascript/reflect.js"></script> -->
3334
</body>
3435
</html>

javascript/reflect.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class FirstClass {
2+
constructor(firstField) {
3+
this.firstField = firstField;
4+
}
5+
6+
someFunc() {
7+
console.log(`Some text here: ${this.firstField}`);
8+
}
9+
}
10+
11+
const firstObject = new FirstClass("12345");
12+
console.log(firstObject.someFunc());
13+
14+
const secondObject = Reflect.construct(FirstClass, ["54321"]);
15+
console.log(secondObject.someFunc());
16+
17+
class SecondClass {
18+
secondField = "Another text here";
19+
}
20+
21+
const thirdObject = Reflect.construct(FirstClass, ["123"], SecondClass);
22+
console.log(thirdObject.__proto__ === FirstClass.prototype);
23+
console.log(thirdObject.__proto__ === SecondClass.prototype);
24+
25+
Reflect.apply(secondObject.someFunc, { firstField: "000"}, []);
26+
console.log(Reflect.ownKeys(secondObject));
27+
28+
Reflect.preventExtensions(secondObject);
29+
secondObject.newField = "Demo";
30+
console.log(Reflect.isExtensible(secondObject));
31+
console.log(secondObject);

0 commit comments

Comments
 (0)