Pages

Thursday, August 18, 2016

Sorry to throw this in the middle, but how the heck do you use bind? What is bind?

var dog = {
a:5,
b:10,
c:11
}

var cat = {
a:50,
b:100,
c:110
}

var fish = {
a:500,
b:1000,
c:1100
}

var bird = {
a:5000,
b:10000,
c:11000
}

var mammoth = {
a:50000,
b:100000,
c:110000
}

var howMany = function(){
console.log(this.a);
}

var howManydogs = howMany.bind(dog);
var howManyCats = howMany.bind(cat);
var howManyFish = howMany.bind(fish);
var howManybirds = howMany.bind(bird);
var howManyMammoths = howMany.bind(mammoth);

howManyMammoths();   will equal 50000
howManydogs();              will equal 5


And that's all there is to it.  Bind simply gives a context to 'this'.

No comments:

Post a Comment