rares (owner)

Fork Of

Revisions

gist: 74664 Download_button fork
public
Public Clone URL: git://gist.github.com/74664.git
Embed All Files: show embed
improved_binder.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// This is a super simple utility function that I've found very powerful
// when compared to the vanilla binding pattern.
 
function bind(target, func) {
  var binding;
  // Unwrap previous bindings
  if ($.isFunction(func.method)) func = func.method;
  // Generate a new binding
  binding = function() {
    // Picked up environment via closure which will use the
    // returned function object itself (kind of y-combinatorish)
    return binding.method.apply(binding.receiver, arguments);
  };
  // Setup the binding's actual parameters respectively
  binding.receiver = target;
  binding.method = func;
  return binding;
}
 
// NOTE: The receiver is also dynamic in this case. We could use the closure and apply
// using target but that disallows other cool tricks. I'll benchmark sometime and see if
// it is worth allowing receiver reflection on the modern JS implementations. Any cool ideas
// of what I could do with the receiver that I can't do with rebinding?