How can I maintain control of the this keyword when extending prototypes in jQuery?

I'm implementing a class-like structure in jQuery, but I'm having some trouble when I try to call some of my functions. This is how the structure is setup: MyClass = function(name) { this.init(name); } $.extend(MyClass.prototype, { init: function(theName) { this.myFunction(); // works $('.myclass').each(function(){ this.myFunction(); // doesn't work }); }, myFunction = function(){} }); The problem I'm having is that when I try to call one of my functions (e.g., `myFunction()`) from inside a jQuery block (like the `each()` construct above), I get the error "`myFunction() is not a function`." I think this has something to do with the `this` keyword changing its meaning inside the jQuery block, but I'm not sure. Any help would be appreciated!
@Bergi so this question is a duplicate of a question that was posted more than 4 years after it? Nice.

以上就是How can I maintain control of the this keyword when extending prototypes in jQuery?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » jQuery 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