Declaration of custom object or type in Node.js

In Node.js, when you want to assign one object to another that is declared by a custom type or class, you typically need to ensure compatibility between the source object's structure and the target class's expectations. Here's a breakdown:

Scenario 1: Assigning Plain Objects to Class Instances

If you have a custom class and want to assign properties from a plain object to an instance of that class, you can use one of the following methods:

Example:

class CustomType {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}

// Plain object
const plainObject = { name: "John", age: 30 };

// Assign using Object.assign()
const customInstance = new CustomType();
Object.assign(customInstance, plainObject);

console.log(customInstance instanceof CustomType); // true
console.log(customInstance); // CustomType { name: 'John', age: 30 }

Scenario 2: Using a Constructor

You can modify your class to accept an object in the constructor for direct assignment:

class CustomType {
  constructor({ name, age }) {
    this.name = name;
    this.age = age;
  }
}

// Plain object
const plainObject = { name: "John", age: 30 };

// Create an instance
const customInstance = new CustomType(plainObject);

console.log(customInstance instanceof CustomType); // true
console.log(customInstance); // CustomType { name: 'John', age: 30 }

Scenario 3: Validation or Transformation

If you need to validate or transform the input object before assigning, you can implement a custom method:

class CustomType {
  constructor() {
    this.name = "";
    this.age = 0;
  }

  assignFromObject(obj) {
    if (obj.name && typeof obj.name === "string") {
      this.name = obj.name;
    }
    if (obj.age && typeof obj.age === "number") {
      this.age = obj.age;
    }
  }
}

// Plain object
const plainObject = { name: "John", age: 30 };

// Assign using the custom method
const customInstance = new CustomType();
customInstance.assignFromObject(plainObject);

console.log(customInstance instanceof CustomType); // true
console.log(customInstance); // CustomType { name: 'John', age: 30 }

Scenario 4: Using Static Methods

A static method can return a new instance based on a plain object:

class CustomType {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  static fromObject(obj) {
    return new CustomType(obj.name, obj.age);
  }
}

// Plain object
const plainObject = { name: "John", age: 30 };

// Create instance using static method
const customInstance = CustomType.fromObject(plainObject);

console.log(customInstance instanceof CustomType); // true
console.log(customInstance); // CustomType { name: 'John', age: 30 }

Key Considerations

  • Validation: Ensure the source object has the expected properties and types.
  • Prototypes: Direct assignment (Object.assign) won't copy methods or prototype chain relationships.
  • Deep Copy: Use libraries like lodash if the object has nested structures that need cloning.

Choose the approach based on your specific needs for validation, transformation, and maintainability.

댓글

이 블로그의 인기 게시물

Fundamentals of English Grammar #1

Using the MinIO API via curl

Create topic on Kafka with partition count, 카프카 토픽 생성하기

In HBase, the "memory to disk" flush operation

Install and run an FTP server using Docker

To switch to a specific tag in a Git repository

Vespa vs Milvus

Scan an HBase table with a prefix filter

To download a file from MinIO using Spring Boot, 스프링부트 Minio 사용하기

kafka polling vs listen