Skip to content

Instantly share code, notes, and snippets.

@nishant8BITS
Forked from abhiomkar/LateBinding.js
Created October 9, 2018 11:08
Show Gist options
  • Save nishant8BITS/fa9f0fda702f3fc6095e8dc908e756d0 to your computer and use it in GitHub Desktop.
Save nishant8BITS/fa9f0fda702f3fc6095e8dc908e756d0 to your computer and use it in GitHub Desktop.
Early Binding & Late Binding in JavaScript
// Early Binding vs Late Binding
// Early Binding
var sum = function(a, b) {
return a + b;
};
var x = 5, y = 6;
var sum5n6 = sum.bind(null, x, y);
x = 10;
y = 5;
console.log("with Early Binding -->", sum5n6());
// Late Binding
var sum2 = function(p) {
return p.x + p.y;
};
var x = 5, y = 6;
var n = {x: x, y: y};
var sumLate = sum2.bind(null, n);
n.x = 10;
n.y = 5;
console.log("Late Binding -->", sumLate());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment