Lessons: 65Length: 7.1 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

6.2 Working With Functions

Functions are also objects, and as such they have their own properties and methods that we can make use of. Let’s take a look at some of the more common function methods.

Syntax

  • call
  • apply
  • bind

1.Introduction
2 lessons, 07:42

1.1
Introduction
02:12

1.2
Setup
05:30

2.Language Fundamentals
8 lessons, 1:00:53

2.1
Variables
06:33

2.2
Data Types
11:28

2.3
Arithmetic, Assignment, and Comparison Operators
10:24

2.4
Unary, Logical, Comma, and Spread Operators
09:02

2.5
Operator Precedence
03:50

2.6
Reserved Words
04:17

2.7
Strict Mode
04:34

2.8
Functions
10:45

3.Data Structures
5 lessons, 22:52

3.1
Arrays
04:29

3.2
Objects
04:30

3.3
Sets
04:57

3.4
Maps
04:21

3.5
Weak Maps and Weak Sets
04:35

4.Controlling Program Execution
7 lessons, 37:06

4.1
Conditionals
07:49

4.2
Switch Statements
04:41

4.3
The For Loop
06:39

4.4
The `for .. in` Loop
05:17

4.5
The `for .. of` Loop
04:02

4.6
Iterators
05:03

4.7
While Loops
03:35

5.Using JavaScript
13 lessons, 1:44:36

5.1
Working With Strings
09:32

5.2
Template Literals
05:46

5.3
Working With Numbers
06:57

5.4
Working With Arrays
12:53

5.5
Iterating and Transforming Arrays
07:33

5.6
Working With the Object Type
13:55

5.7
Object Literal Extensions
06:45

5.8
Working With Object Instances
06:45

5.9
Getters and Setters
05:00

5.10
Custom Objects
11:28

5.11
The `Math` API
04:54

5.12
Working With Dates and Times
08:10

5.13
The `Array` Constructor
04:58

6.Functions
8 lessons, 56:07

6.1
The `this` Object
06:15

6.2
Working With Functions
10:11

6.3
Scope
07:37

6.4
Arrow Functions
06:59

6.5
Generator Functions
08:13

6.6
Closures
05:00

6.7
Prototypes
06:26

6.8
Default and Rest Parameters
05:26

7.Miscellaneous
6 lessons, 52:39

7.1
Destructuring Assignments
08:09

7.2
AJAX
08:30

7.3
Regular Expressions
10:51

7.4
More About Regular Expressions
08:38

7.5
Classes
06:48

7.6
ES Modules
09:43

8.Working With the DOM
6 lessons, 37:39

8.1
Selecting HTML Elements
05:02

8.2
Manipulating HTML Elements
07:40

8.3
DOM Traversal
05:25

8.4
Adding and Removing Elements
04:45

8.5
Creating Elements and Other Nodes
04:39

8.6
DOM Events
10:08

9.Web APIs
4 lessons, 17:41

9.1
The Selector API
03:03

9.2
Geolocation
05:29

9.3
Web Storage
05:24

9.4
Web Workers
03:45

10.Asynchronous JavaScript
5 lessons, 26:23

10.1
Promises
09:52

10.2
Promise Chaining
05:11

10.3
The async Keyword
03:21

10.4
The await Keyword
04:04

10.5
More About async and await
03:55

11.Conclusion
1 lesson, 00:43

11.1
Conclusion
00:43


6.2 Working With Functions

