课程: JavaScript Practice: Scope and Closures
Solution: Handle scope in a vacation class - JavaScript教程
课程: JavaScript Practice: Scope and Closures
Solution: Handle scope in a vacation class
- [Instructor] The first thing I need to do here is make some adjustments to our expedition class. This is an extension of the vacation class. So everything from this vacation is going to be inherited by the expedition. So all we need to do for this is we're going to define a survival rate. This is its own property so this is a property that the expedition has but the vacation does not. Then we're going to invoke the constructor method and this is going to help us to set up these properties. So if vacation has a destination as well as a length then we're going to reference that here in our class. So we'll say destination length and survival rate. This is going to reference from the super class, in other words from the vacation, the destination, and the length. So that's actually going to look like this. Super, destination and length. And then we'll also use this dot survival rate to scope the survival rate to the expedition. Cool, so once I've done that, I'm going to use the create expedition function to create a new instance of an expedition. So that's going to look like this. We have our details that will come in so that'll look like this. Our array of details about the expedition. To create this, we'll just say const exp, new expedition. We'll use the spread operator to push in any details about that expedition. And then we're specifically interested in the survival rate. So this is what that would look like. We'll finish that line with a semicolon and let's give that a test. Awesome, so it looks like our code has returned 0.5 and again, always appropriate to test this out, make some adjustments to the array and make sure that that third item is being returned over here in the test. So this is our base class, our vacation. We extend that to create an expedition. You could create all sorts of different types of vacations, a resort vacation, a city vacation, but this expedition anytime we create a new instance of it, we use that new keyword and then this will help us successfully get our test passing and return that survival rate.