When I run this code through jslint
(function () { return "helloooo"; }).call();
It gives me following error:
Unexpected '.'.
Why is that?
When I assign function to variable, and then call it I get no errors.
This:
var cb = function () { return "helloooo"; };
cb.call();
Returns no errors.
But, I would like to know why I get error in first place. What Douglas Crockford's sacred rule do I break with first example?
It is not a bad thing. JSLint is opinionated, and Crockford thinks that when you are using a function expression inside parentheses then you should either call that method directly or assign it to a variable, because someone else looking at it might get confused between the value of the function and the function as a value itself.
For this situation Crockford recommends:
(function () { return "helloooo"; }())
^^
Notice how is the function being called
So JSLint doesn't expect anything after the closing parentheses, that's why it said it didn't expect the .
which you are using to invoke call()
.
You can look at the Code Conventions for the Javascript Programming Language (by Crockford) to learn more about this, specifically look for the section about Functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With