Hi folks, in this lesson we're going to look at some of the properties and methods that functions have. Functions ultimately are objects as well, like most other things in JavaScript, and so they also come with a number of properties and methods that we can make use of. So let's have a simple test function first of all. So we have to find a simple function here which accepts two parameters, and it returns those two parameters added together. There's really only one properties functions that we'll ever be interested in and that is the length property. When used with a function, this property refers to the number of named parameters. Functions do have some other properties, but they are generally unavailable in all browsers, and so they're not recommended for general use. Let's move on now to look at some of the methods that functions have. Functions don't have a huge number of methods, but the ones that they do have are extremely powerful. These are the call, apply and find methods. And they can all be used in very similar ways with just some subtle difference between them, especially call and apply. The call method is used to invoke a function and specify the objects that should be used the functions this objects. So let's just add a new object first of all. So we've added a regular object literal in the variable person and we've given it name and job properties. Let's set a new function now called introduce. So this function just logs an introductory message to the console but it makes use of this .name and this .job as part of the message. So if we were to invoke the introduce function normally just by adding it's identifier and a pair of parenthesis, this would be undefined inside the function. What we can do instead is invoke this function using the call method. So, let's see the output in the browser's console now. It works in the way that we were probably expecting. So what's happened is, when we use the call method, we can specify what the this object, inside the function that we're calling should point to. So in this case, we are using the call method with the introduce function and we're saying that inside the introduce function, the Vis object should point to the person object literal. So the first argument that we pass to the call method is the object that we want to use for this. But we can also parse any number of additional arguments, and these arguments will be parsed to the function being invoked as arguments. So let's just add a named parameter to the introduce function. So this time the introduce function accepts a grammatical inductee and we've added that to the message that gets logged to the console. So now if we want to pass in the name of an inductee, we can add a second argument when we use the call method. So what's happened is when we use, and we can see that the argument has been passed through in the way that we would expect. So don't forget, we can pass as many additional arguments as we want using the call method, and they are just passed as a comma separated list. So this argument passing is actually where the difference between the call and the apply methods emerge. Everything about the apply method is the same as the call method, it invokes the function and allows us to specify the binding for the this objects inside the function that apply is used with. So at the moment we aren't passing any arguments and so we see undefined as part of our string. But you can see that so far it's working in the same way that the comma had worked. So now let's work at passing arguments. And now we see the same message as before. So the difference between call and apply is how we pass arguments. The apply method only takes two arguments. The first argument is the objects that should be used for this insider function, and the second argument is an array containing all of the arguments that we want to pass to the function that we're applying. This is useful when we don't know how many arguments we're passing to the function. This makes the apply method very flexible because we can pass an unknown number of arguments to the function that is being invoked. There is a great neumonic to remember the difference between the co and apply methods. That neumonic is as follows, C for call, A for apply. C is for comma because when we use the call method the arguments are passed as a comma separated list. A is for array, because the arguments passed to apply a passed as an array. C for call, A for apply, as long as you remember that, you will always remember how to use the call and apply methods, and what the difference between them is. So, before we finish up in this lesson, let's take a quick look at the bind method. The bind method also allows us to control the value of a function's this object. But this method returns a new function and the this object that we specify is the this object for the function that bind returns. So bind itself doesn't invoke the function that its called on. Let's look at a simple example. The function that we've defined, addToCart, receives an argument called price, which will add to a running total. We check whether the this object has a property called total and if it doesn't, we add one and initialize it to zero. The function then updates the total with the number passed into the function and, lastly, returns a string showing the name of whose cart it is and the current total. So now we have to bind the function to an object to use as it’s this object. We call the bind method on the addToCart function and specify the person object that we used earlier as an argument. So the person object will become the functions this objects. So now we can invoke our function and pass it in a value. And we can invoke it later again if we want. So let's take a look in the console to see what output we're getting. We can see what we're seeing undefined at this point. And the reason we're seeing undefined is because I've used this .price instead of this .total. Let me just fix that quickly. And now it's working as we expect. We can see that the total property keeps on being updated. This is quite useful. So when we use the bind method, JavaScript returned a function that we can invoke. And every time we invoke that function the this object will always point to the object that we passed into bind. We can also specify arguments that may be passed when the bound function is invoked. Maybe we want to add a handling fee to each item added to the cart, which is the same each time the function is called. We can update the function to accept a fee. And now when we bind to the addToCart function we can specify the default value for this fee. And let's now check the browsers output. So we can see that each time the function has been invoked here as simply added one to the total. So in this lesson we looked at the different properties and methods that we can use with functions. We first looked at the length property which simply tells us how many name parameters a function accepts. When then moved on to look at the call, apply, and bind methods which can all be used to set the this object or the context of a function. We saw that call and apply are both very similar. They only differ in that with call we need to specify each argument independently. Whereas with apply we can pass an array containing all of the arguments that we want to pass to the function. Lastly we looked at the bind method, which returns a new function that has this object bound to a particular value, and it retains this each time the returned function is invoked. In the next lesson, we're going to learn all about Scope in JavaScript. Thanks for watching.

Back to the top