6

I'm trying to implement an application using node.js and other related technologies. Heading from java land polymorphism but natural, but for the classical programmer node works differently.

The application will load new code during run-time provided by the user. In order for the main core to use this code "we" need to agree on some kind of a convention. Knowing how new Node is I wasn't that surprised that I didn't find the answer. The problem is this issue is rather vague in JS too.

Requirements:

  1. Strong decoupling.
  2. loading new code in run-time.
  3. The solution should be applicable so I can share as much code as possible with the browser.

Update:

  1. I did fiddle around with duck-typing, I've also encountered ideas from Clojure in regards to protocol based implementation.
  2. I would appreciate some code in the answer.
3
  • 5
    have you tried anything? show your efforts and / or share your own thoughts on the subjects. Commented Aug 3, 2012 at 14:39
  • 2
    1: gist.github.com/848184 2: loader-js: github.com/pinf/loader-js Or nczonline.net/blog/2009/07/28/… Commented Aug 3, 2012 at 14:54
  • 1
    Great links @LarryBattle Could you share opinion in answer? This is the main focal point of the app. Commented Aug 3, 2012 at 15:16

1 Answer 1

8

JavaScript, just like most other scripting languages (i.e. no compile-time type checking) does polymorphism through duck typing.

If you're from Java-land you're probably looking for Dependency Injection which generally provides uber decoupling. You can probably use google to find a good dependency injection framework for Node, like this one.

Although truthfully you can probably just make a single Javascript/Coffeescript file that does all the wiring and config loading.

Because of the flexibility of Javascript just about every form polymorphism has been implemented (traits, interfaces, inheritance, prototypes). Each have their advantages/disadvantages but almost all are runtime check (if any) and not compile time.

Personally I would probably just use either Coffeescripts inheritance, traits.js or Javascript's builtin prototype chain.

EDIT: However since you're talking about allowing users to extend the system then callbacks and/or custom events are the preferred approach (i.e. higher order functional programming and event-bus). If you're looking for something substantial like a plugin system then loader-js looks rather complete (tip of the hat to @Larry Battle).

Sign up to request clarification or add additional context in comments.

6 Comments

While googling I've also encounter protocol based polymorphism inspired by Clojure. Could you please share some more details on that?
@Eliran thanks for fixing my English :). I was too lazy to correct :)
Since this will run user code, I'm looking for the best way to give users an Interface.
Doc and plain duck typing is the best way :)
sure, no problem. nice answer BTW, i++.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.