javascript - Explanation for the "nonew" rule in JSHint -
there's rule/option in jshint i've never understood: nonew rule:
this alternative prohibits utilize of constructor functions side-effects. people phone call constructor functions without assigning result variable:
new myconstructor();
there no advantage in approach on calling myconstructor since object operator new creates isn't used anywhere should avoid constructors one.
here's don't understand:
what's illustration of "side effect" result invoking object constructor?
what's proper way invoke object constructor if have no need reference instance variable?
what meant saying there no advantage on calling myconstructor()
without new
? wouldn't create object intended.
to help elicit helpful responses, how should code below refactored instantiate object when there no farther need referencing instance after invoking constructor?
var module = require('./module'); (function (module) { new module({ el: '#module', tpl: '#tpl-module', status: false }); })(module);
what's proper way invoke object constructor if have no need reference instance variable?
don't invoke constructor if don't need instance.
what meant saying there no advantage on calling myconstructor() without new? wouldn't create object intended.
if purpose of phone call execute side effects, don't need create object. should utilize plain function (i'm tempted phone call "procedure"), instead of constructor, if need such.
in case constructor utilize instance (and binds somewhere), considered bad practise. constructor should create , initialise object. else should done in separate method.
javascript jslint jshint
No comments:
Post a Comment