objects
# Node.js
const obj = {
someProperties: {
foo: "bar",
},
someMethod: (prop) => {
return obj.someProperties[prop];
},
};
let item = obj.someProperties["foo"];
console.log(item);
item = obj.someMethod("foo");
console.log(item);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
Output
bar
bar
1
2
2
# Go
package main
import "fmt"
type Obj struct {
SomeProperties map[string]string
}
func NewObj() *Obj {
return &Obj{
SomeProperties: map[string]string{
"foo": "bar",
},
}
}
func (o *Obj) SomeMethod(prop string) string {
return o.SomeProperties[prop]
}
func main() {
obj := NewObj()
item := obj.SomeProperties["foo"]
fmt.Println(item)
item = obj.SomeMethod("foo")
fmt.Println(item)
}
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
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
Output
bar
bar
1
2
2
编辑 (opens new window)
上次更新: 2022/09/30, 11:34:22