最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js how to implement polymorphism? - Stack Overflow

programmeradmin0浏览0评论

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.

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.
Share Improve this question edited Aug 3, 2012 at 14:48 qballer asked Aug 3, 2012 at 14:35 qballerqballer 2,1112 gold badges22 silver badges41 bronze badges 3
  • 5 have you tried anything? show your efforts and / or share your own thoughts on the subjects. – Eliran Malka Commented Aug 3, 2012 at 14:39
  • 2 1: gist.github./848184 2: loader-js: github./pinf/loader-js Or nczonline/blog/2009/07/28/… – Larry Battle 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. – qballer Commented Aug 3, 2012 at 15:16
Add a ment  | 

1 Answer 1

Reset to default 8

JavaScript, just like most other scripting languages (i.e. no pile-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 pile 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 plete (tip of the hat to @Larry Battle).

发布评论

评论列表(0)

  1. 暂无评论