Archive for October, 2010

Scoping in javascript

Monday, October 11th, 2010

Not sure why I don’t see more addressing this on the internet, but when you make an object in javascript and want to call the methods it contains via event listeners all the method calls fall out of scope, making instance variables out of scope as well. I have done all kinds of weird things to get around this in the past, until one of my coworkers set me up with this method:

function CreateDelegate(object, method) {
return (function() { return method.apply(object, arguments); })

Nice to have easy to use and light. I don’t need to import any external libraries.