I often like to say that JS is not my home language and that's led me to this question.
import Dropzone from "dropzone";
let myDropzone = Dropzone({
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 2, // MB
accept: function(file, done) {
if (file.name == "justinbieber.jpg") {
done("Naha, you don't.");
}
else { done(); }
}
});
The preceding code is from Dropzone website about their great file upload tool.
The else done() confuses me. How would you know that's a function if you were calling this? Done sports appears to be a parameter to me. If I'm using a good IDE, would I be directed as such? If not how do I know?
In Javascript, every function is an object, and can be used just like an object.
You can call
console.log(typeof done)
to see that the parameter is a function.
JS uses duck typing, which in this case means that the value of done is checked just before trying to call it. If it's callable, everything is good. If not, an error is thrown at runtime.
As to whether your IDE will tell you about this, that will depend on how good your IDE is at doing static typechecks, but I'd assume it's not a common feature. Duck typing is very permissive, making it generally difficult to determine whether some code will throw an error without running it.