What does instance, class, and reference mean in JavaScript?

340 viewsOther

I’m searching online but I’m too… dumb to understand.

😭 TIA.

In: Other

5 Answers

Anonymous 0 Comments

This is more of a fundamental Object-Oriented Programming concept than a Javascript-specific thing.

A **class** is a data structure that represents sone sort of complex object. For example, let’s say we have the class “car”, which represents the idea of a car. What information is there to know about a car? Well, there’s the make, model, color, number of miles, license plate number, owner, and all sorts of other things.

An **instance** is a specific, concrete thing that follows the “blueprint” that the class defines. For example, let’s say I have a truck. Back to our blueprint, this particular car has the following info, so in our example it’s an **instance** of the **car** class:

Make: Ford

Model: Ranger

Color: White

Number of miles: 100,000

License Plate Number: 123ABC

Owner: My buddy John

A **reference** is closer to what it sounds like — a reference to something else. When we include information on an instance, it’s typically “by value” (like the number of miles), or “by reference” (like my buddy John, who himself is an instance of the Person class). Notice, when I’m describing the car instance, I’m *not* telling you my buddy John has brown hair, is 5’11, and has 2 kids. I’m telling you my Car instance is associated with this Person instance, but you can go look at the details of that Person instance if you want more info on it. That’s a reference.

I hope this helps! Feel free to ask for clarification on any point if you’d like. I don’t work with Javascript specifically, but I should still be able to answer some conceptual questions.

Anonymous 0 Comments

It means the same thing in every Object Orented Language

A class is a descriptor of an Object. So the code you write, all the data, the functions, that is the class.

A class cant used directly since its an abstract description of how you want objects to be made, so to use it, you have to instanciate it and make an instance

An instance is 1 copy of a class, it has its own copy of all the data

A reference is how the language keeps track of the instance. the instance exists somewhere in memory and the reference keeps track of where. If you have a reference to an instance, you can change the instance, and everyone else with a reference to that instance can access the changes.

If you want to change YOUR copy of an instance without changing everyone elses, you have to make a new instance and copy all the data to it (there is usually a copy constructor to do this)

Anonymous 0 Comments

The class is the blueprint, an instance is a specific building. A blueprint can be used to make multiple buildings.

“Reference” is related to calling functions. When you call a function and you pass in arguments, it’s important to know which arguments can be changed by the function. For example, if you call a `sort` function and give it an array, it doesn’t get a *copy* of the array. That would be inefficient. Instead, it gets a *reference* to the array, and it can actually do stuff to it. So that `sort` function can modify the array itself and not return anything.

In JavaScript, all objects are passed by reference. Otherwise, when you pass things like numbers or booleans, the function can’t change their values because it really gets a copy.

Anonymous 0 Comments

A class is a category of things, such as plastic red square, or greet metal circle.

An instance is an actual single plastic red square that you made. Just having the words “plastic red square” doesn’t mean you actually have one in hand, you have to make one.

A reference is when you give something you made a name, so you can have you program do things with it.
“Make a plastic red square and named it Steve.” “Now, call the ‘paint it black’ function on Steve.”

Anonymous 0 Comments

**Class**: A template for making an object. The class itself doesn’t do anything (except “static methods,” which are just functions that can be grouped with a class for organizational purposes).

**Instance**: An object created from a class using `new MyClass()`. Each instance is unique, and multiple references can be held to the same instance.

**Reference**: A variable that holds an instance as it’s value. Setting another variable to the same instance doesn’t copy it, and instead both variables point to the same instance. A real-life use case might be enemies in a game all referencing the same `player` instance, so that as the player’s position changes, all the enemies are using the most up-to-date position in their calculations.

**EX**:

// Create a class. This just acts as a template to create instances
class MyClass {
constructor(value){
this.val = value;
}
}

// Create two separate instances of the same class
const instance1 = new MyClass(2);
const instance2 = new MyClass(3);

console.log(instance1.val); // outputs: 2
console.log(instance2.val); // outputs: 3

// Create a reference to instance1
const myReference = instance1;

// Since ‘myReference’ and ‘instance1’ both point to the same instance, changing the value of one changes the other
myReference.val = 17;

console.log(instance1.val); // outputs: 17, which is the changed value