2

I read in the chapter on timers in Chad R. Adams' book "Mastering JavaScript High Performance" (PacktPub) that capitalizing the name of a function indicates to the interpreter that it is a constructor.

It's the first time I've ever heard this. Previously I was led to believe that capitalizing functions was just a convention by programmers but now I'm not sure.

So does capitalizing affect how my code is interpreted at all?

Exact citation from the book:

Notice how we renamed Delay3000 on line 22, capitalizing the d. The purpose of this is to indicate to the JavaScript interpreter that this is a constructor, a function that requires it to be initialized in memory.

Chapter: Operators, Loops and Timers -> Timers

Rupert
  • 1,629
  • 11
  • 23
  • 2
    I've never heard of that. And since JS doesn't really _have_ constructors, I doubt it. – Cerbrus Nov 13 '15 at 07:36
  • 2
    No, its just a common convention. – Moob Nov 13 '15 at 07:38
  • 3
    maybe you could cite precisely the passage – Anonymous0day Nov 13 '15 at 07:38
  • Possible duplicate of [Javascript Method Naming lowercase vs uppercase](http://stackoverflow.com/questions/1564398/javascript-method-naming-lowercase-vs-uppercase) – Moob Nov 13 '15 at 07:39
  • 2
    _"We renamed `Delay3000`, capitalizing the "d" to indicate to the JavaScript interpreter that this is a constructor, a function that requires it to be initialized in memory. This is done by capitalizing the first letter in the function's name. You may recall from Chapter 2, Increasing Code Performance with JSLint, if we use a capitalized function name JSLint will return a warning that it "thinks" a constructor is being used even if it's a plain function. To keep our interpreter from second-guessing itself, we want to ensure we are writing our functions and objects as we intended."_ – Cerbrus Nov 13 '15 at 07:42
  • 4
    (Shortened a little to make it fit in 1 message). There are a couple of examples like that with similar explanations. It looks like the author mistakes _"Confirming to whatever the linter wants"_ with _"Performance improvements"_ – Cerbrus Nov 13 '15 at 07:45
  • 5
    Now i can advice you : bad book ! change book ! – Anonymous0day Nov 13 '15 at 08:00
  • Ah, yes I'm starting to think that myself. To be honest there is some stuff for me to learn there but I'm skipping anything that talks about constructors, and inheritance. – Rupert Nov 13 '15 at 08:02
  • 2
    Think of all the less glaring misconceptions that the book probably throws at you that you don't notice and just eat up. Not worth the risk, just change to a better book. – demiters Nov 13 '15 at 08:39
  • Point taken, adios Chad. – Rupert Nov 13 '15 at 09:02

1 Answers1

4

No, it makes no difference. The use of the new keyword does that.

Capitalization is a coding convention that indicates to the maintainer that it is a constructor function.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 3
    Technically, `new` doesn't say a function _is_ a constructor, but that it's used _as_ an constructor. – Cerbrus Nov 13 '15 at 07:38