now i'm gonna go over one more thing and that would be that there was a question how do you
know whether you have to use parentheses or not use parentheses like for example if i do here get
num rows do i have to do parentheses after that or not so an easy answer for this is if i go here
and do dot and you see there is this purple icon sticking next to it that means it's a
method so if it's a method then you're going to have to use this parenthesis after that
now let's see if that same thing has anything that's not a method so let's scroll down and find
out now this one doesn't have it anything here you can see this icon it's a method so you would
have to use open close parentheses right there but let's go on the spreadsheet app and hit dot
and see if this has anything so see here we have this purple ones right here these are all methods
that means with parentheses and if we scroll up see like this autofill series it's not one of
those icons like this so it's not a method so that means for this one you would not do parentheses
so that's just a property and on that you might have other things so if i do dot see now i have
see this is also not that purple thing so again it's not a method so that would be just this so
the quick answer to that without really understanding the underlying difference between
methods and properties is that now as far as in general let's just go over and add a script i'm
just going to call it object i guess i know people sometimes get really irritated when they come from
different programming languages and you call this thing an object but i'm going to do it anyways
so generally speaking if you create a javascript object
we'll call this object the cell and this cell will be an object so within this cell
we can have different properties of that cell so for example we can say color
is red and then i can do comma and do like a value of the cell is 45 something like that so
if i wanted now to access that color and value i would do cell dot see color and value and these
are called properties so you can see how that purple icon doesn't show up so that means if i
wanted to get that red or that value i would just simply do this because it's actually
a property so let's just do console.log so you can see how this works
so if i go back and run this thing that's called my function
now this is not any more triggered execution so we can see this here you can see this is red
right so now if i go inside of this object i can actually create a function in this object
like this let's call this function
get value i guess let's make it similar to how spreadsheets sort of work like this and that would
be a function and in this function let's return something so i'm going to return 55. like this
so now this is a function and this is a property as you can see because it's not a function
aligned to this so that means that if i now wanted to get that use that get value thing that i have
there then the way that would work it would be that same cell dot and then you would do get
value and this should be a function so let's try this and see if this works and it does see
i got that 55 which is that value of this so because this is a function within that object
then we have to use those parentheses here
right there and this color it's not a function within that object so therefore i don't know how
i get this dots all the time but there should be no dots like this so then here there is no
parenthesis so that's the real answer of why
you may or may not have those parentheses one is a property the other one is basically a function
how would you use classes in apps script
i don't know are you asking why you would use it or how you would use it
because why it's a little difficult question to answer but like if you
were doing like basic scripts you would probably not use classes
but if you just want to create a class so if i wanted to replicate this maybe
so i could create like a class and let's actually get this outside of this
and create a class called cell
and here you could have some sort of constructor that runs so constructor is the first function
that's going to run when you instantiate this for example you might want to set some of those things
that we had before so for example if you remember i had like the color so i would say this color
red and this refers to the instance of this object it's kind of a complicated topic
to just explain what this means but basically for anything you set on that object it's going
to start with this so i would say this color this value equals let's say 45 something like that
and then we could have functions within this two so if you remember i had that get value
function so here notice that when i create a function i don't put the word function
in front of it i just do this to do the get value and we can return let's say 55
something like this so this would be the class way of doing that same thing i just went through
a second ago for a class to work we have to basically instantiate the class so what that means
is that i can go to my function here and use this class cell now so i can say const
my cell create a variable equals new cell and because i'm not passing any arguments
in this function then we're not going to pass anything here in this parentheses
and this my cell at this point will be that object and you can see now that autocomplete
is very similar to how that autocomplete works when we work with that internal stuff
we have so see this color now would refer to this red so if i do console.log
color that should now if i run this function that should now return that red
and if i wanted to use that get value function then again
you would use this because it's a function it goes with parentheses
and that's if i run this see this is red this is 55 right there so the way this works is that
once you do this new cell this my cell variable that you create essentially becomes this this
so this this at that point is this my cell once i do this and we're saying now
in this my cell let's add a dot color and make it red and then dot value and make it 45
and this is what that this will refer to this instance of that particular
object now you could create multiple objects using the same what we call constructor
at the same time other cell like so and now this other cell would be its own instance of that
object and it will have its own this color this value so now we have this that refers
that this now here first time refers to this and then for this other cell again that would be this
too so basically it allows us to reuse all of this code for both of these in the same manner
while we have them as separate objects and that will allow us to change things in these objects
and things like that so maybe we want to add like set value here function so what we can do maybe we
want to change this value here right using the set value so i can get that value whatever that
argument is going to be i'm just going to call it v and then i can take this internal
value of this object and set it to be that v that was passed in this function right there
and now what that means is that i have a way to change what the value here is now
let's actually get rid of this for a second here and get rid of this color
and for now let's just look at this value and remember this value is not this it's this it's
that value because it's the property that 45. so if i run this that gives me 45 right
but now what we can do we can use that function set value on this my cell
to change that value so let's say i want it to now become 77.
so now if i do that console log in the next line after this save this and run
my cell is not defined did i misspell it of course i did it's a uppercase c not lowercase
save this around this again see it was 45 now it's 77.
so initially it was 45 here then we can use now this set value function here to modify what this
internal value 45 is using this set value over here and we can change our get value for example
instead of returning that 45 to actually return the value so we can say return this dot value
and this way now we should be able to use that get
value instead of just value to get the value of that
property so these would be our methods these are our properties basically
and right now i'm not using any of the properties i'm just using methods to set and get the value
so if i run this you can see we should get the same thing 45 then we change it now it's 77.
so that's the gist of it of how you would create an object
i guess one thing that's worth to mention here is that when you create this constructor you
may want to just pass some things by default so let's say you want this default value instead of
being 45 to be whatever you give it when you create this and the color so we can just go
here and do something like two arguments in in this constructor we can do uh like color
actually let's just call it c so it's not confusing just one uh the color and v
so now we have this two arguments which we can pass here we can pass like black and make it
by default to be like 32 something like this so what we can do here instead of making this
red here we can just make it this argument that we pass here to be the c and then this one would be
this argument we pass here to be v so that means when we instantiate this object when we create it
it will make the color here black and this to be 32 and then again we can get that value and we can
set that value to 77 later so if i run this you'll see it's 32 to begin with then we modify it to 77
so that's the way you can pass arguments directly to this constructor too if you want