Transcript from the "Functions are Objects & Functions" Lesson
[00:00:49] I'm gonna try and write this way. What here are we doing, Elliot, in line one?
>> Elliot: Declaring a new function, multiplyBy2.
>> Will Sentance: Declaring a function, multiplyBy2. There it is, saved as a function. Perfect. Next line? What's there to do, Elliott?
>> Elliot: Add a new property, store it.
>> Will Sentance: But it's a function.
>> Elliot: Also an object.
>> Will Sentance: Okay.
[00:01:47] They are never functions alone, they are function-object combos. Now most of the time we don't really care because we don't really use the function's object bits. But we absolutely are in object-oriented programming. So as soon as you declare multiplyBy2 it is, yes, a function, okay. But immediately it also is an object.
[00:02:13] And if you use what type of notation, Elliot, to get the object bit?
>> Elliot: Dot.
>> Will Sentance: If you use dot notation you're going to access its object form, and we're gonna assign the property five. But maybe, Elliot, that having this object and assigning stored to it, maybe that overwrote the function part.
[00:02:36] Maybe now if we try and call multiplyBy2 with the input of three, we gonna find multiplyBy2, it's an object, it's not a function. Or what, what do you think, Elliot? Can we still do this? We absolutely still can. We access the function piece with our what symbols? How do we access the function piece, Nick?
>> Elliot: With the-
>> Elliot: Global memory.
>> Will Sentance: In global memory. And then we use dot notation to access multiplied by, we find it, it's a function-object combo.
[00:03:29] And what, Elliot, which bit of it do we look for when we use a dot notation?
>> Elliot: The property?
>> Will Sentance: We look on its object bit-
>> Will Sentance: In its function-object combo, where we find the property what?
>> Elliot: Stored.
>> Will Sentance: Stored, beautiful, and we assign to it?
>> Elliot: Five.
>> Will Sentance: Here is where we access the number five. So this is what actually happens when we defined a function. In fact over here when we declared user creator earlier, here's what we actually declared, a function-object combo. A function object combo.
>> Will Sentance: An object, a function, an object. This reminds me of that bit when Steve Jobs launches the iPhone.
[00:04:20] I don't know if you've watched that many many times. An object is slightly more underwhelming than the launch of the iPhone, I think I got some slight delusions of grandeur there. An object, well what do we need to store our functions? That we want this function when called with a new key word to return our objects for those objects to have a bond to some object full of functions, and we don't know where to put those functions.
[00:04:48] Because we can't put them in an object and give them a name like we used to. What if we, if we've got a function object combo, what if they were. Now actually what it turns out is when you declare a function, in other words a function-object combo, you actually get in that object automatically a property.
[00:05:09] Can anyone figure out what that property is? Seth, can you figure out what the property is?
>> Seth: Prototype?
>> Will Sentance: Prototype, it is just a regular property, people, it is not. You've heard the phrasing a thousand times from developers, the function's prototype, as though it's some special thing. No, it is the function in its object form of its function-object combo property prototype.
[00:05:32] It is nothing more magic than that, it is a regular property on the function in its object format, the function-object combo. One second, Neil, I promise. And that is automatically an object, and it's actually this big old empty object here that we might want to use to put all our functions in.
[00:05:59] Such that when I call user creator,
>> Will Sentance: When I call user creator with the new key word and it makes me my object inside automatically, we're gonna walk through all of this, so not fear. Makes me my object automatically inside. It's automatically gonna make its bond or we can't manually make its bond to something called user functionStore.
[00:06:21] Instead it's automatically, by the very nature the new key word, gonna make its bond to the name of the function in its object format on the prototype property, that big old empty object. Meaning we better put our functions in there that we want the object that gets returned out to have access to.
[00:06:39] So we're gonna walk through this line by line to make it really, really clear. All right, so we could use a father all functions have by default a property on their object version known as prototype, which is itself an object to replace our user function store object.