javascript - Why does this prototypjs observe have a bind on the callback function? -


i ran pattern in project.

$('some_form').observe('keypress', (function(event) {     if (event.keycode == event.key_return) {         this.some_function()         event.stop();     } }).bind(this)); 

i want know why there .bind(this) on handler. in initialize function class.create() if helps. asking intent convert jquery.

without binding, scope (this) some_form element. since some_function isn't member of form element, wouldn't able call it.

with binding, scope containing scope, has access some_function.

here pattern solves issues scope, compatible super ancient browsers.

var _this = this; $('some_form').observe('keypress', (function(event) {     _this.some_function(); })); 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -