I want to use an object in nodejs to store url paths, and reuse a previous entry for example:
let URLS = {
root: "www.example.com",
paths: {
user: root + "/user",
login: root + "/login"
}
}
is there a way to achieve this?
I want to use an object in nodejs to store url paths, and reuse a previous entry for example:
let URLS = {
root: "www.example.com",
paths: {
user: root + "/user",
login: root + "/login"
}
}
is there a way to achieve this?
2
Answers
You cannot access
URLS.root
before the object is created, but you can define a separated variableroot
and use that in multiple places in theURLS
object:In the example above, the
root
property is defined using a shorthand property names instead of writing the property name and the property value separately (e.g.{ root: root }
).You need to slightly modify your code because you can’t directly use
root
inside thepaths
object since it’s defined within theURLS
object. Instead, you should refer tothis.root
orURLS.root
. Here’s the corrected code: