javascript - Why does JS report 'this.function' as not being a function? -
i wonder if please me understand seems odd js?
the below code works. function inlineeditevent.init()
called, , t.copy()
called correctly (where var t = this;
).
however, if replace this.copy()
, error this.copy not function
.
what's difference here? why below work, not way described in last paragraph? thanks.
jquery(function($){ $(document).ready(function(){inlineeditevent.init();}); inlineeditevent = { init : function(){ var t = this; /** copy row on click */ $('#the-list').on('click', '.row-actions a.single-copy', function(){ return t.copy(); }); }, // init copy : function(){ // stuff here } } // inlineeditevent });
this
refers this
within functions scope. that's why need set self
variable, it's accessible within scope of function. considering you're using jquery, use $.proxy:
$.proxy(function(){ return this.copy(); },this)
Comments
Post a Comment