Can JavaScript allow you to pass a function reference to a specific object's function, similar to what can be done in Java?
Let's take a look at this code snippet:
_.every(aS, function (value) {
return exp.test(value);
});
What if we want to achieve the same by doing this instead:
_.every(aS, exp.test);
This would result in calling the test
function of the specified RegExp
.
Is it possible to accomplish this in JavaScript?
Answer: Yes, it is indeed possible. Refer to chapter 2 of You Don't Know JS: this & Object Prototypes, specifically the section on Hard Binding.