ArticleZip > Javascript Type Of Custom Object

Javascript Type Of Custom Object

Custom objects in JavaScript are a powerful feature that can greatly enhance your code's organization and efficiency. By creating your own custom object types, you can encapsulate data and behavior specific to your application, making your code more modular and easier to manage.

To create a custom object type in JavaScript, you can use either the constructor function approach or the class syntax introduced in ES6. Let's walk through both methods.

### Constructor Function Approach:

Javascript

function CustomObject(name, age) {
  this.name = name;
  this.age = age;
}

// Creating instances of CustomObject
let obj1 = new CustomObject('Alice', 25);
let obj2 = new CustomObject('Bob', 30);

console.log(obj1.name); // Output: Alice
console.log(obj2.age);  // Output: 30

In this approach, we define the structure of our custom object using a constructor function. The `new` keyword is then used to create instances of the object. Each instance has its own set of properties that are defined within the constructor function.

### Class Syntax (ES6):

Javascript

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

// Creating instances of CustomObject
let obj1 = new CustomObject('Alice', 25);
let obj2 = new CustomObject('Bob', 30);

console.log(obj1.name); // Output: Alice
console.log(obj2.age);  // Output: 30

In the ES6 class syntax, creating custom object types becomes more concise and readable. The `class` keyword defines the structure of our object, and the `constructor` method initializes its properties.

### Adding Methods to Custom Objects:
To add methods to a custom object, you can simply attach functions to the object's prototype. This allows all instances of the object to share the same behavior. Here's an example:

Javascript

function CustomObject(name, age) {
  this.name = name;
  this.age = age;
}

CustomObject.prototype.greet = function() {
  return 'Hello, my name is ' + this.name;
}

let obj1 = new CustomObject('Alice', 25);
console.log(obj1.greet()); // Output: Hello, my name is Alice

### Inheritance with Custom Objects:
JavaScript supports prototypal inheritance, allowing custom objects to inherit properties and methods from other objects. This can be achieved by setting the prototype of a custom object to an instance of another object. Here's a simple example:

Javascript

function Animal(name) {
  this.name = name;
}

Animal.prototype.greet = function() {
  return 'Hello, I am ' + this.name;
}

function Dog(name, breed) {
  Animal.call(this, name);
  this.breed = breed;
}

Dog.prototype = Object.create(Animal.prototype);

let myDog = new Dog('Rex', 'Labrador');
console.log(myDog.greet()); // Output: Hello, I am Rex

By mastering the creation and manipulation of custom object types in JavaScript, you can write more organized and powerful code. Experiment with these techniques and unleash the full potential of custom objects in your projects. Happy coding!