Placeholder Image

字幕表 動画を再生する

  • has it going.

  • Guys, my name is dumb.

  • And today I want to talk about classes in Javascript.

  • So, basically, for those of you who haven't ever used classes before, a class is used to create one normal objects.

  • So you can think of an object as being a creation from a class definition.

  • So these classes are actually used directly by your program logic, but they used to create objects.

  • Now, these objects are the ones that actually used by your program to achieve different things.

  • So with that being said, you can define two main areas.

  • With that class definition, you can define what an object we'll have.

  • So this is called an instance property and also what they do.

  • This is called an instance method.

  • So with instance, properties taking into basically example off a human being a human might have a name and age and a hot so and instance, property describes the current state's off that object what they have.

  • It's basically just data about that property.

  • Sorry that objects.

  • So these instance properties are unique to every object which is created from that class definition.

  • So we now have the instance methods.

  • This is what they do.

  • So on instance, method for human being might be to talk, to run and to jump.

  • So this refers to what they actually do.

  • What actions on their behavior.

  • So typically an instance Method will use the instance properties to achieve their results.

  • So if those two men areas of rock there, So let's not create a class in Java script.

  • So Dan here we can create a class definition by using the class key.

  • Would let's talk about class luck that followed by the name off your class.

  • Let's create a class for a rectangle.

  • Let's talk about rectangle right there now, typically, but convention.

  • You want to use a capital letter at the beginning of your class name, So it's gonna be rectangle with a capital R, just like that, followed by to curly braces.

  • So now inside here this is your class definition, so every class also has what's called a constructor.

  • Now the constructor is basically just a method which is ran only once during the life off the objects.

  • So this constructor is ran once and it's ran when the object is being created.

  • So let's define this constructor, but helping I constructed like that, followed by two parentheses and your normal curly braces.

  • So the constructor is used to set up your object.

  • Okay, so this behaves like a normal method or normal function, which means if our top concert log inside here and then just say rectangle, he's being created.

  • Okay, so now this method constructor is ran when the object is being created.

  • So we have a class.

  • Let's now create an object from that class, the dead.

  • Here, let's create a new variable.

  • And we call this one something like my rectangle number one, which is an equal to a new object based off this class.

  • The stop that new.

  • That's the That's the new Kay would.

  • I didn't say rectangle just like that and then to parentheses to actually call that constructor method.

  • So it falls to hover over this.

  • We see we get constructor just like that.

  • So this will call the constructor method.

  • So now if I was to save these html foe and then refresh the Web browser in the console window, we get the rectangle is being created.

  • If I was to, um, make two of these rectangles, so my rectangle number two down here.

  • Saved these and refresh your browser one more time.

  • We get two messages off creation.

  • Okay, so that is how the constructive method actually works.

  • Okay?

  • The constructor is used to actually opposite Was it used to sit up the actual object.

  • So back to these instance properties.

  • What you might commonly see is the instance properties being defined inside the constructor.

  • So we have a rectangle now, a rectangle might have a week, a hot and also a color.

  • So let's to find these three instance properties within the constructor so below here.

  • Best to find a instance.

  • Property.

  • We can do these spots hopping out these dot and then the actual property name, for example, weeks is equal to and then develop you.

  • Let's just say it would be five Finnell.

  • We could do the same thing for the other two properties was like this and then dot quite equal to let's just say something like three and the color.

  • Let's make this a blue rectangle.

  • So this key word right there, that is a key word which refers to the current object.

  • So when I say this, I'm talking about the objects which is being created by that class.

  • Okay, so we now have our three instance properties defined inside the constructor.

  • So now if I was to save these and refresh your browser and in the console, if I just refer to the my rectangle objects or the variable here, Okay.

  • And press enter, we can see we get three properties.

  • There is color, height and width, which described the current rectangle.

  • Okay, so these properties are unique to this rectangle instance.

  • All right, so in most cases there, you actually to find instance, properties when you construct the object.

  • But these are typically user defined.

  • So usually you were topping my rectangle and then say something like three and in five.

  • And also maybe the colors that will say blue like that.

  • Okay, it's that blue.

  • So you want to make this 35 and Blue actually turned out to be the instance properties for this created object, Okay, this allows you to make custom rectangles with whatever values that you actually pass in.

  • So to achieve this result, you actually want to make the constructor function, except parameters for arguments.

  • So let's talk about underscore.

  • Yeah, and then say hearts.

  • Actually, that's terrible to say and then cuts and in public.

  • So these are now three parameters which are passed him through the constructor function and then days will be assigned to the instance properties off this particular rectangle object.

  • So let's stop here this week with days actually variables brought here.

  • So now we're assigning the width off distraction Good to be with heart and color and so on.

  • Okay, let's just make this a five and a three song out running This line of code right here creates a rectangle with that with the width off five hot off three at a color off blue.

  • So these are all passed in and inserted right there.

  • So now if I was to save this one and refresh the brother, we get the same result this just log out the actual rectangle object.

  • We got this rectangle with the five part of three on a color off blue.

  • So with this scenario, we can actually create two objects here to reptile was once again for the second time.

  • Let's give different data.

  • Let's say 10 and then rids.

  • So now, using this class definition but defining the structure off this rectangle.

  • But giving it custom values based on the individual needs off this August Current state in the program so else to save these sundry fresh brother this time.

  • What about rectangle Number one we get with 53 and blue number two we get 10 5 and writ so we can see how the instance properties are unique to the actual object being created.

  • We have two squares here with different instance properties for their done.

  • So that is actual instance properties and the constructive function.

  • Now for the instance methods, we can define these under the constructor.

  • So to work out, I believe the area off a rectangle, he multiplied the width and the heart so we can actually create a method which will give us the area off this rectangle or the example being created.

  • So let's define a new instance method.

  • We can do this, but topping out the name off the method, Let's say get area.

  • So get there right there.

  • That is the name of our instance method followed by the two parental season right there and then, um, the brackets.

  • So inside here, let's actually just return the width and the heart off this rec time.

  • So let's say return.

  • Return this dot with multiplied by this dot hot.

  • Okay, so now we should get different return values from this method based on the, um, the rectangle being used.

  • So forms that done here, let's say console dot above and then this a rectangle one gets area.

  • Okay, so using dot get area, This will call the instance method.

  • So for this rectangle right here, we should get five times three for the 2nd 1 Rectangle number two get area will give us 10 times five.

  • Let's save these and re fresh to Brussels, we get 15 and 50.

  • Okay, so you can see how these methods are defined in the actual class definition and their generic to the actual class.

  • And then they're using the instance properties that are defined by the constructor to give you different results based on the object being used.

  • Okay, now, one more example of the actual method.

  • Let's make a method that will actually print out a description off the actual object.

  • Let's say description Okay.

  • Inside here.

  • Let's just say Consul, don't look and then just say something like, um, on a rectangle, all in the missus put the the witness heart will say this stuff with And then, um this is hot.

  • Then we'll say and I am and then the actual color.

  • So now, by calling this Prince description method on rectangle we get I'm a rectangle off with Tom's heart and I am than the color.

  • So now down here, let's call the prince description method on the morrow rectangle inopportune.

  • Okay dot print be screwed Those to save and refresh this one we get the expected result rectangle off 10 times.

  • Father, I am ret.

  • Okay, so just to recap, we have a class which is a definition basically a structure off the eventual object being created.

  • We have an instance property, which refers to data that is relevant to each individual object being created.

  • And you also have an instance method which uses the instance properties to achieve an action or some sort of result.

  • Um, based on what's again each individual object being created from that class.

  • What and that is the basics off classes in Java script.

  • I want to talk about getters and set us in JavaScript classes.

  • So basically getters and sitters I used to define methods on a class which are then used as if their property.

  • So essentially, they looked like properties, but they're actually methods off that.

  • Plus, So let's see, with an example, just gonna define a classier in JavaScript.

  • And we call this one a squid.

  • Okay, for the constructive, Let's just taking a single argument here forward with.

  • And that'll be the wit on the hot off the actual square.

  • So inside he And let's just say this week is equal to that with parameter and the same thing for the heart.

  • Okay, so now the square has a cool with and hot.

  • Okay, now we have this basic square, So to work out the area off the square, you will be the heart multiplied by the width.

  • Okay.

  • So we can actually define a getter to achieve this.

  • So down here, let's create a getter for the area.

  • We can do this part, starting out gets just like that, then a space and then specify the name of your get up.

  • And this will be the actual name that you talked about as if it's a property.

  • So let's just say get and they put down area followed by parentheses.

  • Okay?

  • then the normal brackets there for the actual method body, sir.

  • Now inside here.

  • Um, this is now basically a function.

  • We can put any logical want inside that we could do anything.

  • So let's just return returned this week multiplied by these dot and that would give us the area off these square.

  • So you can see how it put a return statement inside this block because it literally behaves as if it's a method in case.

  • And now, um, we can we contest this together?

  • We can create an instance off this square.

  • So it's just go down here and say, We'll say, Let's make a new variable and say we call this one square one.

  • They go to a new square with a wit and heart off something like 25.

  • Okay, so now I'm down here.

  • That's just a concept of love.

  • And then we're gonna call this area, get up, but topping at square one and in dots area, they can see how it essentially behaves like property.

  • Okay, No.

  • So there's no need to put any any parentheses here, because yet it literally is properly right.

  • If I was to say this one and then re first brother.

  • Okay, we get 625 as a result.

  • So, um, 25 times 25 would be 6 24 Let's make that.

  • Maybe for just so I get a small number of square up for width and height saving very fresh, and we get 16.

  • So you can see how we're calling this area.

  • Get up running this dysfunction body, but behaves like it's a property.

  • I guess that's the actual get out right there.

  • So now let's actually create a setup.

  • So a sit up behaves basically in the same way.

  • Um, you can assign new values to your instance properties with a property like syntax.

  • So you can actually do this.

  • You can say square one dot area equals and then something like 25 for example.

  • So let's create this center.

  • So this right here, this line right there will actually convert this 25 into a width and heart for these two properties.

  • Okay, So back inside here, let's just create a Sarah full area, so we'll say sense and then put area.

  • Look, that this one is gonna take a single argument, and that will be this value right there.

  • So let's say sit area in parentheses.

  • Let's put down area.

  • Okay, so it's not here.

  • We can now simply just find these square roots off this 25.

  • Um, and then we can assign that that result to the Witham hot off the script because obviously the square root off the area would be the width and hot off the actual square.

  • So let's say this stuck quick is equal to the square.

  • So we used the math and dark s Q r T method and passing here.

  • The area?

  • I guess so.

  • Now, if I was to pass in, 25 inside here would say they with is equal to the math dot square root off 25 which is actually gonna be five.

  • Okay, so there were six of us were we can do the same thing for the arts.

  • At least it is equal to me.

  • Just say this dot whipped for, I guess.

  • Consistency.

  • Okay, so, no, we can run this curd and that line right there will change the value off the width and heart for this for the square.

  • So let's just say everything looks so let's get into that rotten day and console.

  • Don't love Leon here.

  • Square one dot area.

  • Okay, that saved the century fresh.

  • The browser.

  • Okay, 24 of you, of course.

  • That's actually just council.

  • Don't look the week off the square and also the high.

  • Okay, let's try again.

  • Saving refresh.

  • Let me get five right there.

  • So we have converted the initial width and height off four into five.

  • Using this setup, which modified the within the heart based on the area that you pass it.

  • That is the basic spot there.

  • Others force on another example of hell.

  • You can basically treat these like a function.

  • So, um, with this getup, let's actually keep track of how many times we have requested this square to give us the area.

  • So every time I call this getter, I want to actually increment a number, which tells us how many times we've caught this.

  • Get out.

  • So up inside here, let's create a new instance room when we called this one number number off the quests for area.

  • Okay, people to zero.

  • So when it's constructed, we have no requests for the area inside.

  • They get up.

  • Let's just say the start.

  • None over Christopher area plus plus.

  • So now every time we request for the area, this will be implemented.

  • So, Dan here, let's just get rid of these.

  • Some that what they're and then let's just say Consul, don't look.

  • Square one dock area.

  • Okay?

  • And do it.

  • Let's just say four times.

  • Okay, So now if I was to then console dot log and say swear, one.

  • The number of requests for area this should now equal four because of requested it four times.

  • And that has been implemented four times.

  • Okay, so now, forced to save the sentry first breast one last time, you can see we get the area and also number four.

  • And that his town, the terms of actually requested for the area.

  • Okay, so that is how you can use get us and said is on classes in javascript.

  • I want to talk about static methods in Java, skipped classes.

  • So basically, a steady method is a method which you define on a class, but it is not actually part of the instance created object who wants to speak created.

  • So basically, aesthetic method does not require an instance off.

  • Um, the class to be created in order to be used.

  • So what aesthetic methods are what's called a helper method?

  • So they're like little utilities that are relevant to that class.

  • But not really.

  • You know that I have a an object band to them, so let's just see with an example, I'm going to find a new class.

  • Here we go with the standard square example.

  • So a class of off sweet And for the constructor, it's just taking a width.

  • And the width will be assigned as per usual to the width and height.

  • Instance.

  • Properties.

  • That's just a constructor, and we'll say, did stuff with physical to that with parameter.

  • And this heart is also that with perimeter.

  • So now we have a square class with a constructor said.

  • Now let's just create an instance.

  • Pop this squares here.

  • That's very new.

  • Variable here and call this one square one equal to a new square passing in a week of Let's just say thanks.

  • Okay, so now we have this disgrace.

  • It also just canceled out Lord B squared counsel, save this and re first brother in the console we get.

  • Obviously, it's we're with the heart of eight.

  • So now let's create a static method on this square class that's basically, um, gonna compare to square instances and then let us know if they are equal width and height.

  • So in here, let's define aesthetic method.

  • We can do this, but first topping out of the keyword study, followed by the name off the method.

  • So let's just say, uh uh, any equals equals Hey, somewhat that equals.

  • And then this method here will take in two parameters or two arguments.

  • Now, the 1st 1 being obviously a square on the 2nd 1 being another sweet, let's just say A and B.

  • So A and B are two different square instances, and the Eagles aesthetic method is going to check the Witten heart off these two squares and then give us the result if they are equal or not equal.

  • So that's just, um, this is a return.

  • Then use the turn ery operetta.

  • Let's just say on return if I don't we, um, times a height.

  • He's equal to being done with times, baby duck hot, then returned True.

  • Actually, it's just this.

  • So now we're gonna overturn the result off this expressionless comparison here.

  • If the area off the first weight equal to the area off the second square.

  • Obviously just return.

  • Return that comparison result.

  • So now this method should work.

  • Let's test this out.

  • I'm gonna create another square here.

  • Square two with a with a hot off.

  • Not we have two different areas for these two squares.

  • And then down here, I'm gonna console to look the return Billy off.

  • This equals a second with it.

  • That actually cool.

  • I study method.

  • You call that directly on the actual square class itself.

  • And not on, um, a square instance.

  • That's to stop your square here and then dot equals Okay.

  • And he ever gonna passing square number one on square number two?

  • Like I said now, close to save this and refresh the brass a window, The council, we get false because he clearly the area off a is different to the area off.

  • Be if I was to make the area the same taken birth eight.

  • Okay.

  • 64 here and 64 here for the area saving.

  • Refresh this one.

  • We get truth so we can see how we've used aesthetic method here.

  • That aesthetic method doesn't require an instance for it to work it does in the regard of the actual parameters.

  • But the actual method itself doesn't ever say we start this or destroyed area.

  • So there's never any, um, I guess, referrals to the actual this cable.

  • Okay, so there's no instance bonded to this aesthetic method.

  • Probably a better example of this would be maybe a static method that will test whether or not, um, I, with a hot is a valid square.

  • So for a square to be valid, it needs to have an equal with an unequal hot.

  • Okay, so this is creating a new aesthetic method, Anya, and call this one something work is valid.

  • Maybe he's valid dimensions.

  • Okay, this takes in a week and a hot.

  • So now this aesthetic method is technically relevant to the square class.

  • So we're checking effects Square is valid.

  • Um, four dimensions is developed based on the width and hot.

  • So if the width and heart are the same, then it could be available square.

  • Let's just that's his return on the result off that condition.

  • If the whip he speaks to the huts, then it is a valid squares.

  • Let's just return with it too hot.

  • That comparison result.

  • So now down here again, fellas, Just console Dholakia Council got Look and then say once again, calling the square class directly and say he's developed dimensions and passing in something like six and six inside there.

  • Supposed to save the Sunday first Brass that we actually get true.

  • Okay, because obviously six equal to six.

  • So we get the return of true here.

  • Supposed to make this seven and six.

  • Well, we should get false in the council.

  • Refresh me, Get false.

  • Okay, So aesthetic methods I used for, I guess Helper Helper functions so they're still relevant to the actual class, but they don't actually require an instance off that class to work.

  • In fact, if I was to just get rid of these two guys right here, Okay?

  • Save every fresh.

  • We still get the same result.

  • There's no air or anything, so you can see how it's sort of like a help of function.

  • And that's how you can use aesthetic methods in classes in job script.

  • I want to talk about inheritance or the extends keyword in regards to javascript classes.

  • So basically Theo extends key would allow you to achieve the concept of inheritance in JavaScript classes.

  • So, um, you're basically gonna use this whenever you have a generic class and then you want to actually create a new child class off that generic one while still maintaining its features and behavior, but a little bit extra.

  • So basically, it's when you have a period class to create a child class.

  • Okay, so let's see how to actually do this.

  • So I'm gonna create a new class here, and this will be a representation of a person.

  • So let's talk about the class keyword and then just say, person just like that, this person would have.

  • Let's just say two properties will have to have a name and age.

  • So in the constructor that's taking a name and an age just like that.

  • And then a sign to instance, Properties will say this stop name equals name, and it's not a sequel to the age past.

  • I'm kidding.

  • This will be our parent class, the generic class.

  • Now, let's just say inside out code, we have a function.

  • Okay, Dysfunction.

  • Um, we call this one something like a developed software.

  • Dysfunction requires a bunch of programmers to develop to suffer, So this is taking an array off program is just like that.

  • Okay, it's idea, basically would just say developed stuff.

  • So each one of these programs are gonna develop some sort of suffering.

  • So as you can see, a normal person won't be able to actually be passed into this array because we expect program is not a generic person with no attributes on this school or occupation.

  • So we're actually going to create a child class which inherits from this person class, and these child class is gonna be a program so we can achieve this by creating a new class.

  • Okay, call this one program.

  • Okay.

  • Now, if we top your extends here, extends and then say, person, we've just achieved inheritance.

  • So now we have a program which extends a person, which means it takes in all the attributes and behavior off this person's name and age on that also, who have its own custom, um, behavior and probably is itself.

  • So, um, I just create a new method inside, the person will quick and just say something like Describe something that's describe.

  • This is just simply console.

  • Don't log.

  • Um, a message saying I am, and in their names.

  • So I am.

  • I don't And I am.

  • Hey, years old.

  • Okay, say this.

  • Start.

  • Name this program.

  • A class is gonna have access to not only these properties, but also access to the describe method.

  • Okay, So, Dan, here, let's create the constructor for these programs.

  • Top yet constructed here, it's worth taking a name and an age.

  • Okay.

  • The same thing is the actual parent class on.

  • And also, let's just say, uh, years of experience would say he is He's over experience, huh?

  • So now, inside you we actually have to call the parent class are sorry, the parent construct.

  • So we still have to actually assigned the name and age.

  • But we can do this in a more generic way about topping out super and then passing in name and h.

  • So hear this super is basically just going to call the constructor off the person.

  • That way, all this logic is still maintained in this class.

  • Okay, So super name age basically just caused this in the parent class.

  • And down here is the custom behavior off.

  • What's up?

  • I got off.

  • Um, the program Mr.

  • Saying this stuff years of experience is equal to that perimeter right there.

  • Okay, so now, before we define any method for this program, and let's just create some instances off these people sit down here.

  • Let's just, um, create a new person.

  • So a new generic persons lit will say person one ticket to a new person and passing a name off something like Jeff and an age of something like 45.

  • Okay, that's also create a program.

  • Will say, Let's program at one.

  • You could do a new programmer passing in our names.

  • Such as?

  • I don't myself Age of 56 years of experience.

  • About about 12.

  • Okay?

  • Not really.

  • That's right.

  • So we have these things to people.

  • Person.

  • We have one person here, another person.

  • We have two people, but the program is a special top of person.

  • Okay, so we can now consulate a look person one under the same thing for the program.

  • A number one knows to save this and re fish the web browser in the console.

  • Okay, friend, let's just see what's happening here.

  • Yeah, Cool.

  • Put that perfect.

  • Saving refreshed.

  • This one.

  • No, we get a person name of Jeff.

  • Age 45.

  • Programmer named, um 8 56 and eight years of experience.

  • 12.

  • So we can see how we have, obviously this the same behavior as the person with Adam.

  • Sorry.

  • Without, with extra added functionality off 12 years of experience.

  • The prototype for this programmer is person, and you can see how it all works with the With the product up train, any inheritance, they can see how this program is a child off this person class.

  • Okay, so let's add some extra functionality to these programs.

  • Dania, let's just say, um, developed.

  • Actually, let's call this method code.

  • I can make a new method here court code, and it would just say OK, concert love, I will say, Um uh, the star name.

  • The stark name is program always is coding.

  • Okay, so now obviously hear anything the program is have access to.

  • These methods have also say, um person, one dark coat.

  • It's not gonna work.

  • Let's say I'm refreshed.

  • This reef fish encore top gear up.

  • Not a function.

  • So a regular person cannot code.

  • If I change this to program on one this time we get the message.

  • Refresh me.

  • Get that right there.

  • You can see how we've added extra functionality to these programmer class while still maintaining the functionality off the person.

  • Let's just prove these right now by calling the describe admitted on the programmer.

  • I'm not the person that same refresh and we still get.

  • I'm dumping on 56 years old so you can see how we're maintaining the same behavior.

  • So back to this function, we might as well finish it off.

  • So this just create a new Arabia, Give it up all this stuff.

  • Let's make a new array Courtis One programs equal to Honore off programs So we'll say New program.

  • Say dumb.

  • We'd, uh, 56 years old and 12 years of experience.

  • What?

  • Um, let's just say Jeff once again.

  • Okay?

  • Age of Think it was 24 somewhat that anything he's got four years of experience.

  • Okay, mas will stop there.

  • That's one.

  • Okay, so now we can just obviously passing into this function here developed software on passing these programmers inside the function body.

  • Let's just loop through all these programs.

  • So it's a full Let's program up in programs, celebrities, and we'll say programmer duck.

  • Okay, so let me do what the program is and just calling Dick.

  • Hold method opposed to save these and refresh or he gets okay.

  • Cool.

  • That's fun.

  • Yeah.

  • Oh, I think it's all visited.

  • Gets off.

  • Okay, give me that.

  • Refreshed this and we get dumbest coding and Jeff is coming, all right.

  • And that's how you can achieve inheritance.

  • Using the extends keyword in JavaScript, I want to talk about the concept off polymorphism in Java skipped classes.

  • So basically, polymorphism is the act off redefining a method inside a derived child class off a parent.

  • So let's just see with an example, we'll go with the typical animal example.

  • So let's just define a new class inside here.

  • Call this one animal, which obviously represents a generic top of animal.

  • Okay, so for the actual constructor, we're just taking a single argument.

  • This will be just the name off that animal.

  • Okay?

  • And we can set that tone instance property.

  • So we can say this dot name is equal to the name that is passed in in the constructor that so now we have an animal with a name.

  • So let's just define a new method inside here.

  • And let's call this method make sounds all right.

  • It's all taking their arguments.

  • Now.

  • This method right here is the one that we're gonna eventually override to achieve polymorphous.

  • So let's just say for a generic animal, we're gonna consult Log.

  • I'm going to say generic animal sales.

  • Okay, so this is the make sound method that is present in the apparent class off animal, and it just says generic animal sound.

  • So he falls to make a new instance off this animal real quick down here.

  • This is to find a new constant and call this one a one equal to a new animal.

  • I call this animal, don't what?

  • And then if I say a one, don't make a sound just like that.

  • We should now see that in the console.

  • Let's just save this end, reef fish, the Web browser.

  • And we see that right there.

  • Okay, so that's fine.

  • Now, that's actually just create a new child class off this animal class and then override that makes sound method.

  • Okay, so let's go down you and declare a new class.

  • Let's call this class, doc.

  • Okay, So what dog is an animal?

  • So let's just say dog extends animal.

  • What?

  • Cool for the constructor.

  • Let's just take in once again the night and then called the super constructor passing in name.

  • So that means just called his constructor for the parents.

  • So we have this functionality right here.

  • Now let's just redefine that makes sound method.

  • And this is called polymorphism.

  • Okay, maxims This time, let's say, Consul, don't law, that's a wolf.

  • What?

  • Okay, so we're not done said now it felt to make a new animal down here or need Dawg Danielle near Constant.

  • Call this one a two people to a new dog.

  • Let's call this dog, Jim.

  • Now we're gonna call the make sound method on the constant, which is the top off, Doug.

  • Let's say a two don't make sense, right, fellows, to save this and very fresh this time we get goof off in the console.

  • You could see how we actually achieving polymorphism here by overriding the method inside the child class dog.

  • Um, you know, off that in them in the parent class, Mexican.

  • So we're just basically overriding it.

  • So false.

  • Thio comment that out in the child class and save and refresh, we get once again the generic sound.

  • So, um, what's happening here is it's actually checking, um, on the Doug class if it has that method.

  • If it doesn't, then it checks the parent class and it uses that one right there.

  • All right, so that's that's polymorphism.

  • I'll just show one more example this time calling the super inside the actual, um, overridden method so I can actually call the parent method inside the derived myth it.

  • So inside this Mexican method up and say super dot make sound And that what there is going to cool the parent class animal dot and make sounds So we'll actually see that message followed by that message.

  • And this is common practice.

  • You see how, actually using the generic behavior and then adding on special functionality for the special derived child class false to say this and refreshed the brother this time we see here generic and then wolf.

  • So these two right here are part of the dog dot makes our method co good, and that's how you can achieve polymorphism inside your job skipped classes.

  • I want to show you an example of how you can use the JavaScript classes to make a bonding to a HTML element.

  • So what I mean by this is we're actually gonna be able to create a class which can then manipulate the dumb or the document object model directly.

  • So, for example, here I have an unordinary list element on the A team l document with a few list items inside it.

  • Okay, in the job, scripts are connection.

  • Just talked about, at least and then at had an add an item to that least directly.

  • So, for example, least add a bottle, press enter, and we see bottle appears right there.

  • We could do the same thing for removing an honor that side least opera blue and passing an index.

  • For example, index number one press enter and is is removed.

  • Okay, so we're gonna make this brought here.

  • So inside the clean document here we have a simple structure.

  • So first, let's just create the actual anon ordered list item to you.

  • So let's say you l with an I d off something like my list.

  • So this list will be filled by a job Scripts.

  • Let's just say we'll put it.

  • Calm it down.

  • Say Judy feels by job.

  • Okay, so now we need to actually reference this list arm this list element in jobs skip.

  • So using the actual i d.

  • Here, we're gonna make a new constant Which references of this unfolded least element.

  • So down here, let's make a new constant and pour this one something like my least equal to document dot Get element by D passing in my list right there.

  • So now ma least mains that element Rot there.

  • Okay, so now let's start by.

  • Well, let's let's start creating the actual class.

  • So let's make a new external file here, make a new fall.

  • Let's cool these class least bonding dot Js.

  • Okay, let's now link this class in the actual eight female document.

  • Say skips that source of least finding duck Js.

  • Okay, so in this class Oh, let's just actually firstly defined this class was like class.

  • Call this one list bonding.

  • Okay, so in this class, we're actually gonna be able to passing the my least element to the class constructor.

  • Okay, so this class will actually keep a reference to the physical, um, tag or element guy.

  • So in the constructor, let's just take in a single argument.

  • Um, call this one, Alan.

  • Okay.

  • No, we can say this dot list element is equal to Alan.

  • Okay, so we have an instance.

  • An instance.

  • Property here called list element referring to the physical list.

  • HTML element.

  • Okay, so now we can make a underlying simple array that will be filled with all that text.

  • We can say this stock text least is eagle to an empty array.

  • So this Texas tear my contained some strings, for example, Decode.

  • He's I'm not bad.

  • Okay, So, back in the actual demo we have, we have that, right?

  • Yes, sir.

  • You can see how this array structure well, eventually actually contain all these texts items right here.

  • That's what the actual text this property is used for.

  • Okay, so now let's just make a instance method for this class.

  • Let's call this method update update so taken.

  • No, no arguments.

  • The update method is actually just going to update the physical dump.

  • So the physical list element with the items contained in text list so you may attend and a write off of strings and then the update methods actually gonna insert a ll these strings into the physical list element yet property, all this element.

  • So down here we can start by first clearing the the list element off least autumns.

  • So let's just say removed all existing ally elements.

  • Full staff tax.

  • Okay, we can say while the start list element dot first filed.

  • So why there is a first child?

  • Then we can say that the stock list element.

  • Really child parsing the first child, I said, This right here is going to remove all the existing AL I tags from the least element.

  • So let's just see an example of this in use so back inside the age to him out.

  • Let's just put some sample ally tags inside you.

  • It was a lot of number one on autumn and number two Okay, inside.

  • Yeah, let's make a new constant.

  • Like I called least binding.

  • This is equal to a new instance off that least bonding class who say knew least bonding.

  • Taking in.

  • Come on, at least.

  • Okay, so this is like a registering, um, this list element with this list bonding class.

  • Okay, let's save this and refresh your browser.

  • We get that about this.

  • So now let's call the update method on that list bonding and soon happens State responding update and they go away so you can see how, um, that occurred right here removes all the existing list item tax.

  • So now we have two ads.

  • Ads?

  • Um, let's just a fuel so feel, you know, it's less tag along.

  • Okay, let's just, um let's just fill up this list element with all these strings.

  • Inside this text list are a property.

  • So let's just say for each off the text list items for each parts text off this text list.

  • Okay, We're going to say the start list upend child, and we're gonna upend, obviously a L R.

  • Tech.

  • So how do we actually convert a string to L.

  • A Tech?

  • Let's just say that's That's just like least bonding, Doc creates least autumn and passing.

  • So we're gonna make aesthetic method called create least Autumn, which will generate an eight team l element for for the text that is past it.

  • All right, so let's just make this study class all this aesthetic method right now up here, let's say I study before this one create this autumn, which takes in a single argument being texts okay for the body.

  • Let's just make a new constant year called this one ally equal to it's documented tree Get element often Allied.

  • Attack.

  • Okay, I didn't say Ally takes content.

  • Let's put the actual text inside the action tag and then Ally.

  • So now this method actually makes makes and I lie.

  • Okay.

  • Element elements slash tag with text inside the actually passing right there.

  • All right, so, no, If we actually put some sample Donna inside this array, for example, decode and then out of the bottle.

  • Okay.

  • What saved isn't refreshed, brother.

  • We still get these two existing elements, right?

  • If we say least finding update, press enter, they get replaced.

  • So here we are, first clearing the existing elements.

  • So we clear him with that.

  • What?

  • Dick?

  • And then add ally tags for each text or eight string inside this text list.

  • Underlying array.

  • Right there.

  • Okay, let's make this back too.

  • Elements.

  • Okay, so now that's actually create the ad and remove methods we could first just get rid of those sample.

  • Um, text safe, please.

  • So let's make the ad and remove method.

  • So basically, um, these two methods right here simply just going to update the underlying array, that glass right there, and then call the update method, So it's going to be quite lean and what you call it loose coupling.

  • So down here, let's make a new ad method.

  • Uh, it's to take in some texts.

  • It was like ticks just like that inside here.

  • Let's just say this talk text list and call the push a method.

  • We're gonna push text to the erect.

  • So this will add this time next to this underlying array quite simple.

  • And then we're going to say this up there.

  • Uh, so basically, um, the ad method does it simple job adding the actual text to the array.

  • Then the update method does its job off updating the actual physical document object model, actually HTML page.

  • All right, let's just save this intestine saving.

  • Refresh your browser.

  • All right, let's say least bonding don't add.

  • All right, let's add decode to the list boom area.

  • That's a more full that dog.

  • So it works completely fine.

  • Good for the extra move method.

  • Um, for a move, this method right here, this all taking an index, so removing autumn at a particular index index there.

  • Okay, then once again, it'll fall a similar sort of structure.

  • It's gonna say these text list dots plus.

  • All right, that's an array method.

  • SPLA us at the index.

  • Okay.

  • And then one.

  • So this means remove an autumn starting at all multiple items, maybe starting at this index.

  • And then how many so removes an autumn at this index.

  • But only one of the highest actually removes one on the start up dick.

  • Okay, So similar thing.

  • Modify the existing underlying array.

  • And then, um, update the dumb right there.

  • Let's save this refreshing Rosa.

  • And to sit up.

  • That's add dog deacon and Mr ST Bottle.

  • Okay?

  • And then said this bonding don't remove at index one percenter.

  • Boom area.

  • All right, so that's the whole thing done about this recap what's actually happening?

  • All right, So what's happening?

  • Well, in this class, you actually started extra extra.

  • So in the extreme out, we're making an unwanted list element right there, which is then passed into the JavaScript through that method right there.

  • And then we're making a new instance off the least bonding class passing in the actual physical on ordered list element from bay in the class.

  • In the constructor, we are maintaining a reference to the actual list element property and then making an underlying array to store all these strings of text to bay display inside the update method, we are removing all the existing list autumn elements and then filling up the actual U L or the ol tags.

  • So this guy right here with the actual text inside the underlying text list array this is done by calling the study committed, create least bottom, which simply just Mexico least autumn element.

  • Uh, putting the actual text inside that fully admitted we are simply just adding, adding a an autumn to the actual array and then using the update method to actually update the dumb once that has been at it.

  • So where KP things lane, we're only actually updating what we need.

  • Inside the admitted sand goes for the room method was removing an autumn at that index and then once again passing over to the actual update method to do the actual dumb of usual work.

  • All right, And that's how you can make a bonding from a job skip class to an HT.

  • My element, entire your job skip classes.

has it going.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

B1 中級

JavaScriptクラスのチュートリアル (JavaScript Classes Tutorial)

  • 6 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語