FAQ: Classes - Instance

This community-built FAQ covers the “Instance” exercise from the lesson “Classes”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Instance

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

11 posts were split to a new topic: Double quotation marks are not allowed in the Instance (Classes) exercise

I am brand new to programming and having a difficult time identifying the differences between Classes and Factory Functions. (Still trying to straighten out all the vocabulary, so I’m sure this doesn’t help either.)

In what instances (no pun intended) would you use Classes instead of Factory Functions?

2 Likes

JavaScript Factory Functions vs Constructor Functions vs Classes | by Eric Elliott | JavaScript Scene | Medium

Class vs Factory function: exploring the way forward

The first article covers the pro’s and con’s fairly well. Neither is exhaustive, so keep digging for more literature on both class and factory function.

3 Likes

why does

console.log(tt${surgeonCurry}
${surgeonDurant}tt)

tt = backtick, I don’t know the escape character for this forum.

print

[object Object]
[object Object]

EDIT: I tested console.log(surgeonCurry, surgeonDurant) and that works as expected, but console.log(surgeonCurry + surgeonDurant) does not

I want to print

Surgeon { name: 'Curry', department: 'Cardiovascular' } Surgeon { name: 'Durant', department: 'Orthopedics' }

but I want to put each Surgeon on their own line.

same question that i have in my mind :slight_smile:

What do we expect from this,

 console.log(surgeonCurry + surgeonDurant) 

knowing what we do about those two variables?

A post was split to a new topic: Covert insertion of Warriors propaganda

Can i used let instead of const to craete an instance? I thought i could, because both are the same thing but with let i can re-istance the variable. But apparently not?

If not why not?

Kind Regards.

You can use let, but for an instance of a class it’s not a very good idea. Say you created a robot class, and an instance of the class named ‘Bob’. With let you could accidentally overwrite your instance with any value. Consider:

class robot {
  constructor(name){
    this.name = name;
  }
  addProp(key, val){
    this[key] = val;
  }
}

let bob = new robot("Bob");
console.log(bob);
bob.addProp("height", 6.5)
console.log(bob);
bob.addProp("model", "K94V3");
console.log(bob);
bob = 11;
console.log(bob);

Output:

robot { name: ‘Bob’ }
robot { name: ‘Bob’, height: 6.5 }
robot { name: ‘Bob’, height: 6.5, model: ‘K94V3’ }
11

1 Like

Hi, I came back to the Class Chapter to try this:

Instead of having this object, use class to creat it:

myObjArrayUSERS = [{name:“Ana”, age:25}, {name:“Jhon”, age:30}, and 30 more…]

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

But, how do I put them back into a singles object array? .push didn’t work.

Never mind, I think this is the wrong usage of classes…

I’ll create a function instead… :slight_smile:

how to print all instances of class ? i tried to do console.log(Surgeon) and thought it will print both instances but it’s not.

2 Likes

What does the const halley=… refer to?Is it storing an object?And is the Dog after the new keyword referring to an object or the class or a constructor?
class Dog {
constructor(name) {
this.name = name;
this.behavior = 0;
}
}

const halley = new Dog(‘Halley’); // Create new Dog instance
console.log(halley.name);

halley is the variable to which the new instance is assigned. Declaring with const simply means it will persist for the entire session and cannot be changed or deleted.

No, it is not storing an object. It is only a reference to the location in memory where the object is stored. When the instance was created, it was stored by JS, and never moves from that location. As long as there is a reference to it, it stays alive. Take away the reference and JS queues it up for garbage collection (another topic for much later).

Dog is the name given to the class definition. The constructor is a method of the class that takes in our argument as its parameter and assigns it to the instance variable.

this.name = name

The only access we have to the properties of the object is through its instance name, halley.

this is the execution context of the object.

console.log(halley.name)

Hi mtf,

Why is “const halley” not written as “const dogHalley” similar to the surgeon exercise below, which states “const surgeonRomero”?

Do you always just reference the last name, such as “const surgeonRomero” when dealing with names? What happens if you have two people with the same last name?

I thought the first letter of class names were always capitalized, yet surgeonRomero is lower case, while “new Surgeon” is upper case? How do I know what classes to capitalize?

Thanks.

John

First thing to consider is that variable names are arbitrary. The only rules are, well, there is just one rule, really, they cannot begin with a number or any special character except _ (underscore). The unwritten rule is that we normally don’t begin variable names with a capital letter, that is any that refer to an object or object instance.

A class is more than just an object, it is a template or prototype object that is used to fashion other objects intended to have the same attributes (properties) and methods.

class Foo {
    // constructor
    // method
}

Notice there is no assignment? An instance declaration is an assignment:

foo = new Foo()

As for using variables to identify real people with the same name,

foo1 = new Foo();
foo2 = new Foo();

We essentially follow a convention which is just another way to say what people commonly conform to. Apart from writing names we can relate to now and in the future, we need also to consider anybody who is reading our code. Will our variable names make sense to them or will they get confused or put off and therefore not be able to see clearly the meaning and objective of our code.

In many instances our variable names help to document the code. In others they are just enough to fulfill their role:

const addAB = (a, b) => a + b;

The above utility function (sometimes called a ‘helper’) is clearly understandable even with the letter (symbol) names. We know that a and b are the placeholders for real numbers (or even string characters or strings) for what is either an addition operation (arithmetic) or a string operation (concatenation). Either way, do we need to expand on the variable names? Honestly, no.

Variable names have no bearing on how the code will perform. The computer only sees the value it stands in for, not the variable name.

a = 42

Above the computer sees ‘42’, and ignores ‘a’.

a = 6
b = 7
c = a * b

Again, the computer sees ‘42’, and nothing else. The only reason we give variables a name is so we can identify the value they represent and can refer back to that value at any time.

If this is all still a bit confusing, let us know how we can further clarify it.

Thanks mtf for the extensive write-up. This is helpful.

This is my first time reviewing JS. There is a lot of unique terminology to understand upfront. I’m sure it will get easier over time.

I find the Codeacademy lessons address how to code without sufficient discussion on the why and what (i.e. definitions) aspects, leaving me with many unanswered questions. I just purchased a JS book to fill in the details.

1 Like