Comments: Design-By-Contract in JavaScript, part 2

How will you add this to wikipedia's DBC article? It looks like Javascript is a glaring omission in http://en.wikipedia.org/wiki/Design_by_contract#Languages_implementing_DBC

(From Alex: Because I don't believe in tooting my own horn like that, I would rather someone else wrote the mention after reviewing my approach. Besides, my DBC implementation is somewhat incomplete, I think - lacking invariant methods for objects, for example.)

Posted by Mardeg at September 1, 2006 7:05 PM

Wow, you discovered closures. Congratulations. It's really mind-blowing what closures can do. Every time I think I understand just how amazing they are I learn about some other unbelievable thing that I never would have thought was possible.

I think your DBC stuff could be really great, but it needs more work. It's still a bit clunky. And what's the deal with "objectThatDoesAction.doAction()"? Don't get taken in by the Kingdom of Nouns.

(From Alex: Actually, I knew about closures several years ago. I probably knew about it when I wrote the JavaScript Developer's Dictionary's Function chapter in 2001. I just didn't know what the term was called.

I've thought about using closures to define private members, but it snowballs out of control very quickly.)

Posted by HeroreV at September 1, 2006 8:27 PM

Why bother with sqrtObject? just do |var sqrt = getContractFunction({…});| and then you can call sqrt(42);

(From Alex: In this case, yes, it isn't strictly necessary. This example was intended to demonstrate that the getContractFunction() call can be used to define methods on an object or class, with the full precondition/postcondition support.)

Posted by db48x at September 1, 2006 8:30 PM

Added a link on testdriven.com. Feel free to modify its description as needed.

Posted by Eric Vauter at September 6, 2006 9:41 AM

I developed an alternative approach to DBC in JavaScript. It makes use of my interception mechanism and simply attaches pre- and postconditions to the function. An interceptor checks whether the contracts are violated.

Posted by Robert Cerny at May 21, 2007 1:10 AM

I updated the Wikipedia entry to include both libs for DBC in JavaScript.

Posted by Robert Cerny at May 21, 2007 2:10 AM