1
00:00:02,260 --> 00:00:04,360
Now let's stick to functions for the moment

2
00:00:04,360 --> 00:00:07,860
because there's one other thing you should be aware of

3
00:00:07,860 --> 00:00:09,940
when we talk about functions

4
00:00:09,940 --> 00:00:12,948
and that's stat behind the scenes,

5
00:00:12,948 --> 00:00:17,948
internally, functions are in the end, just objects.

6
00:00:18,760 --> 00:00:22,040
And that can be confusing because we did learn

7
00:00:22,040 --> 00:00:25,950
that objects are these things with those curly braces,

8
00:00:25,950 --> 00:00:27,340
which you create like this.

9
00:00:27,340 --> 00:00:31,040
And then you could add a name which has max and the age,

10
00:00:31,040 --> 00:00:33,950
which is 32 and do something like that.

11
00:00:33,950 --> 00:00:35,370
That's an object.

12
00:00:35,370 --> 00:00:37,340
That's what we learned before.

13
00:00:37,340 --> 00:00:40,060
And objects have these key value pairs,

14
00:00:40,060 --> 00:00:41,638
which are called properties,

15
00:00:41,638 --> 00:00:45,370
which you can access with the dot notation

16
00:00:45,370 --> 00:00:49,040
to set a new value or to read the value.

17
00:00:49,040 --> 00:00:52,750
And you can also add methods to objects,

18
00:00:52,750 --> 00:00:56,750
but functions are also objects.

19
00:00:56,750 --> 00:01:01,750
And for that let's console log sumUp without parentheses.

20
00:01:02,710 --> 00:01:05,253
So just the name of the function here.

21
00:01:06,250 --> 00:01:08,970
With that, we are not executing the function,

22
00:01:08,970 --> 00:01:12,750
but instead we are just getting a reference

23
00:01:12,750 --> 00:01:13,860
to that function.

24
00:01:13,860 --> 00:01:17,560
We're just pointing at that function and we are therefore

25
00:01:17,560 --> 00:01:20,340
logging the function itself.

26
00:01:20,340 --> 00:01:23,970
Instead of the result of calling the function.

27
00:01:23,970 --> 00:01:28,400
That's what we do here instead, here in this line,

28
00:01:28,400 --> 00:01:32,150
we output the result of calling that function because

29
00:01:32,150 --> 00:01:34,413
we do have those parentheses here.

30
00:01:35,600 --> 00:01:37,037
On the other hand in this line,

31
00:01:37,037 --> 00:01:40,050
we are just outputting the function itself.

32
00:01:40,050 --> 00:01:43,560
And if we do that and I save everything here,

33
00:01:43,560 --> 00:01:45,920
if we execute this file,

34
00:01:45,920 --> 00:01:50,053
what we see is this strange syntax here, function sumUp.

35
00:01:51,020 --> 00:01:53,170
Now that's not telling us too much.

36
00:01:53,170 --> 00:01:55,570
It tells us that we have some function here,

37
00:01:55,570 --> 00:01:56,980
which is called sumUp,

38
00:01:56,980 --> 00:01:59,660
but that's actually just how NodeJS outputs

39
00:01:59,660 --> 00:02:02,030
this function object.

40
00:02:02,030 --> 00:02:05,160
If we do the same thing in the browser,

41
00:02:05,160 --> 00:02:10,139
simply because there we get a, a nicer, more useful output,

42
00:02:10,139 --> 00:02:12,400
then we'll see something different.

43
00:02:12,400 --> 00:02:14,350
And for that, I opened a brand new tab

44
00:02:14,350 --> 00:02:17,044
and then the console here in the dev tools,

45
00:02:17,044 --> 00:02:19,610
because in here, we can actually also write

46
00:02:19,610 --> 00:02:23,780
some JavaScript code that will be evaluated.

47
00:02:23,780 --> 00:02:27,090
And therefore here I'll just add a function called add,

48
00:02:27,090 --> 00:02:29,763
which accepts two numbers, let's say,

49
00:02:29,763 --> 00:02:33,050
and which then just returns.

50
00:02:33,050 --> 00:02:35,440
Num one plus num two,

51
00:02:35,440 --> 00:02:37,600
and we're writing it all in one line here.

52
00:02:37,600 --> 00:02:40,200
And it's a very simple function because here,

53
00:02:40,200 --> 00:02:42,650
I don't want to talk about this function too much,

54
00:02:42,650 --> 00:02:45,723
but instead about that object, which every function is.

55
00:02:46,560 --> 00:02:48,500
So for that, I'm writing a function here.

56
00:02:48,500 --> 00:02:52,880
If I console log at here in the browser dev tools

57
00:02:52,880 --> 00:02:55,610
in this console, which you can also use

