Renaming Python Names for JavaScript
In this lesson, you gave the release note’s names their JavaScript spelling, and saw why: JavaScript runs Python’s names, but its readers expect their own.
- A line-by-line port that keeps every Python name runs and prints the same header as Python. Underscores are allowed in JavaScript names, as the settings show. JavaScript doesn’t check a name’s style; whoever reads the file next does, and to them snake_case reads as Python in JavaScript syntax.
- For variables and functions, JavaScript uses camelCase where Python uses snake_case: the first
word lowercase, and each word after it capitalized.
open_bugsbecomesopenBugs, andprint_headerbecomesprintHeader, where it’s declared and where it’s called. - Classes, when you meet them, use PascalCase, like Python’s CapWords, and a JavaScript name can hold a dollar sign, which you’ll see in library names but shouldn’t use for your own variables.
- The six settings keep their capitals: many projects write top-level settings that someone edits in UPPER_SNAKE_CASE. Python enforces nothing, so there the capitals are the only signal not to change a value.
- In JavaScript,
conststops a name from being reassigned, soopenBugsis aconsttoo, in camelCase. The capitals only mark the settings, the values someone edits before a run. - In JavaScript,
defaultis a reserved word, kept for the language’s own syntax, as Python keepsifandclass.delete, also ordinary in Python, is another. Using one as a name stops the file with aSyntaxError, so the port names the client’s channeldefaultChannel. - JavaScript doesn’t mind underscores in a file name, but many projects use kebab-case, lowercase
words joined by hyphens:
release_note.pybecomesrelease-note.js.
Here’s the top of the Python script, with the header’s channel lines:
CLIENT = "Fernleaf Bakery"
VERSION = "2.4.1"
DEPLOYED_BY = "Maya"
BUGS_OPENED = 9
BUGS_CLOSED = 7
CODE_FREEZE = True
open_bugs = BUGS_OPENED - BUGS_CLOSED
def print_header():
print(f"Release {VERSION} for {CLIENT}")
print(f"Open bugs: {open_bugs}")
default = "#fernleaf-bakery"
print(f"Channel: {default}")
print_header()