Bind

by Arnaud Buchholz

JavaScript

function log (text) {
	var line = document.createElement("div");
  line.appendChild(document.createTextNode(text));
  document.body.appendChild(line);
}

function getValue() {
  return this.value;
}

// Passing the context
log(getValue.call({
	value: "Hello World"
})); // output "Hello World"

// Binding
var boundGetValue = getValue.bind({
	value: "Bound"
});
log(boundGetValue());  // output "Bound"

// Trying to pass a different context
log(boundGetValue.call({
	value: "Hello World"
})); // output "Bound"

// Trying to bind again
var reboundGetValue = boundGetValue.bind({
	value: "Hello World"
});
log(reboundGetValue());  // output "Bound"