Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does JSlint return an "Unexpected '.'." error

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?

like image 816
dugokontov Avatar asked Nov 11 '22 20:11

dugokontov


1 Answers

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.

like image 74
Ibrahim Najjar Avatar answered Nov 15 '22 04:11

Ibrahim Najjar