I understand below two usages in TS/ES6.
-
Below, a variable
myname
of typestring
which is going to be constant(final) is declared.const myname: string = ‘abcd’;
-
Below a variable/function
initialPath
is declared and is assigned a function body using the=>
operator.const initialPath = ({pathRoot, myvar = ”, locale = ”}) =>
path.join(process.cwd(), pathRoot, myvar, locale.toLowerCase());
But I am not sure of the below usage, what is :
in the below doing.
module.exports = {
application: myname,
myservicePostfix,
loadablePath: ({pathRoot, myvar}) =>
path.join(basePath({pathRoot, myvar}), 'loadable.json')
}
-> does application: myname
mean that application = myname
and application is of type typeof(myname).
-> similarily what does loadablePath:
above mean
2
Answers
The thing your confused about is simply a dictionary object. Here is another example of an object like yours:
As you can see, my
object
variable contains four elements, or things. Each of them can be called by writingobject.nameOfThing
.In your instance, you have a nested function as an element, the one that is called
loadablePath
. That is very much the same as mything3
element, which runs its function whenever it’s called:In simple words, yes. Just as
object.thing1: “hello”
means thatobject.thing1 = “hello”
,application: myname
means thatapplication = myname
.This is simply a dictionary object and that’s as far as it goes. Hope this helps.
There’s no TypeScript involved in your example. I get that some confusion may arise from the fact that TS uses colons to associate a type with a variable, but none of that is in play here. This is pure javascript.
In a javascript object literal, properties are declared with
key: value
:This is equivalent to:
The value can be any valid javascript type, including a function:
You can also use an arrow function:
The arrow function above can be inlined in the object declaration:
Putting this all together using your example: