This is not a parameter, even less it belongs to a function. This is an object, if it were a parameter it would be in parentheses, for example the n
there in the function factorial()
is a parameter.
This is a member of an object, and yes, it can be used as a symbol in the code or as string, gives the same. So what you’re defining there is a member of an object whose type is a function (function
) and its value is just a function, perhaps from that comes the confusion, the function is there as the value and has nothing to do with what it is talking about.
A JS object is created as a array associative, a map, therefore in fact it is a structure that has elements with a key and a value. As it is an object all keys are strings same. There is a syntactic sugar that makes you not need to write the quotes or the object indexer.
Then the same code can be written like this:
var math = {
factit: function factorial(n) {
console.log(n)
if (n <= 1) return 1;
return n * factorial(n - 1);
}
};
math['factit'](3) //3;2;1;
I put in the Github for future reference.
Note that I have now reversed the use of syntactic sugar. Now in the field definition I did not use quotes in the name, but when calling the method inside the object I used the Apas and indexer to access the field. As the field is a function I might call it, but this is another mechanism, by coincidence it is there, but it could be the same thing with a value whatever is not a function.
The ideal is to always use syntactic sugar unless you have a very good reason not to do so which is almost always a mistake (there are a few legitimate advanced situations, but there is a lot of abuse). The example of the documentation could have made the statement of the object as I did here, but the call there is in the best shape even.
Every object key, in the background, ends up being converted into string, see more here and here
– hkotsubo
Thank you very much!
– user173282