ReSharper warnings for JavaScript naming conventions for constructor functions

In JavaScript, I like the PascalCase naming convention for constructor functions and camelCase for other functions. It seems that ReSharper is configured for these settings. However, for code like this:

function Thing(a, b) { return { prop1: a, prop2: b }; } var thing = new Thing(2, 6); 

... I get this warning:

The name "Thing" does not match the "Local Function" rule. The recommended name is "thing."

It doesn’t matter if I change Thing to this:

 function Thing(a, b) { this.prop1 = a; this.prop2 = b; } 

I suspect that only "public" functions are considered constructors. Do any of you know how ReSharper distinguishes between "Local" and "Constructor"? Better yet, do you know how to reverse this behavior?

+8
javascript naming-conventions resharper
source share
2 answers

Well, this is a bug in ReSharper. You can wait or fix it yourself. Or define it globally.

Btw, these functions do very, very different things. You definitely do not want to name the first constructor.

+9
source share

if you want your constructor to be publicly available, you can use this workaround:

 (function(global){ function kindOfPrivateFunction(thing){ //Do something with thing } global.Thing = function(a, b) { this.prop1 = a; this.prop2 = b; knindOfPrivateFunction(this); }; global.Thing.publicFunction = function() { }; })(this); 

Or, if you do not want resharper to complain about "ThisInGlobalContext":

 (function(){ function kindOfPrivateFunction(thing){ //Do something with thing } this.Thing = function(a, b) { this.prop1 = a; this.prop2 = b; knindOfPrivateFunction(this); }; this.Thing.publicFunction = function() { }; })(); 

Edit

Given that your class must be local, you can use a local object to "save" the local constructor. But it's really just a hack to disable Resharper ...

 (function(){ var local = {}; function kindOfPrivateFunction(thing){ //Do something with thing } local.Thing = function(a, b) { this.prop1 = a; this.prop2 = b; knindOfPrivateFunction(this); }; local.Thing.prototype.publicFunction = function() { }; var instance = new local.Thing(1, 2); })(); 
0
source share

All Articles