Javascripit - class

소바·2022년 8월 17일
0

Let's make a class in JS

when you create a class, you always capitalized the name of the class in this case, cookie.



Classes always have something that is called a constructor(생성자).
in this case, we're going to pass this a color, and that's what this is referencing.
we're going to pass it to the particular cookie that we are creating.
And that right there is a valid class, it doesn't do much, but it is a valid class.


Now that we can create new cookies. we set it equal to a new cookie.
In this case, we can describe CookieOne is the specific instance of Cookie that we are creating. *instance ≒ example

And we do that by using the new keyword that calls the constructor.


And we are passing it a color green, and that is going to create a green cookie.

This one right here, that is called 'cookieOne' that's where that 'this' keyword applies because we can have multiple cookies that are created by this.
That this keyword is going to refer to a specific one.

so to expand on this, let's create another.
Let Cookie to equal the new cookie that is blue.
So Cookie, too, is a specific instance of cookie and This one is blue. That is this cookie Is blue


so now we have created a class which is like a cookie cutter, and then we've created a green cookie and a blue cookie.

Let's drop the cookies out of here and expand this class, so far we have created a constructor.
Now we are going to create get color and set color , and these are the two broad categories of methods that you create.
They are getters and setters.
So there is a simple class and the creation of our two cookies.

so if we say cookieOne.getColor() this is where that this key word comes in. We are getting the color for this(cookieOne) cookie. And you can see that this is green.

then we're going to do setColor for CookieOne and we're going to change the color of cookieOne to yellow.
this is roughly(approximately) how you can use classes.

let's flip back over and we have changed cookie one to yellow.

how were we going to use this in this?

In fact, all of our data structures are going to use classes.

Let's say we're going to build a linked list class.
So as we discussed, we're going to have to have a constructor.
So we're going to pass this a value to create the first node in the linked list and the curly braces I'm just putting a {...} to show that there would be code here to do whatever in this case the constructor would need to do.
So some of the other methods that we could create for a linked list would be push.




We could say let my linked list equal a NewLink list will pass it a number 23, and now we have a node with 23.
We can say my link lists push with the number 7, we could do that.
On shift with the number3 and do that.
Insert at the index of one, the number 11.
We can pop an item.

these are some of the things that we can do by building out the linked list class, and we have all of this ability to do all of these very specific things with these data structures by using classes.

profile
소바보이

0개의 댓글