Strict Violation using this keyword and revealing module pattern

Having trouble getting the following to pass jslint/jshint /*jshint strict: true */ var myModule = (function() { "use strict"; var privVar = true, pubVar = false; function privFn() { return this.test; // -> Strict violation. } function pubFn() { this.test = 'public'; // -> Strict violation. privFn.call(this); // -> Strict violation. } return { pubVar: pubVar, pubFn: pubFn }; }()); myModule.pubFn(); I understand it's being caused by the use of `this` in a function declaration, but I read something Crockford wrote and he said the violation is meant to prevent global variable pollution - but the only global variable here is the one I'm explicitly defining... `myModule`. Everything else is held in the immediate function scope, and I should be able to use `this` to refer to the module. Any ideas how I can get this pattern to pass? **Update:** if I use a function expression instead of a declaration, this seems to work, ie var pubFn = function () { ... I'm not a fan of this format though, prefer to have the function name and named params closer and the declaration looks/feels cleaner. I honestly don't see why this is throwing the violation - there's no reason for it in this pattern.
Sounds like one of those cases where you can just ignore jslint's complaints. As an aside, how does the pubVar work when accessed as myModule.pubVar? It doesn't really give code outside the model access to the variable in the module does it? I would've thought that if you want to actually get/set the current value of the module's variable you'd need getter and setter functions.

以上就是Strict Violation using this keyword and revealing module pattern的详细内容,更多请关注web前端其它相关文章!

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

评论 抢沙发

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

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

联系我们

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

支付宝扫一扫打赏

微信扫一扫打赏