58
00:02:55,610 --> 00:02:57,810
to play around with some dummy code,

59
00:02:57,810 --> 00:03:01,000
as you see here, and I hit enter here,

60
00:03:01,000 --> 00:03:04,150
then we already get a slightly different output.

61
00:03:04,150 --> 00:03:08,020
And if we now replace console log with console dir,

62
00:03:08,020 --> 00:03:11,520
which is more useful when outputting objects,

63
00:03:11,520 --> 00:03:15,790
then you also get a little arrow here, which you can click.

64
00:03:15,790 --> 00:03:20,240
And if you click it, you'll see a couple of properties.

65
00:03:20,240 --> 00:03:25,240
These are key value pairs, and these are key value pairs,

66
00:03:25,500 --> 00:03:30,500
of an object, of the function object that every function is.

67
00:03:32,020 --> 00:03:35,840
Now, these are not properties, which you added manually.

68
00:03:35,840 --> 00:03:39,030
We haven't added any properties at all to this function.

69
00:03:39,030 --> 00:03:41,050
Instead, these are properties which are

70
00:03:41,050 --> 00:03:43,610
inferred automatically.

71
00:03:43,610 --> 00:03:44,480
For example,

72
00:03:44,480 --> 00:03:48,770
the name of the function is stored in a separate property

73
00:03:48,770 --> 00:03:50,470
or the number of arguments.

74
00:03:50,470 --> 00:03:53,180
The number of parameters this function takes

75
00:03:53,180 --> 00:03:56,630
is stored in a length property.

76
00:03:56,630 --> 00:03:59,830
Now, why do you need to know that, if you don't set

77
00:03:59,830 --> 00:04:01,463
those properties on your own?

78
00:04:02,330 --> 00:04:05,940
Well, not because we will start setting properties

79
00:04:05,940 --> 00:04:08,870
on our functions, but because this is something

80
00:04:08,870 --> 00:04:10,913
we encountered before already,

81
00:04:11,880 --> 00:04:15,780
when we used express this express package,

82
00:04:15,780 --> 00:04:19,315
then we did require it in our NodeJS code.

83
00:04:19,315 --> 00:04:22,973
And we executed it like a function to create

84
00:04:22,973 --> 00:04:25,600
this app object here.

85
00:04:25,600 --> 00:04:27,590
That's what we did before.

86
00:04:27,590 --> 00:04:30,180
Now in other places of the code.

87
00:04:30,180 --> 00:04:33,880
We however, used that same express function,

88
00:04:33,880 --> 00:04:37,370
which it seems to be, to then all of a sudden access

89
00:04:37,370 --> 00:04:39,840
properties on that function.

90
00:04:39,840 --> 00:04:44,030
And we were able to do that because the team that developed

91
00:04:44,030 --> 00:04:47,762
the express package added some custom properties

92
00:04:47,762 --> 00:04:51,840
to this express function, to add more functionality

93
00:04:51,840 --> 00:04:55,483
to that express thing that you can use in your code,

94
00:04:56,660 --> 00:04:59,070
you could do something similar in your own code

95
00:04:59,070 --> 00:05:01,010
with your own functions as well.

96
00:05:01,010 --> 00:05:04,995
You can use the sumUp function and use the dot notation

97
00:05:04,995 --> 00:05:07,460
to add some property.

98
00:05:07,460 --> 00:05:11,140
You can do that for all objects that you add new properties

99
00:05:11,140 --> 00:05:14,850
by simply accessing properties that don't exist yet and

100
00:05:14,850 --> 00:05:16,970
storing new values in them.

101
00:05:16,970 --> 00:05:19,420
But of course, typically you don't do that

102
00:05:19,420 --> 00:05:21,692
because it's not too useful.

103
00:05:21,692 --> 00:05:25,970
The team that developed the express package did that though.

104
00:05:25,970 --> 00:05:28,660
And I'm just emphasizing this again here.

105
00:05:28,660 --> 00:05:30,960
So that it's clear that even though we

106
00:05:30,960 --> 00:05:33,810
don't typically do that, we can do that.

107
00:05:33,810 --> 00:05:36,200
We can add properties to functions

108
00:05:36,200 --> 00:05:40,760
because functions behind the scenes are all just objects,

109
00:05:40,760 --> 00:05:44,220
special kinds of objects, but still objects.

110
00:05:44,220 --> 00:05:48,530
And that's why express could both be executed as a function

111
00:05:48,530 --> 00:05:53,210
and then be used like a object when we access properties.

112
00:05:53,210 --> 00:05:57,360
And that's just something which I wanted to bring up again

113
00:05:57,360 --> 00:06:01,823
to make it clear why that code worked the way it did work.

