Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Nodejs documentation

I am probably over thinking, but I am having trouble digesting the Nodejs documentation. I am new to javascript and come from a Java Background.

My question is not about any specific nodejs function just overall understanding. Below I give an example of what I am trying to understand...

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

public static void sort(int[] a)

However javascript is a dynamic language thus there are no types for api calls. Take this example in the 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 example code, or looking at the nodejs source how do I know the argument types of the function? I realized that it is possible to derive the types by looking at the name (ie callback is a function type) but is there any other way?

For example the documentation says that the callback gets two arguments err and derivedKey. What is the type of derivedKey, what is the type or err? Am I missing something about the documentation? How do you know if you are passing in the right types?

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

like image 714
Shane Avatar asked Jul 21 '13 19:07

Shane


People also ask

Is node JS documentation good?

Yes and No. It depends on your requirement. Node js documentation gives you a overview of the functionalities available. To make use of them with little to no knowledge of them, you can simply use the available npm modules or frameworks like Express, Hapi.

Can I learn node JS in 10 days?

It takes around 3 months to fully learn Node JS and be able to build a functional full-stack application. If you already know some other programming, you can get the basics down within a few week's time. This is because there are so many moving parts that go into building a working app such as a social network.

What are the basic concepts of node JS?

Node. js is an open-source runtime environment for server-side and networking applications and is single-threaded. It uses the Google JavaScript V8 Engine to execute code. It provides an event-driven architecture and non-blocking I/O that is optimized & scalable.

How do you explain node JS?

Node. js (Node) is an open source development platform for executing JavaScript code server-side. Node is useful for developing applications that require a persistent connection from the browser to the server and is often used for real-time applications such as chat, news feeds and web push notifications.


1 Answers

Well you are pretty much over thinking. You'll have to guess most of them if it's not explained explicitly. like you can guess iterations and keylen are numbers rather than strings. NodeJS docs explain parameters explicitly when they think you can't guess, or you have to know something additional about it. like in crypto.createCredentials(details) they explain that details is a dictionary and which keys you need to use. F.i. in case of err and derivedKey, since there is no explicit info, i would have assumed both are strings. If it turns out they are not, i would console.log them in callback function to see what they are.

Documentation could be a lot more clear if they have written down types of all parameters but don't know if it's worth the effort.

like image 157
Desu Avatar answered Oct 23 '22 08:10

Desu