Fork me on GitHub

What Better.js Can Do For
Your Functions?


It provides features such private or strong typing. Those feature were missing in javascript. Better.js make it possible and easy to use. It helps you find bug earlier, so write a better code.

Strong Typing For Arguments

When your function is calls, it receives some arguements. Better.js will ensure that they match the types you defined.

View details

Strong Typing For Return Value

Your function returns some value ? You define the type it should have and better.js makes sure those rules are respected.

View details

Private For Functions

Your function belong to a class and it is private. Tell that to Better.js and it will make sure your wishes are respected!

View details


Make a Better.js for Your Code

We declare a better.js for it. It receives a name string and a age Numbers as arguments. So we say that to Better.js

            
cat = Bjs.Function(function(name, age){
  console.log('my name is', name, 'and im', age)
}, {
  arguments : [String, Number],
})




cat('kitty', 5)
// display 'My name is kitty and im 5.'

cat('kitty', 'ten')
// Exception thrown
// "Invalid arguments 1 - Should be Number"


Detect Invalid Arguments

Now let's use this cat function. If age is not present, it works ok, same if it is a Number. But if age is passed as a string, Better.js immediatly detect the invalid argument and the execution is stopped.


Make a Better.js for Your Code

We declare our function with a better.js for it. It receives a name String and returns it. We tell better.js returns a String. We purposely dont say the arguments should be a string too. It makes it easier to test :)


cat = Bjs.Function(function(name){
  return name
}, {
  return : String,
})




console.log('the cat name is ', cat('kitty'));
// display 'the cat name is kitty'... All is OK...

console.log('the cat name is ', cat(99));
// Exception thrown
// "Invalid Return Value - Should be String"

// The error is immediatly detected by better.js!

// Perfect!

Detect Invalid Return Value

Now let's use this cat function. As we dont let better.js check the arguments, it is possible to receive any type. If the name returned isn't a String, Better.js immediatly detect the invalid return value and the execution is stopped.

Make a Better.js for Your Code

Say we got a Dog class, and it has two functions: a .sayPublic which is public and another .sayPrivate() which the same but is private. So you tell that to better.js in your class constructor.

// Constructor of the class
var Dog = function(name){
  this.sayPublic  = Bjs.Function(function(text){
    console.log('im publicly saying', text)
  };
  // this function and its better.js
  this.sayPrivate  = Bjs.Function(function(text){
    console.log('im private saying', text)
  }, {
    private : true
  })
}

// get a new dog
var dog = new Dog('brutus')

// Let's try to say something
dog.sayPublic('waf')
// display 'im publicly saying waf'... All is OK...

// Now let's try with .sayPrivate from outside
dog.sayPrivate('yapyap')

// Exception thrown
// "Denied access to private function sayPrivate"

// better.js detect unauthorized access! Perfect!

Detect Unauthorized Access

Now let's use this Dog class. If we use a .sayPublic() from outside, it matches our better.js, so all is ok. But When we try to call .sayPrivate() from outside, the unauthorized access is immediatly detected and the execution is stopped.

Features With Better.JS Without Better.JS
Detect Wrong Type for Arguments
Detect Wrong Type for Return Value
Detect Unauthorized access to private functions

arguments

Description

arguments option declares type for the function arguments. Every time your function is called, better.js will check the arguments are valid.

Values

a array of type It uses the same syntax as other strong typing features. See details on type definition.

Examples

// define the function
var Cat = function(name, age){
  console.log('my name is', name, 'and i am ', age)
}
// make a better.js for it
Cat = Bjs.Function(Cat, {
  arguments : [String, Number]
})

return

Description

return option declares type for the returned value of your function. Every time your function is called, better.js will check the return value are valid.

Values

a array of type It uses the same syntax as other strong typing features. See details on type definition.

Examples

// define the function
var cat = function(name){
  return name
}
// make a better.js for it
cat = Bjs.Function(cat, {
  return : String
})

private

Description

private option will declare your function as private as soon as it is called. Using Bjs.Class with privatize is recommended.

Values

true Enable private for this function
false Disable private for this function. Default to false.

Examples

// define a function
var cat = function(){
  this.john = 'smith'
  this._bar = 2

  // init this class for privates
  Bjs.Privatize.prepare(this)
  Bjs.Privatize.privatize(this)

  // declare a function function
  this.catPrivate = Bjs.Function(function(name){
    return name+this._bar;
  }, {
    private   : true,
  })
}
Fork me on GitHub