-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use arrow function in declaration #103
Comments
Another use case for this would be if you want to use services as funcs instead of classes. function myService() {
return function() { /* do something */ }
}
// vs
class MyService {
do() {/* do something */ }
} I created a wrapper function myself to implement it: function fnService(container, name, serviceFactory, ...deps) {
container.factory(name, (c) => {
return serviceFactory(...deps.map(dep => c[dep]))
});
}
function myService(arg1) {
return () => {
// do something
}
}
fnService(container, 'myService', myService, 'someDep'); |
@jimhigson The documentation in the readme for
Take note of the the parenthesis around your function body here: bottle.service('foo', () => ({ doSomething: () => {} }) ) The parenthesis around the curly braces forces the code to be a one-line arrow function that returns an object literal. It is equivalent to writing it this way: bottle.service('foo', () => {
return { doSomething: () => {} };
}); So essentially you are passing in a function that constructs an object and returns it. This is what the bottle.factory('foo', () => ({ doSomething: () => {} }) ) Hope that helps! |
Ok, so the practical difference between a service and a factory is that a service is always created by a constructor, whereas a factory is created by a function? I understand that now, but since constructors and functions in Javascript are for the most part the same thing, I wonder if it would be better to unify them? |
@young-steveo Correct me if I'm wrong, but |
@jimhigson That practical difference is correct. My thoughts on unifying them: Under the hood bottle only has one mechanism for creating and returning services; it's the service provider. A provider is a Constructor that exposes a single factory method, When you call When you call So, no matter what function you use, bottle registers providers for you. Now, I could start allowing each method to accept a bag of different things (functions with a prototype, functions without, object literals, etc.) and then attempt to figure out how you want to create the eventual provider, but using specific methods allows you to tell me your intent. If you have something that returns a service, give it to me as a factory. If you have a construct-able service, give it to me as a service. If you want to make a provider yourself, give it to me as a provider. This keeps my code clean (no branching conditions) and there's no magic for the user to understand. @ragboyjr You are correct, the factory will receive the container. I can consider adding a new method to bottle that will take a factory that is injected with dependencies rather than getting the container, but I would have to be convinced that there is enough demand to justify it. |
@young-steveo totally understand, thank you for your time and work on this project. I'm willing to submit a PR for this feature as well, and if you can merge or close when you feel like a consensus has been reached. I'm sure i'm in the minority with using functional services vs objects, so I doubt this will be the next hot killer feature in bottleJS, but this would be quite convenient and encourage more functional(ish) style programming. |
@jimhigson I think the factory method should be able to solve your issue. Now, with the release of v1.7.0 you can also use the |
Under any environment that isn't transpiling arrow functions down to normal functions, this will fail:
The reason is because Bottle creates services using 'new' -
One solution might be to detect functions without prototypes (ie, arrow functions) -
Alternatively, updating the docs to explicitly state that traditional js functions are required would help a lot. Or maybe throwing an exception when the service is registered. It took a little digging to work out what was wrong here.
The text was updated successfully, but these errors were encountered: