text
stringlengths 1
81
| start
float64 0
10.1k
| duration
float64 0
24.9
|
---|---|---|
And what are we going to do? | 383.384 | 1.166 |
Well, we should attach to
the instance the array. | 384.55 | 6.01 |
And that's basically saying,
when I'm constructed, | 390.56 | 2.38 |
I expect to have one argument,
which is just an array. | 392.94 | 3 |
And the only thing I'm going to
do when creating this set object | 395.94 | 3.87 |
is just store a reference to that array. | 399.81 | 3.33 |
By doing this.arr = arr. | 403.14 | 1.68 |
And so in this case, the
this keyword is referring | 407.33 | 2.71 |
to the instance of the object. | 410.04 | 4.36 |
Cool, so let's try adding a couple
of different methods to this. | 414.4 | 3.44 |
So we should be able to support
add, which should take a value. | 417.84 | 5.88 |
We should be able to support
delete, which also takes a value. | 423.72 | 5.39 |
And we should have something called
has, which checks for inclusion. | 429.11 | 6.62 |
So how might we do something? | 435.73 | 3.2 |
How might we add to this class? | 438.93 | 1.594 |
Does anybody have any ideas? | 440.524 | 1.166 |
AUDIENCE: [INAUDIBLE] | 446.78 | 3.76 |
JORDAN HAYASHI: Exactly. | 450.54 | 1 |
So we should use something
like push to add to the array. | 451.54 | 3.6 |
But before we do that, we should make
sure that that number does not already | 455.14 | 4.35 |
exist. | 459.49 | 0.85 |
And so maybe we should implement the
has method first, which is a great idea. | 460.34 | 4.626 |
Let's go ahead and do that. | 464.966 | 1.124 |
So how might we do has? | 466.09 | 3.39 |
Well, it turns out on
the array prototype, | 469.48 | 2.19 |
we already have something
called includes, which tells us | 471.67 | 3.222 |
if an array includes a value,
so we can just do that. | 474.892 | 2.208 |
We can do return this.arr.includes(val). | 477.1 | 2.98 |
And so now that we have that,
how might we take care of add? | 486.23 | 2.68 |
We should say, oh, well, if this does
not have the value already, add to it. | 492.43 | 8.3 |
And so here I use this.has. | 506.08 | 3.15 |
So this here is referring
to the instance of the set, | 509.23 | 5.37 |
and so this.has is
referring to this method | 514.6 | 2.459 |
down here, on this particular instance. | 517.059 | 3.411 |
And then when I do this.arr, this still
refers to the instance of this set. | 520.47 | 5.78 |
And so we're just getting at
this array property that we have, | 526.25 | 3.53 |
and we're pushing that
value to that array. | 529.78 | 3.25 |
Cool. | 533.03 | 0.5 |
So how might we go about this delete? | 533.53 | 2.475 |
AUDIENCE: [INAUDIBLE] has value. | 540.718 | 2.119 |
JORDAN HAYASHI: Yeah, we can
check if we have the value, | 542.837 | 2.333 |
but it doesn't really
matter all that much. | 545.17 | 2.27 |
A quick and easy way would just be
doing this.arr = this.arr.filter | 547.44 | 6.691 |
and then we can just
filter by the values. | 554.131 | 1.749 |
So we could say, oh, we
want for every x in here, | 555.88 | 4.17 |
we want the x's that
don't equal this value. | 560.05 | 2.56 |
Cool. | 568.14 | 0.5 |
And so we can go ahead and
run this to see if it works. | 568.64 | 2.39 |
Oops. | 578.945 | 0.5 |
While I edit, I should flip these. | 585.88 | 1.77 |
And we go ahead. | 593.96 | 1.739 |
S should have five members and actually
has-- ooh, we forgot to influence size, | 595.699 | 3.291 |
actually. | 598.99 | 0.5 |
And so we took care
of all of the methods, | 601.9 | 2.19 |
but we didn't include the size. | 604.09 | 3.45 |
And so we should be able to
return the size of this set. | 607.54 | 3.84 |
How might we do that? | 611.38 | 1.05 |
Well, this is interesting. | 615.247 | 1.083 |
So we should be able to get at this
value by doing the instance.size. | 616.33 | 6.9 |
And JavaScript actually
has a convenient way. | 623.23 | 2.17 |
We can do get size,
which is saying, when | 625.4 | 8.26 |
somebody tries to get at the
value or the property.size, | 633.66 | 4.2 |
actually run this function. | 637.86 | 2.41 |
So this is just syntax
for that shortcut. | 640.27 | 4.94 |
So how might we implement size? | 645.21 | 1.81 |
AUDIENCE: [INAUDIBLE] | 650.713 | 2.397 |
JORDAN HAYASHI: Yeah, just
return this.arr.length. | 653.11 | 3.09 |
So now we can run this, and we
see I should have five members, | 661.13 | 5.589 |
and it actually has five. | 666.719 | 1.041 |
So that's good. | 667.76 | 1.41 |
S should contain 5. | 669.17 | 1.03 |
That's true. | 670.2 | 0.5 |
That works. | 670.7 | 1.06 |
S should contain six. | 671.76 | 0.95 |
It's true. | 672.71 | 0.93 |
S should have six members, and
actually has six, so we're good still. | 673.64 | 3.2 |
S should no longer contain six. | 676.84 | 1.54 |
That also returns true. | 678.38 | 1.53 |
And lastly, S should have five members,
and actually does indeed have five. | 679.91 | 5.34 |
So does anybody have any questions with
our implementation of set as a class? | 685.25 | 4.39 |
Great. | 693.47 | 0.5 |
So it turns out JavaScript
actually already has a set class, | 693.97 | 7.1 |
and it works exactly as we implemented. | 701.07 | 2.49 |
But say we actually wanted to use
the native implementation of set | 703.56 | 3.18 |
and actually add some stuff to it. | 706.74 | 2.91 |
So that's where we use these other
keywords, called extends and super. | 709.65 | 3.7 |
So extends is the
JavaScript way of saying, | 713.35 | 2.31 |
hey, I want to start with a base
class and actually add to it. | 715.66 | 2.66 |
Extend this class. | 718.32 | 1.83 |
And super, as we'll see in a second,
is when we're writing that class, | 720.15 | 3.016 |
so we can refer to the original
class using this keyword. | 723.166 | 2.374 |
And so in this example
called my set, we're | 730.49 | 2.27 |
going ahead and extending that set
with a bunch of different things. | 732.76 | 3.197 |
And so here you see constructor. | 735.957 | 1.333 |
It still takes an array. | 737.29 | 1.08 |
And the first thing that we do
is we invoke super on that array. | 738.37 | 3.87 |
So this is basically saying,
hey, we're extending a set. | 742.24 | 5.769 |
So when you do the constructor,
the first thing you should do | 748.009 | 2.541 |
is actually run the
original set's constructor. | 750.55 | 3.69 |
And then let's also keep
track of this.originalarray | 754.24 | 2.23 |