Difference between a class and an object in JavaScript?


#1

I'm a bit confused... I googled it, but only found answers pertaining to Java.

Can someone please clarify?

Thanks!


#2

An object is an instance of a class. Everything in JavaScript is an instance of one constructor or another. They get their class definition from the constructor(s) in their prototype chain. All of them descend from Object() which can be found at the top.


#3

Since JavaScript is not class based, but prototypal, classes are inferential, not formally declared as they would be in Java and other class based languages. ES6 is going to change all this, but for our purposes we can ignore that.

Any function can be an constructor in JS. The way we invoke a function to make it into a constructor is with the new operator. Study the role of return in a constructor function, as well as the prototypal architecture of JS. Add to this scope and context and the picture will begin to clarify and your thinking will fall into place enough to become creative with the language.

Happy coding!


#4

This is the answer I find, but I'm kind of dense so I'll need it spelled out a bit more.

An object is an instance of a class.
So a class is a kind of an object but an object is not necessarily a class?

Everything in JavaScript is an instance of one constructor or another. They get their class definition from the constructor(s) in their prototype chain.
I'm sorry I don't follow - I thought objects were the same whether made with literal or constructor notation and it was just a choice of syntax.

All of them descend from Object() which can be found at the top.
I thought some exercises had me create a class with no parent object?

Still really confused


#5

A class is a defining construct. It has a set of associated properties and methods either directly written in the constructor, or indirectly in the constructor prototype. Object instances of the same constructor share the same class definition in that they have properties and methods in common, though the values are unique to each instance.

Whether we write an object in literal or constructor notation is of no matter. It still invokes the Object() constructor. This will start to make more sense once you get some practice.

A custom constructor can create instances of itself, but the instances are themselves still descended from Object. The prototype chain immediately above the instance has the custom constructor prototype, but above that is Object.prototype.


#6

Thank you for bearing with me. It's the terms that throw me. What exactly is a "defining construct"? Or can it be shown with some sample code? Please excuse me, I learn best by example since I don't yet know what the text-book terms mean.

I do grasp the prototype chain concept though, however I am still foggy on the diff between classes and objects.


#7

function Vehicle(type, speed){

}

This is the function definition of a Vehicle constructor that takes two formal parameters, a type and a speed. A call to this function will send values called arguments to define them.

Functions have a special variable that lives just above the function called this which defines the execution context of the function. When we instantiate an object, this is that object, and what will be assigned as the new instance. The following is the defining construct I referred to earlier:

function Vehicle(type, speed){
    this.type = type;
    this.speed = speed;
}

An instance of this constructor is said to be of the Vehicle class. Let's create a couple of objects in the Vehicle class:

var bicycle = new Vehicle('bicycle', 20);
var car = new Vehicle('car', 200);
console.log(bicycle);          //  { type: 'bicycle', speed: 20 }
console.log(car);              //  { type: 'car', speed: 200 }

#8

Thank you! I get it.


#9

:laughing:

I had the same problem I actually looked up the definition of "construct" in hopes to get a better grasp on what Roy was trying to explain. This is what I found:

an idea or theory containing various conceptual elements, typically one considered to be subjective and not based on empirical evidence.
"history is largely an ideological construct"

That did not help because there in nothing theoretical or conceptual about a class. A class is tangible not theoretical. :confused:


#10

In what way does an object created using a customized constructor invoke the Object() constructor?

Can you please explain this statement?


#11

To use an everyday example: dog is a class of which my dog Spot is an instance. Spot is unique but has the properties and methods of dogness. e.g Spot.woof()


#12

Up to now, a class in JS has never been a declared object, only a theoretical one. We referred to object instances as being of a class, but that is only the way they are described. The diction was fashioned to put it in those terms.


#13

    var obj = {};
    console.log(obj.constructor);     // [Function: Object]

#14

SERP: javascript a classless language

Plenty of jewels in that list.