Fork me on GitHub

What Better.js Can Do For
Your Classes?


It provides features such private functions/properties 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.

Private Things Stay Private!

Your class has some properties or functions which are supposed to be internal only ? You declare them as private and better.js will make sure they do remain private!

View details

Strong Typing For Arguments

When your class is intentiated, it receives some arguments ? Better.js will ensure that they match the types you defined.

View details

Strong Typing For Properties

Your class got some properties ? You define the type they should have and better.js makes sure those rules are respected.

View details


Get A Class

Here is our example class. This is a nice little kitty class. There is a contructor which receives a name arguments, put that in a private (or intented to be) variable this._name and a getter function for private variable.

// Constructor of the class
var Cat = function(name){
  this._name  = name
  this.age    = 7
}

// getter for _name
Cat.prototype.getName = function(){
  return this._name
}

Make a Better.js for Your Class

We declare a better.js for it. We give the class various attributes for better.js. A Simple privatize : true. After that, all functions or property starting with _ are private.

// Here _name is accessible by everybody

// Make a Better.js for Cat
Cat = Bjs.Class(Cat, {
  privatize : true
})

// Here _name is now private
// Only Cat's functions can use it

// get a new cat
var cat = new Cat('kitty')

// access thru our getter - OK
console.log('My cat name is ', cat.getName())

// display "My cat name is kitty"

// direct access to private property - BAD
console.log('My cat name is ', cat._name);

// Exception thrown 
// "Denied access to private property _name"

Detect Unauthorized Access

Now let's use this Cat class. If _name is read thru the getter .getName(), all is ok. if _name is read directly, an exception is thrown.

Unauthorized access is immediatly detected and the execution is stopped.


Make a Better.js for Your Class

We declare a better.js for it. Our class can receive a name which is a String. From now on, when this class is instanciated, better.js will check arguments against this definition.


// Make a Better.js for Cat
Cat = Bjs.Class(Cat, {
  arguments : [String]
})



// get a new cat
var cat = new Cat('kitty')

// all is OK...

// get a another cat
var cat2 = new Cat(99)

// Exception thrown
// "Invalid arguments 0 - Should be String"

Detect Invalid Arguments

Now let's use this Cat class. If the first arguments name is passed as a String, it matches the better.js definition, all is OK. But if somebody tries to pass a Number instead, the unauthorized type is immediatly detected and the execution is stopped.

Make a Better.js for Your Class

We declare a better.js for it. Our class has a _name property a name which is a String and an age which is a Number.

// Make a Better.js for Cat
Cat = Bjs.Class(Cat, {
  properties : {
    _name : String,
    age : Number,
  }
})



// get a new cat
var cat = new Cat('kitty')

// Let's try to set cat age with a Number
cat.age = 14
console.log('My cat age is', cat.age)
// display 'My cat age is 14' ... all is OK...

// Let's try to set cat age with a String
cat.age = 'smoth'

// Exception thrown
// "Invalid property .age - Should be Number"

Detect Invalid Properties

Now let's use this Cat class. If we put a Number in cat.age, it matches our better.js, so all is ok. When we try to put a String in there, the unauthorized type is immediatly detected and the execution is stopped.

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

arguments

Description

arguments option declares type for the contructor arguments. Every time your constructor 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.Class(Cat, {
  arguments : [String, Number]
})

properties

Description

properties option will declare the strong typing for the properties of an instance of this class. It is a Object where each key is the property name, and the value is the strong typing definition for this property.

Values

js object It is a Object where each key is the property name, and the value is the strong typing definition for this property.

Examples

// Declare a Class
var Cat = function(){
  this.name   = 'kitty'
  this.age    = 7
}
// Make a better.js for it
Cat = Bjs.Class(Cat, {
  property  : {
    name  : String,
    age   : Number
  }
})

privatize

Description

privatize option will declare as private any property or function with a name starting by _. Thus they are no more accessible from outside your classes.

Values

true Enable privatize for this class
false Disable privatize for this class. Default to false

Examples

// define a class
var Cat = function(){
  this._name  = 'kitty'
}
// Make a Better.js for it
Cat = Bjs.Class(Cat, {
  privatize : true
})
// Now ._name is accesible only from inside the class
Fork me on GitHub