< Return to Video

9.14: Arguments Array in JavaScript - p5.js Tutorial

  • 0:00 - 0:05
    Hello and welcome to another p5.js
    javascript video tutorial thingy mabob
  • 0:05 - 0:09
    thing that I'm making. In this tutorial I'm
    going to cover something called the
  • 0:09 - 0:14
    arguments array which exists
    mysteriously in JavaScript perhaps
  • 0:14 - 0:16
    without you even knowing that it is there
  • 0:16 - 0:20
    what you can use it for and this idea of
    overloading a function or overloading a
  • 0:20 - 0:24
    constructor and how do you have optional
    arguments to a function all this stuff.
  • 0:24 - 0:29
    Okay what am I talking about first of all,
    so let's just say today is a nice sunny
  • 0:29 - 0:33
    warm beautiful day it rained last night a
    rainbow came out and you just sat at
  • 0:33 - 0:39
    your computer and decided I want to
    program my own function called sum, and
  • 0:39 - 0:44
    what sum is going to do is it's going
    to take two arguments a and b; and
  • 0:44 - 0:49
    it's going to say return (a+b). So
    it's just going to make a sum of
  • 0:49 - 0:54
    whatever things I pass to it, so I
    could then say, here in setup
  • 0:54 - 1:01
    I could say, var c equals.. aah you know c
    equal or whatever.. value equals the sum
  • 1:01 - 1:06
    of 5 and 15 and if I say console.log(val);
  • 1:06 - 1:10
    we should see then if I go here, I should
    see 20 and I could say something like
  • 1:10 - 1:17
    sum(2, 7) and I'm going to get 9
    or sum(-3, 7) and I'm
  • 1:17 - 1:23
    going to get 4. So this function works;
    but what if I were to say this: sum(4)
  • 1:23 - 1:26
    Not a Number (NaN)
  • 1:26 - 1:30
    Well let's say I wanted to be able to
    actually have it return just the
  • 1:30 - 1:31
    number four.
  • 1:31 - 1:36
    How could I have it do that? It's getting
    not a number because what is the value
  • 1:36 - 1:43
    of the b in this case? If I were to say
    console.log(b) and run this again
  • 1:43 - 1:47
    you're going to see the value of b is
    undefined
  • 1:47 - 1:53
    another.. another way that I can
    consider undefined, undefined if I were
  • 1:53 - 1:56
    to ask is undefined true or false,
  • 1:56 - 2:01
    undefined is actually false so something
    that I could do for example is just say
  • 2:01 - 2:04
    something like if b
  • 2:06 - 2:19
    return a + b, otherwise return a. So in
    this case I now have a function that can
  • 2:19 - 2:23
    perform differently based on the number
    of arguments it gets, right? As long as it
  • 2:23 - 2:29
    gets a second argument return two of
    those two numbers together if it doesn't
  • 2:29 - 2:30
    get the second argument right?
  • 2:30 - 2:36
    if b is false simply return a so in
    this case now if I go back to the
  • 2:36 - 2:39
    browser and I say sum(4, 5)
  • 2:39 - 2:43
    I should get nine but if I say sum(4)
    I'll just get four so we have now
  • 2:43 - 2:49
    written a function that can optionally
    have a second argument very very basic
  • 2:49 - 2:50
    idea here
  • 2:50 - 2:57
    now let's take this a few steps further
    what if I just said sum() with no arguments
  • 2:57 - 3:06
    now I've got undefined again so in this
    case I could actually say if a.. if a and
  • 3:06 - 3:15
    b else if.. then.. wait.. I was going to say if b
    that means there has to be an a right?
  • 3:15 - 3:25
    return a + b, else if a return a, else
    return 0. Ooh I made this sort of like messy
  • 3:25 - 3:27
    and awkward and weird but you can start
    to sort of see what the possibilities
  • 3:27 - 3:32
    are so now I'm kind of checking if I get
    two arguments return some of both of
  • 3:32 - 3:37
    those, if I have.. if I have one argument
    return just that one argument if I have
  • 3:37 - 3:38
    no argument
  • 3:38 - 3:42
    return the value zero. Let's just at least
    make sure that works, right? So I could
  • 3:42 - 3:48
    say sum of four and five, I get nine;
    sum of four, I get four; sum with nothing
  • 3:48 - 3:55
    I get 0. So great, so this is working but
    what if I now want to say sum(4, 6, 8)?
  • 3:55 - 3:58
    I still only get 10, right?
  • 3:58 - 4:03
    Eight isn't involved, well guess what
    anytime you call a function in
  • 4:03 - 4:09
    JavaScript the arguments to that
    function 4, 6, 8
  • 4:09 - 4:17
    those things get placed into what..?
    an array, an array called arguments. You could
  • 4:17 - 4:21
    just say the word arguments and suddenly
    you have everything in an array. Let me
  • 4:21 - 4:22
    show you what I mean by that,
  • 4:22 - 4:26
    if I go back to the code and
    I just say here
  • 4:26 - 4:39
    I say console.log(arguments) and I
    say sum.. oops.. sum(4, 6, 8) you can see
  • 4:39 - 4:41
    what is the value of arguments?
  • 4:41 - 4:46
    It's an array 4 , 6 , 8 so you know what
    I could actually do in the case of this
  • 4:46 - 4:51
    function I can write a function called
    sum, I don't even have to name any of
  • 4:51 - 4:55
    the arguments here. I can say there is a
    value at 0
  • 4:55 - 4:58
    I can then go through the
    length of the array
  • 4:58 - 5:03
    i starts at zero; i goes all the way up
    through the.. all the arguments; val
  • 5:03 - 5:08
    plus equals arguments index i
    and then return val.
  • 5:08 - 5:14
    this is now a function that can take any
    amount of things you've.. any number of
  • 5:14 - 5:19
    arguments, 0 arguments, 5 arguments, 10
    arguments, 20 arguments and it can loop
  • 5:19 - 5:22
    through and add them all together
  • 5:22 - 5:29
    so now here I could say things like sum(5)
    and I have a woops..
  • 5:29 - 5:36
    sorry. I think I didn't reload it, sum(5)
  • 5:36 - 5:40
    I have the number five; sum(5,6)
  • 5:40 - 5:50
    I have the number 11;
    sum(5, 3, 2, 5, 1, 10, 11, -5, 3, 33)
  • 5:50 - 5:53
    68!! Somebody confirm for me that that
    number is correct.
  • 5:53 - 5:57
    It's going to take any number of
    arguments and loop through all of them
  • 5:57 - 6:01
    so you can think of all sorts of
    interesting possibilities that this
  • 6:01 - 6:03
    might open up for you in how you write a
    function.
  • 6:03 - 6:07
    I could also name those arguments too as
    well so like for example
  • 6:07 - 6:10
    I don't know why I want to do this
    but I could
  • 6:10 - 6:14
    name the first argument and I could also
    say that, return val + a
  • 6:14 - 6:20
    so in this case this is a little bit
    weird but if i say sum(1, 2) it's
  • 6:20 - 6:23
    going to add one and two and then add 1
    to it again so I would get the number 4.
  • 6:23 - 6:28
    A little bit silly but you can see
    that you can both use that arguments'
  • 6:28 - 6:30
    array and you can also name
  • 6:30 - 6:33
    optionally name those arguments coming
    in so there's a lot of possibilities of
  • 6:33 - 6:34
    things you can do here.
  • 6:34 - 6:39
    Now let me make this even.. just to make
    this even more a little bit stranger
  • 6:39 - 6:46
    what if i want my function let's say I
    were to create an array.
  • 6:46 - 6:54
    So I have an array that has the values
    [5, 3, 6] in it. So I know I could say
  • 6:54 - 6:57
    (5, 3, 6) with sum and i'm going to get 19
  • 6:57 - 7:00
    did I take.. did I leave that weird
    extra plus a thing in there?
  • 7:00 - 7:05
    I want to take that out and run this
    again and I'm gonna make that array
  • 7:05 - 7:10
    I'm going to do this 14, right? So what
    what if I.. what if I also wanted to be
  • 7:10 - 7:15
    able to say sum(array)? In this case look
    what it did something crazy happened
  • 7:15 - 7:20
    because it actually tried to add 0 with
    the array but then it tried to make the
  • 7:20 - 7:22
    array a string and it concatenated
    together
  • 7:22 - 7:25
    JavaScript just tried to figure
    something out but this isn't what I want
  • 7:25 - 7:31
    I want that if you pass this function
    an array as its argument that you're
  • 7:31 - 7:33
    going to add the elements of that array
  • 7:33 - 7:36
    so how would I do that? Well what I want
    to do is I want to check
  • 7:36 - 7:45
    I could put this back in, a, I could say
    if a is an instance of array and I think
  • 7:45 - 7:52
    capital or like this so this is a kind
    of keyword in JavaScript, instanceof
  • 7:52 - 7:55
    that can determine what kind of argument
    you passed in
  • 7:56 - 8:00
    so you could say like oh well if the
    function receives a string as its argument
  • 8:00 - 8:03
    it should act this way but if it
    receives a number as its argument
  • 8:03 - 8:04
    it should act this way
  • 8:04 - 8:09
    so if a is an instance of array, and I'm
    actually going to call this
  • 8:09 - 8:17
    arr, then what I want to do is do exactly
    this algorithm but instead of using the
  • 8:17 - 8:19
    arguments array
  • 8:19 - 8:23
    I just want to use that actual array so
    now I'm going to say
  • 8:23 - 8:27
    array.length and add all that up,
    right?
  • 8:27 - 8:31
    so if you get an array loop through that
    array and return the sum of everything
  • 8:31 - 8:36
    in that array, if you don't get an array
    then loop through the.. because it's..
  • 8:36 - 8:39
    there's just a sequence of numbers then
    use the arguments array so now I have
  • 8:39 - 8:43
    like all sorts of possibilities here for
    how this function could perform
  • 8:43 - 8:50
    so now I can say let me have sum.., it
    still works if I give it five three and six
  • 8:51 - 8:55
    now if I make an array [5, 3, 6] and I
    say sum of that array
  • 8:56 - 9:02
    I should also get that same number and
    if I were to say array dot 4 equals
  • 9:02 - 9:06
    negative one and look at that array..
  • 9:06 - 9:12
    Whoops I have an undefined, array[3]
    equals 2 and look at that array again
  • 9:12 - 9:17
    and let me clear that, clear it,
    look at that.. look at the array and now if I
  • 9:17 - 9:21
    say sum(array) I should still get..
    I should get 15 which is right
  • 9:21 - 9:25
    fourteen plus two is sixteen minus one is 15
    so now I have this function which can
  • 9:25 - 9:27
    both receive an array.. now
  • 9:27 - 9:30
    unfortunately I can't do this, right?
  • 9:30 - 9:34
    I would have to write it.. as an exercise
    for yourself if you want a tricky exercise
  • 9:34 - 9:38
    how could you now write this function so
    that it can have mixed arrays and
  • 9:38 - 9:41
    regular numbers and still add all those
    things together
  • 9:42 - 9:45
    Ok... yes
  • 9:45 - 9:49
    so in the chat some people are saying I
    could use the apply function up and
  • 9:49 - 9:53
    other types of things to like put the
    array into the arguments array and
  • 9:53 - 9:56
    that's a great suggestion maybe I'll
    cover that in a separate video, but right now
  • 9:56 - 10:00
    I just want to look at different ways
    that you can have flexible arguments for
  • 10:00 - 10:05
    a function and this often comes up in
    the case of making objects like a
  • 10:05 - 10:08
    particle object. So let's take this a
    little bit further and let's just say
  • 10:08 - 10:11
    that this is going to be a program and
    I'm just going to take this and
  • 10:11 - 10:16
    comment it out for right now and I'm
    gonna say let's say I have a.. I'm writing
  • 10:16 - 10:22
    a program with some sort of particle
    object and the whole part of this
  • 10:22 - 10:28
    particle object is that it has a
    position which is a vector and I'm gonna
  • 10:28 - 10:32
    make that vector width/2, height/2
  • 10:32 - 10:36
    and then I'm going to have a function
    called show which..
  • 10:36 - 10:43
    what does that function do? It draws a circle on
    the screen at that vector's x and y and
  • 10:43 - 10:51
    maybe fill that 255 and I'm going to
    stay here.. I'm going to say var p is a
  • 10:51 - 10:56
    new particle and then p.show
  • 10:56 - 11:02
    now, you can see I run this program and I
    get that circle appearing in the center
  • 11:02 - 11:03
    of the window
  • 11:03 - 11:07
    now let's think about all the possible
    ways I might want to make a particle
  • 11:07 - 11:10
    for example I might want to make.. and
    let's do it.. let's do this differently
  • 11:10 - 11:17
    now, let's.. let's say there's an array
    called particles and what I'm gonna do
  • 11:17 - 11:22
    is I'm gonna say particles index 0 is a
    new particle and then I'm going to loop
  • 11:22 - 11:29
    through and I'm just going to call show
    and all the particles in that array so
  • 11:29 - 11:33
    this is essentially the same program but
    the reason why I want to show this to
  • 11:33 - 11:34
    you, right?
  • 11:34 - 11:39
    is that I want to be able to say things
    like this, well particles index one
  • 11:40 - 11:44
    what if I could make a particle this way?
    what if I could say this?
  • 11:48 - 11:58
    and what if I could say particles index
    2 equals a new particle with that
  • 11:58 - 11:59
    particular vector?
  • 11:59 - 12:06
    what if I were to say this, particles
    index 3 equals equals a new particle
  • 12:08 - 12:18
    whoops let's make this (150,100)..
    (100,150). Now I'm not so sure I really want
  • 12:18 - 12:21
    all of these but you could imagine an
    object
  • 12:21 - 12:24
    you might be able to, in your code you
    might want to make particles and
  • 12:24 - 12:27
    sometimes you make a particle with no
    object no arguments and always puts it
  • 12:27 - 12:32
    in a default location or you could
    specify the location as two values an x
  • 12:32 - 12:37
    and y or you could just pass in a vector
    it would put the particle at that vector
  • 12:37 - 12:41
    or maybe for some reason you are getting
    data from a text file you're reading
  • 12:41 - 12:42
    in strings
  • 12:42 - 12:45
    and what if you give it two numbers
    separated by a comma how would you deal
  • 12:45 - 12:46
    with it then?
  • 12:46 - 12:49
    This is the kind of scenario that can
    come up in many things that you might
  • 12:49 - 12:50
    want to program.
  • 12:50 - 12:53
    So let's see if we can get all these
    particles to appear on the screen
  • 12:53 - 12:59
    so one thing that I might do here first
    of all is give it some
  • 12:59 - 13:07
    let's give it two arguments an x and y
    so one thing we can do is say this and
  • 13:07 - 13:13
    actually I'm gonna.. I'm going to change
    it from having a position vector
  • 13:13 - 13:17
    just to give it.. just right now I'm going
    to change it to just having an x and y. So one
  • 13:17 - 13:21
    thing I could actually do that you'll
    see is am gonna say this.x equals x or
  • 13:21 - 13:31
    100, this.y equals y or 100. This would
    cover me for these first two cases, right?
  • 13:31 - 13:36
    I'm basically saying I want to take the
    value that comes in or some default
  • 13:36 - 13:37
    value if it's false
  • 13:37 - 13:41
    and in this case it would be false because it
    would be undefined and no value would come in
  • 13:41 - 13:47
    so let's uh let's actually say I want to
    make the particle at 150, 50 and we
  • 13:47 - 13:51
    could see.. we should be able.. we should
    see two particles now, right?
  • 13:51 - 13:53
    so this is
  • 13:53 - 13:57
    and let me give all the.. ok it
    doesn't really matter the point is you
  • 13:57 - 14:02
    can see now when the particle gets two
    arguments coming in it..
  • 14:02 - 14:07
    it uses those two arguments like
    150,50; when it doesn't get those
  • 14:07 - 14:09
    coming in to use 100,100
  • 14:09 - 14:16
    so this is 100,100 and this is wh..
    50,150 thats that second particle
  • 14:16 - 14:19
    ok so those two cases are covered
  • 14:19 - 14:25
    now what about this third case what if
    I'm passing in a vector? So one thing I
  • 14:25 - 14:31
    can do is I can say okay well actually
    right here I could say if this. x is an
  • 14:31 - 14:39
    instance of a p5.vector object then
    this.x equals x.x and this.y
  • 14:39 - 14:40
    equals x.y
  • 14:40 - 14:46
    now this is a little bit weird because
    I'm sort of stuck in this place of I
  • 14:46 - 14:51
    named that first argument x so now x is
    kind of this vector I still have to call
  • 14:51 - 14:56
    it x, so I might actually want to just
    use more generic names like a and b
  • 14:56 - 15:01
    in this case so I can say if.. and I'm not
    saying if this.x, I'm saying if a is an
  • 15:01 - 15:08
    instance of p5.Vector do this otherwise
    go back to this other idea
  • 15:09 - 15:12
    so now I'm checking, I've had two three
    possibilities, one is I've gotten a
  • 15:12 - 15:15
    vector or another possibility I got
    nothing
  • 15:15 - 15:20
    or I got two separate values and we can
    run this again you can see now I have
  • 15:20 - 15:25
    three of them, all three of those work
    and I have one last possibility which is
  • 15:25 - 15:34
    this string so I can also say else if a
    is an instance of string I guess?
  • 15:34 - 15:39
    I forgot there's a sort of funny thing with
    strings that I forgot about because this
  • 15:39 - 15:44
    if a the instance of string is not
    actually checking it a is just text but
  • 15:44 - 15:49
    if it's an actual like fully formed
    string object in JavaScript and let me
  • 15:49 - 15:51
    just show you this, a little bit of it
    aside but it's kind of worth it since it
  • 15:51 - 15:53
    might come up
  • 15:53 - 15:57
    let's say I create a var s equals test
  • 15:57 - 16:00
    now if I say s instanceof String
  • 16:00 - 16:04
    you're going to actually see that that
    gives me false but if I were to say s
  • 16:04 - 16:10
    is a new string with the text 'test' in it,
    right? it's actually like a whole string
  • 16:10 - 16:17
    object now and this will actually return
    true but in most cases I'm just going to
  • 16:17 - 16:20
    be in the land of saying s equals test
  • 16:20 - 16:25
    whoops something like this so another
    function that I can use in JavaScript is
  • 16:25 - 16:31
    typeof() and I could say typeof(s) and
    I'll actually get the word string back
  • 16:31 - 16:41
    and so I think in this case I could say
    else if typeof(a) equals string
  • 16:41 - 16:46
    oops.. then now let's see if that works
    I'm gonna run this and you can see that
  • 16:46 - 16:47
    came through
  • 16:47 - 16:55
    so since that came through what I need
    to do now is say I can use like
  • 16:55 - 17:02
    a.split by a comma and then I can say
    this.x equals Number
  • 17:03 - 17:08
    nums index 0 and this.y equals Number
  • 17:08 - 17:15
    nums index one. So split() takes something
    takes the string.. so let me do this
  • 17:15 - 17:16
    actually
  • 17:16 - 17:23
    so let's say s is 100.. the numbers in a
    string 100,150 and what I want to do
  • 17:23 - 17:29
    is I want to say nums equals s.split
    by a comma ahh.. s.split by a comma
  • 17:29 - 17:34
    so now nums is just an array with two
    strings in it and so I could say nums
  • 17:34 - 17:39
    index zero but that's actually not the number
    one hundred it's the string 100 but..
  • 17:39 - 17:42
    and there's lots of ways I could probably
    convert it but one way is I could just
  • 17:42 - 17:43
    do this
  • 17:43 - 17:49
    convert it to a number num..whoops not
    numbers number and you can see now I
  • 17:49 - 17:53
    have the value 100 so I'm just doing
    that in the code here I'm saying Ah if I
  • 17:53 - 17:58
    happen to get a string split it up by the ,
    then convert those two numbers and we
  • 17:58 - 18:01
    should see then another particle at ahh..
  • 18:01 - 18:07
    100,150. So if I now run this again we
    can see now I have four particles so we've
  • 18:07 - 18:12
    now written a constructor function
    that optionally it takes two arguments,
  • 18:12 - 18:14
    you could give it no arguments
  • 18:14 - 18:17
    you could give it two numbers, you could
    give it a vector, you could give it a
  • 18:17 - 18:21
    string and it's going to figure out how
    to fill in the x and y based on what it
  • 18:21 - 18:27
    gets so this is one just kind of small
    instance of that but there's lots of
  • 18:27 - 18:30
    other scenarios where this could come up
    where you want to write a function to
  • 18:30 - 18:35
    arbitrarily handle a certain number of
    arguments of different types. In the
  • 18:35 - 18:39
    incidentally if you were doing this in
    Processing or in Java you would just write
  • 18:39 - 18:43
    multiple copies of the function each
    with different arguments and then handle
  • 18:43 - 18:46
    the.. those arguments in that but in
    JavaScript since the arguments don't
  • 18:46 - 18:50
    have a type, a type is not part of this
    signature of that function of that
  • 18:50 - 18:54
    method you have to handle them with an
    if statement by having sort of optional
  • 18:54 - 18:56
    flexible arguments
  • 18:56 - 18:59
    ok hopefully you found this helpful and
    you learned something about what the
  • 18:59 - 19:03
    arguments array and JavaScript does and
    how to write a function or a constructor
  • 19:03 - 19:06
    function that has flexible different
    kinds of arguments and different numbers
  • 19:06 - 19:07
    of arguments.
    Thanks for watching, goodbye.
Title:
9.14: Arguments Array in JavaScript - p5.js Tutorial
Description:

more » « less
Duration:
19:09

English subtitles

Revisions