Book Image

Lo-Dash Essentials

By : Adam Boduch
Book Image

Lo-Dash Essentials

By: Adam Boduch

Overview of this book

Table of Contents (15 chapters)
Lo-Dash Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Binding function contexts


Every JavaScript function has a context. If you're coming from an object-oriented language, the function context is a lot like the object a method belongs to. The difference of course is that JavaScript doesn't classify objects in the object-oriented sense of the concept. Instead, functions are bound to a default context, and this can easily be changed at runtime. There are even built-in language mechanisms to make this happen.

Lo-Dash makes changing function contexts easy. We'll need to work with function contexts often when programming with Lo-Dash. We'll take a look at a number of approaches to working with and changing the context of functions now.

Changing the this keyword

Inside a function, the execution context is referred to by the this keyword. This is a special binding that we don't need to declare. It's always available to reference within a given function scope. It's important to keep in mind that it's entirely up to the caller, should he/she decide to...