Can I create function parameters like string?

Asked

Viewed 117 times

6

I am trying to delve into functions and in the example of the site MDN Web Docs they have the following example:

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;

In that part of the code 'factit': function factorial(n) {} it was here that I did not understand, because in the codes that I see it is usually like this factit: function(){} which is the object parameter and the object value and in the example in that part 'factit' is a parameter, but is a string?

  • 2

    Every object key, in the background, ends up being converted into string, see more here and here

  • Thank you very much!

1 answer

7


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.

  • Maniero, but isn’t an object composed of parameters, values of parameters and methods? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object

  • @draw What the documentation calls parameters are the "things" that are passed to build an object. But note that these "things" are nameValuePairs, that is, name-value pairs (where "name" tb is called "key"). The name (the key) is always a string (otherwise it is converted to a string), and the value can be anything (numbers, string, a method, another object, etc.). But values are not necessarily methods

  • @hkotsubo is only misuse of the tool that was created to describe functions and used to describe the object, so as it did not have a ready section for this and had no way to create or had laziness used a ready made the documentation go wrong. That’s what I always say, to learn programming you need to learn to read documentation, not just to read it, but to interpret it and to do this you need to know the basics before you read the documentation and start programming for real, if the person cannot see that he has an error in the documentation, even if it is only ambiguity of definition, as the case

Browser other questions tagged

You are not signed in. Login or sign up in order to post.