Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between an object and an object from a class in JavaScript?

I'm learning JavaScript and Java at work and in school. As far as I know, in Java, you always have a class and from that class you can create objects. Same works in JavaScript, BUT: In JavaScript it's possible to create objects without classes.

So my question is: what is the difference between those two kinds of objects in JavaScript?

class Test {
    say() {
        console.log("I'm a test.");
    }
}

let TestFromClass = new Test();

let TestFromObject = {
    say() {
        console.log("I'm also a test.");
    }
};


TestFromClass.say();    // Output: I'm a test.
TestFromObject.say();   // Output: I'm also a test.

Now I wanted to know if there is a difference in them, or if we actually need classes in JavaScript.

like image 628
Dop4miN Avatar asked Oct 09 '19 09:10

Dop4miN


People also ask

What is the difference between object and object in JavaScript?

Javascript is case sensitive "object" is essentially a variable that can hold anything. "Object" is an actual javascript type.

What is the difference between an object and a class?

Class vs ObjectA class is a blueprint for declaring and creating objects. An object is a class instance that allows programmers to use variables and methods from inside the class. Memory is not allocated to classes. Classes have no physical existence.

What is the difference between object and object?

Object". There is no difference whatsoever.

What is an object in JavaScript?

In JavaScript, an object is a standalone entity, with properties and type. Compare it with a cup, for example. A cup is an object, with properties. A cup has a color, a design, weight, a material it is made of, etc. The same way, JavaScript objects can have properties, which define their characteristics.


1 Answers

When you use new, you construct a new object whose internal prototype is the class's prototype. For example:

class Test {
    say() {
        console.log("I'm a test.");
    }
}

let TestFromClass = new Test();
console.log(Object.getPrototypeOf(TestFromClass) === Test.prototype);

This is useful when you need to create multiple objects. Usually, such objects are created because they'll have some sort of state associated with each individual object, usually in the form of properties on the object (eg, a person object might have a name and an age property).

But if you don't have any data associated with an instance (in your example, the TestFromClass object), there's not much point having an instance at all.

If you only want to collect named functions into a data structure, your TestFromObject makes much more sense than a class.

That said, you may want to have a class that has some functions associated with it (like say) which don't have anything to do with an instance of its data, while still being able to create an instance, perhaps with other methods on the prototype. This isn't that uncommon, and is done by making the non-instance-related functions static:

class Person {
  static canEat() {
    return ['apples', 'bananas', 'carrots'];
  }
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}
const p = new Person('Bob', 99);
console.log(p.name);
console.log(Person.canEat());
like image 187
CertainPerformance Avatar answered Sep 19 '22 01:09

CertainPerformance