When should I name things with initial capital letters?

后端 未结 3 1651
忘了有多久
忘了有多久 2020-12-29 06:29

I have always wondered when to use identifiers (for example, functions) with capital first letter instead of camel case. I always write my JS in camel case like thi

相关标签:
3条回答
  • 2020-12-29 07:03

    The convention is to name constructor functions (i.e. functions that will be used with the new keyword) with starting capital.

    function MyType(simpleVar) {
        this.simpleVar = simpleVar;
    }
    
    myObject = new MyType(42);
    
    0 讨论(0)
  • 2020-12-29 07:09

    The name convention states that class names are named with a first capital letter, I'm not sure how it's like with javascript, which is a prototype based language, but basically it's

    class ClassName
    var varName
    function funcName()
    
    0 讨论(0)
  • 2020-12-29 07:13

    According to the book "Javascript: the good parts", you should only capitalise the first character of the name of a function when you need to construct the object by "new" keyword.

    This is called "the Constructor Invocation Pattern", a way to inherits.

    0 讨论(0)
提交回复
热议问题