最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ReSharper warnings for JavaScript naming convention on constructor functions - Stack Overflow

programmeradmin0浏览0评论

In JavaScript, I like the naming convention of PascalCase for constructor functions and camelCase for other functions. It looks like 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 receive this warning:

Name 'Thing' does not not match rule 'Local Function'. Suggested name is 'thing'.

It doesn't make a difference 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 differentiates between a "Local" and "Constructor" function? Even better, do you know how to override this behavior?

In JavaScript, I like the naming convention of PascalCase for constructor functions and camelCase for other functions. It looks like 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 receive this warning:

Name 'Thing' does not not match rule 'Local Function'. Suggested name is 'thing'.

It doesn't make a difference 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 differentiates between a "Local" and "Constructor" function? Even better, do you know how to override this behavior?

Share Improve this question asked Jun 25, 2013 at 18:10 JacobJacob 78.9k24 gold badges157 silver badges241 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9 +100

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

Btw, those functions do very, very different things. You definitely don't want to call the former a constructor.

if you want your constructor to be public, 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 don't want resharper to plains 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

Considering your class must be local, you can use a local object to "store" the local constructor. But that's really just a hack to shut off 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)

  1. 暂无评论