Say we have this old-school JavaScript:
let obj = {
foo: function(){
// this === obj
},
bar: function(){
// this === obj
}
}
but if we want to attach some properties to foo and bar, like so:
obj.foo.x = function(){
// this !== obj
}
obj.bar.y = function(){
// this !== obj
}
what is the most performant pattern to use, to bind the "this" value within obj.foo.x and obj.bar.y to obj?
Sometimes we can reference obj directly instead of using this. However, if obj is the prototype of another object, then referencing obj directly will not yield the correct result. I need to use the this value here (I think).
In other words, this will not work:
obj.foo.x = function(){
}.bind(obj);
obj.bar.y = function(){
}.bind(obj);