Comments (9)
This leads to an error in JSLint and a warning in JSHint, "Expected an assignment or function call and instead saw an expression”. As JSLint explains its expr
option:
This option suppresses warnings about the use of expressions where normally you would expect to see assignments or function calls. Most of the time, such code is a typo. However, it is not forbidden by the spec and that's why this warning is optional.
The warning is useful and worth enabling as it does catch typos like JSLint explains. The line you wrote, callback && callback();
is an expression and not a function call. This may be contrived, but take code similar to your example:
var shouldCallCallback = (Math.random() > 0.5);
shouldCallCallback && callback;
JSLint will give the same warning for the code, and this time it caught a typo: callback
is not actually being called, it's missing parentheses. This code would raise the same warning for the same reason:
var shouldCallCallback = (Math.random() > 0.5);
if (shouldCallCallback) {
callback;
}
The second example would likely be more quickly noticed by the developer, but again JSLint/JSHint is able to point it out. I find using an explicit if
more readable for the next developer too. The intention of the conditional in that case is unmistakable whereas callback && callback();
or something like it is not as straightforward.
There is nothing "wrong" with writing callback && callback();
in the sense that it will run just fine. However, I am a fan of keeping it out of the style guide because endorsing a pattern that looks the same as a typo to tools can lead to frustrating debugging and code that is harder to read for the next developer.
from javascript.
I don't think either one adequately covers safely attempting to run your callback
if(typeof callback === 'function'){
callback();
}
is what I use to be safe. I don't think a simple null check is a good idea especially when the arguments are variable or when you intend to use arguments[arguments.length - 1] convention:
function myFn(firstName, lastName){
var name = firstName + " " + lastName;
// Is the last argument a callback function?
var callback = arguments[arguments.length - 1];
// If it is, use it, otherwise, technically callback is just a string equivalent to lastName.
if(typeof callback === 'function'){
callback(name);
}
return name;
}
myFn("Keith", "Rosenberg")
// "Keith Rosenberg"
myFn("Keith", "Rosenberg", function(name){ console.log("Callback: " + name); });
// Callback: Keith Rosenberg
Otherwise:
function myFn(firstName, lastName){
var name = firstName + " " + lastName;
// Is the last argument a callback function?
var callback = arguments[arguments.length - 1];
// If it is, use it, otherwise, technically callback is just a string equivalent to lastName.
if(callback){
callback(name);
}
return name;
}
myFn("Keith", "Rosenberg")
// TypeError: string is not a function
from javascript.
I make such if's, a one liners:
if (callback) callback();
Still lint will scream, but I take such approach as much more justified (and readable) than hacky cb && cb()
.
from javascript.
+1
from javascript.
I think it is more readable to make an if
instead of this shorter statement, although it is not wrong!
Like @ssorallen pointed out!
from javascript.
I added a link to this discussion so folks can come back and read through this and add to the discussion.
from javascript.
I always prefer to use if statements while developing. When the project is built, uglifyjs will short-circuit the if statements to use the construct like yours.
from javascript.
What about callback?.()
?
from javascript.
@yisibl thats equivalent to if (callback != null) { callback(); }
, so that’d be fine (once eslint supports parsing that syntax, and once this guide permits it)
from javascript.
Related Issues (20)
- [spam]
- [spam] HOT 1
- Multiline comments HOT 3
- [spam]
- [spam]
- TypeError: Failed to load plugin 'jsx-a11y' declared HOT 4
- [spam]
- [spam] HOT 1
- Regarding Updation HOT 4
- [spam] HOT 1
- [spam]
- Link 404 not found HOT 2
- Can we add on quiz sections on this repo too HOT 11
- [spam]
- eslint v9 support
- [spam]
- import/no-extraneous-dependencies shouldn't be a code style rule HOT 25
- Parsing error: Unexpected character '#' HOT 3
- [spam] HOT 1
- [spam]
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javascript.