Comments (11)
The reason it says that is because the usage of the new keyword has been limited to
new SomeConstructor();
new SomeConstructor; or
new function() { };
Are both considered weird and "doing it wrong". This is part of the JSLint from crockford.
Imho anonymous function constructors is an abuse of the language and should be re factored.
from jshint.
The first "bad" pattern has no negative consequential effect. Belief that it is "wrong" usually comes from those who are not accustomed to that as being "normal". The other misthinking is that it must be an error becuase the call operator is not used. That is wrong because there is no call operator; it is new
that results in [[Construct]]. See also: http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/6bb13a70aa769e17/282acf2360d8a4ac
And so there is no harm in, for example: new Date;
The second production is useful for creating a singleton:
var mySingleton = new function() { var secret = 1234; this.getSecret = getSecret; function getSecret() { /* ... */ }; };
Other alternatives to this pattern may work fine. But that does not make this pattern inherently wrong. It can actually work fine. See also: http://www.highdots.com/forums/javascript/using-new-function-singleton-298119.html
A
from jshint.
Whoops, should have been 'function getSecret() { /* ... */ }'
from jshint.
the anonymous function constructor is easy to abuse and most of the time there is a better alternative pattern.
I personally find that it hinders readability more then it helps for non-trivial code snippets. It'd highly advice against it.
I do agree on the first matter I quite like new Constructor;
without the use of ().
There probably should be a flag along the lines of "tolerate weird constructors"
from jshint.
You said that most of the time there is a better pattern -- I agree. However using anonymous function in new expression is desirable for a few reasons mentioned in that thread I linked. I suggest reading that through to see others' perspective (though DM goes off the deep end (as usual)).
It's less common, but not weird -- perfectly normal to want to use that sometimes. Am I wrong, here:
https://github.com/GarrettS/ape-javascript-library/blob/master/src/anim/Animation.js#L306
from jshint.
Personally that makes me go wtf. I'd prefer the significantly more readable
manager = (function() { ... return { "register": register, "unregister": unregister }; }());
The worst thing is that you used a pascal case name to suggest a constuctor and then marked as @class
when it's clearly an object and not a constructor.
from jshint.
I see your point about using lower case. And I totally agree about the comments. Most of my more recent code does not sue comments at all.
Using new function() { }
is still legit. It's clear that the result of that must be an object. And if it makes you curse, even better .
from jshint.
The reason everyone prefers the (function() { ... return { ... }; }
over new function() { }
is that the latter is slower
from jshint.
This is an op that is run once. I don't agree on the performance reason here. And I believe that the reason JSLint (original) disallowed this was that it was a "weird construct" or something.
I left a comment there regarding using ReservedWord for Identifier but in browsers where it works, performance outcomes vary.
And performance outcomes cannot be expected. AYCS, the results vary depending on browser. Looks like new function(){};
is faster in somebody's "Chrome 11.0.672".
I think a better argument is whether or not it is a weird construct or what that means or what is wrong with new function(){}
.
from jshint.
I'm going to have to agree with you that both uses are valid. The new operator should not through a weird construct warning for either:
new Foo;
or
new function() { ... };
The former error is caused here. this should simply be fixed by removing the check.
Where as the latter error orginates from this. I think this is caused by the !== "function" check. If you remove this your going to have to add more logic to start parsing a function definition.
from jshint.
Since new Foo;
might as well be a typo by default JSHint will not tolerate neither that nor new
with anonymous functions. However, I made an opt-in option supernew
that disables those checks.
from jshint.
Related Issues (20)
- Getting errors while adding as dev-dependancy HOT 1
- JSHint doesn't recognize bitwise operators like & | >> etc... HOT 2
- is there any kind of way to get the console log outputs using this tool? HOT 1
- can i customize the error messages generated by JSHINT? is there any locale files i can override? HOT 1
- Not work in VS Code's Terminal HOT 1
- 'fetch' is not defined. (W117) - Although it is a base feature of Node 18 (although experimental) HOT 3
- Error: JSHINT is not defined HOT 2
- False error on static property HOT 1
- W080 incorrect message for initializing const with undefined HOT 2
- jshint doesn't expect regexp literals in one-liners following if/while/for conditions, and also else HOT 3
- `jshint` breaks on experimental feature `import * as file_json from 'file.json' assert { 'type': 'json' };` HOT 11
- JShint in VScode HOT 1
- If variable is assigned but never used it should cause warning HOT 1
- esversion beyond 11 HOT 2
- Generate error on specific object properties HOT 1
- jshint 2.13.6 is not warning me about mixing tabs and spaces HOT 1
- No warnings about undefined variable when doing typeof yyy
- VSCode HOT 1
- Jshint doest trigger hint when access non exist property of an object HOT 1
- "Invalid regular expression" when /s flag is used on long RegExps
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 jshint.