gobwas / dm.js

Javascript Dependency Injection Manager
MIT License
108 stars 4 forks source link

Add syntax to call functions without `new` keyword` #40

Closed franza closed 9 years ago

franza commented 9 years ago

Can we add a syntax which won't handle a function like a constructor? Maybe, a flag like instantiate which is true by default? It will say how we need to treat the dependence if it is a function.

Or we can think of a parameter useAs which has next values: constructor (by default), function (to call function) and asIs (to use dependency as is).

gobwas commented 9 years ago

Was thought about it yesterday, and not found any reason to reject this feature. Will implement it soon, as optional value of factory definition property. It will be one of service (default), function, object, primitive. Also, look at this issue - its about using the same feature without definition in config.

gobwas commented 9 years ago

Hey @franza!

This feature released as 0.3.2-rc0 at now. As before, install dm@next tag.

Report if some wrong!

Its now three variants: constructor, function and proxy.

gobwas commented 9 years ago

@franza ping

franza commented 9 years ago

@gobwas sorry about the delay, I'm little stuck on some urgent things in my project but I'm still interested in testing this. I hope to have time on this week.

gobwas commented 9 years ago

@franza Cool, let me know when you got this =)

franza commented 9 years ago

Hey there! I've finally tested the changes you did and it looks fantastic! You can check out the things I did here. Also, a question here, why is the third option called proxy?

gobwas commented 9 years ago

Great =)

Proxy is a factory, that does nothing on required target - basically, it just returns result of loading path (require in node). It also, as other factories, could set properties and/or make calls on result of loading. If your path points to the module.exports = {primitive type} the Error will be occurred.

franza commented 9 years ago

Yeah, I see. I tested that and it works as expected, just wandering why you've picked a name like proxy. Never mind :)

gobwas commented 9 years ago

May be you could propose any better name for this? =)

franza commented 9 years ago

Hard to say. I think I'd to continue function, constructor sequence I'd suggest object. That will also reflect that module.exports = {primitive type} won't work. Still I cannot think of a good name either.

Speaking about proxy - does the name refers to the Proxy pattern?

gobwas commented 9 years ago

No, it is not related to this pattern. And module.exports = {primitive type} should work. Only calls and property settings on exported value will fail.