Javascript Coding Challenge for Beginners Q4

Transcript

English (Auto-generated)

Hello everyone. Welcome to my javascript coding challenge course for beginners. Again. For today's challenge, we will work on javascript classes and constructors for those who don't know what classes are classes are basically templates for creating an object. While constructor is a special method that we use to initialize an object if you don't understand what I'm saying, that is okay because after this video everything will become more clear before we begin. I want you to write a pet class with a constructor that takes in the pets first name and last name, message parameter. We also want you to write a method called get pet name that returns the pets phony so pasta video and when you're ready you can resume the video and we'll work on the challenge together. Now that you're ready, let's write this together. So for those who don't remember a class always starts with a capital letter. So here weight class. Putt and inside we have a constructor that takes in the pets first name and last name. So let's write the constructor method. And inside we want to take in the pets first name and last night. But for those who don't know what this is, I will explain it in a bit. He also want you to write a method called dead pet name that returns the pets phoning. So here let's write that ending and we want to record the pest phony. Now we want to create a new pet class. So for j let's create a cat. So we have a new pet cat and it takes into arguments first name and last name. I think I'm gonna call my cat tutorial um, cookies. So for those who don't know the constructor parameters and the arguments have to be in the same order. So if you put cookies before oreo, what is going to return is cookie is gonna be the first name and the oral will become its last name. So make sure they're in the right order that you're passing through these arguments and my last console logs cat and see what it gives us. So as you can see cats is an object with a first name, oreo and last name cookies. This is what I mean by classes is a template to create a new object and the constructor inside initialize is this object which means it gives us the value first name, oreo and last name. Cookie. Because we're writing it in here now. Like other objects. How can we access maybe the cats first name. So like other objects we can use dot notation. So let's counselor cat dot first name. And we will counsel legit. So as you can see we got the name oreo similar to its method. We can also initialize it using dot notation. So let's try castle logging cat dot get that name. And if we run notes, we get oreo cookies. Now coming back to the topic of this, how does this know what it's returning back to us because this is referring to the object that is calling it and in this case our object is Cat. Because I've shown before, cat is an object with the first name and last name. So whenever there is this, it's referring to the object calling it. And in this case my cat object is calling, get pet name and this already knows that the first name is oreo and the last name is cookie. So I hope you guys have enjoyed this video and you have learned something about classes and constructors moving on. We will work on the next problem in a bit. So thank you all for watching and I hope you all have a good night. Mhm.
96 Views 0 Likes 0 Comments

Create a class and constructor for this javascript problem!

Comment
Leave a comment (supports markdown format)