Object Methods:
We saw how to call an object on the last page, but of course there are other methods, just as is the case for "strings"
,
Numbers
and [arrays]
Below are just a few of them, using our Band
object from the last page:
Above, there are again a few things to note:
typeof
:- typeof
Function
- typeof
new Object()
- typeof
variable
.length
When we call the typeof
method on the function Band()
, we do not
include the ()
. If we do, the result returns undefined
.
When we call the typeof
method on the new Object()
, we can
include the ()
but we don't have to.
Since new Band()
has been assigned as a variable
called dogsDamour
, if we add ()
then console alerts us to the fact that a variable
is not a function
.
We cannot call .length
on the variable itslef. We have to call its key
/ data
values. Or, we can call its .entries
What happens if one of the data
values has not been completed?
Lets create a new Object
from the Band()
function, but lets make
it a 3 piece band, which only released a couple of albums:
Now, let's call the .length
method again:
The code is telling us that there are 13 keys
and 13 data
entries. And there are... but some of the
data
entries are in fact empty "strings"
- we can see this if we study carefully the .entries
code. But
although a computer might read this code perfectly well, it is not very clear to us humans. So let's find a different, better way to get the
key
/ data
values: lets use a for... in
loop (open console
to
see the results):
We can also see the results using a for... of loop:
Note the differences in the two loops, and see here for a reminder about their differences.
Object Constructor: