javascript - Lost the scope of of a function inside a object -
i have problem, i'm losing reference property of object ana when runs within event $(window).resize
as can keep scope.
example:
var testobject = { init: function () { this.divoriginal = $('.selector-tag'); this.resizenow(); }, resizenow: function () { //some code here.... //.... //.. $(window).resize(this.resizeupdate.bind(this)); //without bind(this) scope window }, resizeupdate: function() { console.log($(this)); var scrollwrapper = $(this)[0].divoriginal;//the way refer divoriginal scrollwrapper.css('border','1px solid red'); } } testobject.init();
there cleaner way access object's attributes?
you can access object using this
instead of $(this)
since bound object method called event handler.
see mdn docs on function.prototype.bind() :
the bind() method creates new function that, when called, has this keyword set provided value, given sequence of arguments preceding provided when new function called.
Comments
Post a Comment