Collections
Object
Object literal và method trong Node.js so với struct/class có method riêng trong Go, Rust, Swift và Java.
- Phiên bản tối thiểu
- Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.58Swift ≥ 5.1Java ≥ 25
- Đã chạy thử trên
- Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1
Code Node.js là ES module: lưu file .mjs hoặc đặt "type": "module" trong package.json.
Object literal trong Node.js gói cả dữ liệu lẫn method trong cùng một giá trị. Go tách hai việc: struct chỉ chứa dữ liệu, còn hành vi được gắn vào bằng hàm có receiver (thường là con trỏ tới struct đó) khai báo ở nơi khác. Trong 5 ngôn ngữ này, chỉ Rust tách y như Go: field nằm trong struct, còn method sống trong khối impl riêng. Swift thì khác — method viết ngay trong thân struct, cùng chỗ với property, giống cách Java gộp field và method trong thân class; điểm khác biệt của Swift so với Java chỉ là tự sinh constructor mặc định (memberwise initializer) nên không cần viết tay. Java gộp lại giống object literal của JS: một class chứa cả field lẫn method, chỉ khác là phải khai báo kiểu tường minh.
Object literal với property và 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
}Tham khảo: github.com/miguelmota/golang-for-nodejs-developers#objects