Activate your free membership today | Log-in

Thursday, June 28th, 2007

Cerny.js – Method call interception, Schemas, Type checking and more

Category: JavaScript, Library

<>p>Robert Cerny has created a JavaScript library in his own image Cerny.js.

The library has an entire slew of things:

One of the more out there ideas is for putting design by contract into your JavaScript world via something like this:

JAVASCRIPT:
  1.  
  2. var NewMath = {};
  3.  
  4. (function() {
  5.  
  6.     // Shorten some names
  7.     var check = CERNY.check;
  8.     var pre = CERNY.pre;
  9.     var method = CERNY.method;
  10.  
  11.     // The new division
  12.     function divide(a,b) {
  13.       return a / b;
  14.     }
  15.  
  16.     // For contracts to be enforced, it is necessary
  17.     // that a function is subject to interception. Therefore
  18.     // CERNY.method must be used.
  19.     method(NewMath, "divide", divide);
  20.     // The precondition for a division
  21.     pre(divide, function(a,b) {
  22.        check(b !== 0, "b may not be 0");
  23.     });
  24. })();
  25.  

A lot to check out here.

Related Content:

5 Comments »

Comments feed TrackBack URI

Is it friday already?

Comment by Sergej — June 28, 2007

Nope. Next!

Comment by Dan — June 28, 2007

This has actually been out for a while. Given the dynamic nature of JS though, it seems like some things should be a little more automatic and less verbose. That said there are a few interesting ideas in there.

Comment by Ryan Johnson — June 28, 2007

This is actually very interesting. Although all these ideas are not new, having them in one library is a good academic exercise.

Comment by Ahmed — June 28, 2007

perhaps good with java based javascript engine “Mozilla Rhino” :)
using java 6.0 Script engine.

Comment by nhm tanveer hossain khan (hasan) — June 29, 2007

Leave a comment

You must be logged in to post a comment.