Understanding Nodejs documentation

I probably thought, but I have problems processing Nodejs documentation. I am new to javascript and come from a Java background.

My question is not about any particular nodejs function, but just about a common understanding. Below I will give an example of what I'm trying to understand ...

When working with a statically typed language like Java, it is very clear which types are needed for method calls. A trivial example, if I want to sort an int array, I can just look at Arrays.sort and see that it needs int [] (the same for other types). I also see that it returns void.

public static void sort(int[] a) 

However, javascript is a dynamic language, so there are no types for api calls. Take this example in a crypto module

 crypto.pbkdf2(password, salt, iterations, keylen, callback) Asynchronous PBKDF2 applies pseudorandom function HMAC-SHA1 to derive a key of given length from the given password, salt and iterations. The callback gets two arguments (err, derivedKey). 

So, without going out and finding an example of the code, or looking at the source of nodejs, how to find out the types of function arguments? I realized that you can get types by looking at the name (i.e. the callback is the type of the function), but is there any other way?

For example, the documentation says that the callback receives two arguments err and a derivativeKey. What is the type of derivative key, what type or error? Am I missing something in the documentation? How do you know if you walk in the right types?

Note. I already know what the type of the derivedKey and err is, so I don't need answers like "generateKey is ....". My question is a general understanding of Nodejs documentation for someone coming from a statically typed language and not relevant to crypto.pdkdf2.

+4
source share
2 answers

Well, you almost changed your mind. You should guess most of them, unless explicitly explained. as you can guess, iterations and keylen are numbers, not strings. NodeJS docs explain the parameters explicitly when they think you can't guess, or you need to know something extra about it. as in crypto.createCredentials(details) , they explain that the details are a dictionary and which keys you need to use. FI in the case of err and derivedKey, since there is no explicit information, I would suggest that both are strings. If it turns out that this is not so, I would include them in the callback function to find out what they are.

The documentation can be much clearer if they record the types of all parameters, but don’t know if it is worth it.

+3
source

I have experience with C # and Java, and I have been programming JavaScript for about a year, so I could create this.

objects

One aspect of JavaScript is that you can create such objects in place:

 var options = { name: "something", age: 9, what: function() { return 8; } }; 

Everyone uses this, so this is a big key to understanding JavaScript libraries.

You can also take the above options object and then move on to the following:

 options.mood = "ok"; 

In other words, objects are simply property relationships, and the structure is not defined. You can use language constructs like the for ... in loop to go through them. That is, the β€œtype” of things like err is basically an associative array.

callbacks

Callbacks are mostly everywhere, so the question is how to deal with them. Generic function (err, maybeSomething) . In most cases, you don't care if err "something." That is, you will go so much:

 if (err) { ... } 

Honestly, I do a lot of console.log(err) to see what I get during development.

After that, this is valid until the documentation. Some of them are better than others. You have not missed anything. About the only "trick" is that sometimes the dock explains everything above.

You sometimes find yourself a source to find out exactly what the library does, but in 97% of cases, a few quick guesses and checks will make you move.

+1
source

Source: https://habr.com/ru/post/1492605/


All Articles