Expected an assignment or function call and instead saw an expression

804 views
Skip to first unread message

Joyston Colaco

unread,
Feb 20, 2015, 1:57:24 AM2/20/15
to jsh...@googlegroups.com
For this code
(name !== '') ? $('#id').click(function() {  }) : $('#id').click(function() {  });
$('#id').css('cursor','pointer');

I get below error
'Expected an assignment or function call and instead saw an expression.'

What is wrong in above code and what is the correct way to use?

James Pittman

unread,
Feb 20, 2015, 8:00:21 AM2/20/15
to jsh...@googlegroups.com
I assume the two functions are different and what you wrote was just an example.

I think it's saying that ternary operators should only be used when setting a variable - not as a shortcut to performing different processing.  For example (and I haven't tried this - I'm just guessing)  the following processing would be valid:

var a = (name === '') ? "Name not set" : "Name set";

For your example, you could do the following:

var fn = (name !== "") ?
        function() { // do one thing }  :
        function() { // do something else };
$("#id").click(fn);

or even:

$("#id").click( (name !== "") ?
        function() { // do one thing } :
        function() { // do something else }  );

Or, just keep it simple (and more readable) by doing the following:

if (name !== ) {
    $("#id").click(function() {  // do one thing } );
} else {
    $("#id").click(function() { // do something else } );
}

Jamie



--
You received this message because you are subscribed to the Google Groups "JSHint" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jshint+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
____________________________________________________________________________________
Taste and see the fullness of His peace, and hold on to what's being held out.
~ from "The Healing Hand of God" by Jeremy Camp
Reply all
Reply to author
Forward
0 new messages