I’m testing one of my functions, and I want to test whether a specific function is called with the right argument from that code.
For that reason, I overwrite the original function like this:
var oldSort = sort;
sort = function(array) {
this.arrayToSort = array;
}
Then I execute it and check that the property sort.arrayToSort is set to the value I passed in and reassign the original function:
sort(myArray);
deepEqual(myArray, sort.arrayToSort);
sort = oldSort;
However, I can’t access sort.arrayToSort like that. How can I set a property of the function to make that possible? Right now, I’m declaring a variable above the sort function and have it set that, but I’d rather have it as a property of the function to keep its scope small. Is that possible?
Sounds like you want
arguments.callee, which refers to the function itself, rather than the object it was bound to.