Comments (8)
Would this suffice: https://github.com/srcagency/credential/blob/rewrite/src/index.js#L9-L14?
from credential.
No.
- Idiomatic Node. All core express / middleware are factories, not constructors.
- In ES6, calling a class without
new
throws a hard error. In ES5, calling a constructor withoutnew
causes lint warnings, and - Using
new
couples all callers to the constructor method of object instantiation, so you commit two sins every time you export a factory: force tight coupling where there could have been no coupling, and force more typing by every caller with no beneficial gain.
If been challenging people for years to write a good blog post explaining what constructors and classes provide that factories don't do better. So far that challenge has gone unanswered.
from credential.
You're saying this trick wont work anymore?
if (!(this instanceof Credential))
return new Credential(opts);
If instead a function was exported:
module.exports = function( opts ){ return new Credential(opts); }
Would that fit?
from credential.
Why use new
at all? Seems like all you're doing is opting into unnecessary complexity.
from credential.
I like prototypes :)
- I like the idea that when something is meant to be the same, it is actually the same. For instance if I have two objects that are supposed to have a shared property, I'd rather have them hold a reference to a shared object with the value than copying the value to both objects.
- I think it communicates the intentions of my code very well (readability)
- I think there could be a performance benefit
- Almost all of the top developers I try to learn from is using prototypes (e.g. bluebird, browserify and node itself)
- The only downside I can think of is the one line to have
new
be optional
from credential.
- The whole purpose of making a factory is so that there can be instance safety, which means that each instance needs its own values, which are likely to be different from each other. Shared state is a recipe for bugs.
- What's more readable, an object literal, or a whole bunch of disconnected assignments to
foo.prototype
? I'll take the object literal, thanks. - Factories that export object literals are much faster to instantiate than constructors that assign property values one at a time. How much faster depends on how many properties there are to assign. There is a very subtle benefit from hidden classes created by constructors for property access, but a tiny portion of the time spent running your program is executing JavaScript, and a miniscule portion of that tiny portion is spent accessing properties. On the slowest machines currently on the market, you can access properties millions of times per second without hidden classes. If you spend any time at all thinking about this, you're optimizing the wrong things.
- I've actually seen the numbers, based on GitHub projects. I wish I could remember the URL so I could link, but here's what's really the most common JS object instantiation strategies: Object literals (by a HUGE margin), Factories (regular function that returns an object, which most of the Node APIs do, and most of the popular libs like Express, jQuery, Underscore, etc...), THEN constructors, THEN ES6 class in dead last place. I expect ES6 class to jump to 2nd or 3rd place when more people adopt it.
- First, this is really a much bigger problem than people think. Class -> Factory is a common enough refactor that it made Martin Fowler's "Refactoring" catalog. The problem is, with radically different behavior depending on whether
new
and constructors are used, those refactors can be very hard. The problem with refactoring callers is that in many cases (libraries, public API's, etc...) it's impossible to know about all the callers, so you're left with no choice but to deprecate the class and create a totally new factory, and then you have the problem of educating users about the deprecation and new recommendation. This is really hard work and really expensive in both time and money.
One more thing, If I remember correctly, you can't have new
be optional with ES6 class
. );
from credential.
Reading:
- The Two Pillars of JavaScript: How to Escape the 7th Circle of Hell
- Fluent JavaScript: Three Different Kinds of Prototypal OO
- How to Fix the ES6
class
keyword - Stop Using Constructor Functions in JavaScript
- JavaScript Constructor Functions vs Factory Functions
from credential.
We still need a README update.
from credential.
Related Issues (20)
- I can't make the cli work HOT 8
- Verifying with a pre-parsed JSON object HOT 4
- What if an attacker know that I am using this library? HOT 9
- The use of "time" - a weakness worth noting? HOT 13
- Why hash() just return string rather than object? HOT 2
- fix failing CI server
- Does it really needs webpack? HOT 7
- Node v6 deprecation - "crypto.pbkdf2 without specifying a digest is deprecated" HOT 3
- Release v2.0.0 on npm HOT 3
- Support bcrypt hashing method? HOT 1
- performance optimisation HOT 4
- Make errors programmatically processable HOT 7
- Do not encourage people to write security issue in public places HOT 1
- Default number of iterations seems extreme HOT 2
- Due to the large number of iterations, Its consuming the full CPU usage. HOT 7
- Update deps, freshen code, release new major version. HOT 7
- Bad default settings HOT 4
- the 'verify' function takes too much time, about 1.3 seconds HOT 2
- Inconsistent use of bytes length/encodings? HOT 3
- Hash and Verify taking too much time HOT 1
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 credential.