Collections
Object
Node.js object literals and methods compared to structs/classes with separate methods in Go, Rust, Swift, and Java.
- Minimum versions
- Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.58Swift ≥ 5.1Java ≥ 25
- Verified on
- Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1
Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.
A Node.js object literal bundles data and methods into the same value. Go splits the two: a struct holds only data, and behavior is attached with functions that take a receiver (usually a pointer to that struct) declared elsewhere. Of these five languages, only Rust splits it the same way as Go: fields live in the struct, and methods live in a separate impl block. Swift is different — methods are written right inside the struct body, alongside the properties, much like how Java bundles fields and methods inside a class body; Swift's only real difference from Java is that it auto-generates a default constructor (a memberwise initializer), so there's nothing to hand-write. Java is closer to the JS object literal: a class bundles both fields and methods, just with explicit types.
An object literal with a property and a method
const obj = {
someProperties: {
foo: 'bar'
},
someMethod: (prop) => {
return obj.someProperties[prop]
}
}
let item = obj.someProperties['foo']
console.log(item) // bar
item = obj.someMethod('foo')
console.log(item) // barpackage 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) // bar
item = obj.SomeMethod("foo")
fmt.Println(item) // bar
}use std::collections::HashMap;
struct Obj {
some_properties: HashMap<String, String>,
}
impl Obj {
fn new() -> Self {
let mut some_properties = HashMap::new();
some_properties.insert("foo".to_string(), "bar".to_string());
Obj { some_properties }
}
fn some_method(&self, prop: &str) -> Option<&str> {
self.some_properties.get(prop).map(String::as_str)
}
}
fn main() {
let obj = Obj::new();
let item = obj.some_properties.get("foo");
println!("{item:?}"); // Some("bar")
let item = obj.some_method("foo");
println!("{item:?}"); // Some("bar")
}struct Obj {
var someProperties: [String: String]
func someMethod(_ prop: String) -> String? {
someProperties[prop]
}
}
let obj = Obj(someProperties: ["foo": "bar"])
var item = obj.someProperties["foo"]
print(item as Any) // Optional("bar")
item = obj.someMethod("foo")
print(item as Any) // Optional("bar")class Obj {
Map<String, String> someProperties;
Obj(Map<String, String> someProperties) {
this.someProperties = someProperties;
}
String someMethod(String prop) {
return someProperties.get(prop);
}
}
void main() {
Obj obj = new Obj(Map.of("foo", "bar"));
String item = obj.someProperties.get("foo");
IO.println(item); // bar
item = obj.someMethod("foo");
IO.println(item); // bar
}Reference: github.com/miguelmota/golang-for-nodejs-developers#objects