-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.js
45 lines (40 loc) · 1.12 KB
/
object.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
export function createScope() {
return {
names: [],
getObjectNames() {
return this.names;
},
getObject(name) {
return this[name];
},
defineObject(name, declarationType) {
this.names.push(name);
this[name] = {declarationType};
},
setObject(name, value, initialize=false) {
const object = this[name];
if (!object) {
throw new ReferenceError(name);
}
if (!initialize && object.declarationType == 'const') {
throw new TypeError('Assignment to const');
}
this[name].value = value;
}
};
}
export function createObject() {
return {
getProperty(name) {
return this[name];
}
};
}
export function initializeGlobalScope() {
const jkConsole = createObject();
jkConsole['log'] = (...args) => console.log(...args);
const globalScope = createScope();
globalScope.defineObject('console');
globalScope.setObject('console', jkConsole);
return globalScope;
}