What is an object model in JavaScript?

#javascript

#javascript object model

JavaScript's object model is based on prototypes, which means that objects can inherit properties and methods from other objects. Here's a breakdown of the key concepts and how they work together:

1. Objects

In JavaScript, everything is an object, except for primitive data types (like `string`, `number`, `boolean`, `null`, `undefined`, and `symbol`). Objects are collections of key-value pairs.

let person = {
    name: "John",
    age: 30,
    greet: function() {
        console.log("Hello, my name is " + this.name);
    }
};

console.log(person.name); // John
person.greet(); // Hello, my name is John

2. Prototypes

Every JavaScript object has a prototype. A prototype is also an object. All JavaScript objects inherit their properties and methods from their prototype.

let animal = {
    eats: true
};

let rabbit = {
    jumps: true
};

rabbit.__proto__ = animal; // setting the prototype of rabbit to animal

console.log(rabbit.eats); // true
console.log(rabbit.jumps); // true

In this example, `rabbit` inherits the `eats` property from `animal`.

3. Constructor Functions

Constructor functions are used to create multiple similar objects. They act like blueprints for creating objects.

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

Person.prototype.greet = function() {
    console.log("Hello, my name is " + this.name);
};

let john = new Person("John", 30);
let jane = new Person("Jane", 25);

john.greet(); // Hello, my name is John
jane.greet(); // Hello, my name is Jane

In this example, `Person` is a constructor function. The `new` keyword is used to create instances of `Person`, which inherit properties and methods from `Person.prototype`.

4. ES6 Classes

ES6 introduced a new syntax for creating objects and dealing with inheritance called class. It’s syntactic sugar over the existing prototype-based inheritance.

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

    greet() {
        console.log("Hello, my name is " + this.name);
    }
}

let john = new Person("John", 30);
let jane = new Person("Jane", 25);

john.greet(); // Hello, my name is John
jane.greet(); // Hello, my name is Jane

5. Inheritance with Classes

Classes can also inherit from other classes using the `extends` keyword.

class Animal {
    constructor(name) {
        this.name = name;
    }

    speak() {
        console.log(this.name + " makes a noise.");
    }
}

class Dog extends Animal {
    speak() {
        console.log(this.name + " barks.");
    }
}

let dog = new Dog("Rex");
dog.speak(); // Rex barks.

In this example, `Dog` inherits from `Animal` and overrides the `speak` method.

Summary

  • Objects: Collections of key-value pairs.
  • Prototypes: Objects from which other objects inherit properties.
  • Constructor Functions: Functions that create objects.
  • ES6 Classes: A modern, syntactic sugar over prototype-based inheritance.
  • Inheritance: Mechanism by which an object can inherit properties and methods from another object.

These concepts form the basis of JavaScript's object-oriented programming capabilities.

We help your company with all JavaScript requirements. Click here to contact us