-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructs.js
More file actions
61 lines (48 loc) · 1.71 KB
/
structs.js
File metadata and controls
61 lines (48 loc) · 1.71 KB
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
//https://dev.to/joelbonetr/structs-in-javascript-1p9l
//https://thewebdev.info/2022/05/01/how-to-create-structs-in-javascript/
//tl;dr : no structs in javascript. can create literal objects or a function that creates objects
// you're probably better off using functionless classes in lieu of structs
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
const obj1 = { id: 1, speaker: "john", country: "au" };
const obj2 = {
id: 2,
speaker: "mike",
country: "nz"
};
console.log(obj1.id, obj1.name, obj1.country)
console.log(obj2.id, obj2.name, obj2.country)
// /**
// * @constructor Generates a constructor for a given data structure
// * @param {string} keys separated by a comma + whitespace. struct('id, name, age')
// * @returns {constructor} Constructor for the new struct
// */
function makeStruct(names) {
let names = names.split(', ');
function constructor() {
for (let i = 0; i < names.length; i++) {
this[names[i]] = arguments[i];
}
}
return constructor;
}
//** @type {ObjectConstructor|any} */
const User2 = makeStruct('id, name, country')
//** @type {User} */
const obj3 = new User2('3 Steve UK')
console.log(obj3.id, obj3.name, obj3.country)
// you're probably better off using functionless classes in lieu of structs
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
class User3 {
constructor(id, name, country) {
this.id = id;
this.name = name;
this.country = country;
}
}
const obj4 = new User3();
obj4.id = 4
obj4.name = 'Tim'
obj4.country = 'US'
console.log(obj4.id, obj4.name, obj4.country)
const obj5 = new User3(5, 'Bob', 'NZ')
console.log(obj5.id, obj5.name, obj5.country)