You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Two variables can have the same name if they're declared in different scopes.
In the example below, the parameter x is said to "shadow" the variable x declared above it. The outer x can no longer be accessed inside the sum function.
const x = 1
function add(x, y) {
return x + y
}
While shadowing does not cause any problems most of the time, it does make the code harder to read and understand. We highly recommend against shadowing.
const r = repl.start();
Object.assign(r.context, {
BAD PRACTICE:
const file = "data.txt"
function readFile(file) {
// The parameter file shadows the toplevel variable file.
if (fs.existsSync(file)) {
return fs.readFileSync(file)
}
return null
}
RECOMMENDED:
// Prefer variable names that are distinct and convey as much
// meaning as possible.
const dataFile = "data.txt"
function readFile(filePath) {
if (fs.existsSync(filePath)) {
return fs.readFileSync(filePath)
}
return null
}
The text was updated successfully, but these errors were encountered:
'connection' is already declared in the upper scope on line 11 column 21.
[repl.js]
12 const config = mergeConfig(loadConfig('./config.js'));
13 const services = await createServices(config);
14
15 return withConnection(services.db, (connection) => {
Two variables can have the same name if they're declared in different scopes.
In the example below, the parameter x is said to "shadow" the variable x declared above it. The outer x can no longer be accessed inside the sum function.
const x = 1
function add(x, y) {
return x + y
}
While shadowing does not cause any problems most of the time, it does make the code harder to read and understand. We highly recommend against shadowing.
const file = "data.txt"
function readFile(file) {
// The parameter
file
shadows the toplevel variablefile
.if (fs.existsSync(file)) {
return fs.readFileSync(file)
}
return null
}
RECOMMENDED:
// Prefer variable names that are distinct and convey as much
// meaning as possible.
const dataFile = "data.txt"
function readFile(filePath) {
if (fs.existsSync(filePath)) {
return fs.readFileSync(filePath)
}
return null
}
The text was updated successfully, but these errors were encountered: