Fork me on GitHub

What Better.js Can Do For
Your Properties?


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

When your property is used, it receives some values. Better.js will ensure that they match the types you defined.

View details

Private

Your property 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 an Object in Better.js

We declare a object with a bar property which is a String. You tell that to better.js and it will make sure it is only String.

// define an object
var foo = {
  bar : 'bla'
};

// Make a Better.js for it
Bjs.Property(foo, 'bar', {
  type  : String,
})

console.log('the value is', foo.bar)
// display 'bar'... All is OK

foo.bar = 'smith'
console.log('the value is', foo.bar)
// display 'smith'... All is OK

foo.bar = 99
// Exception thrown
// "Invalid Type for 'bar' - Should be String"

Detect Invalid Values

Now let's use this foo object. You can read foo property without issue. You can set it as long as it is with a String. If You set it with Number, it is immediatly detected by Better.js and the execution is stopped.

Make a Better.js for Your Code

Say we got a Dog class, and it has two property: a .age which is public and another _name which is private. So you tell that to better.js in your class constructor.

// Constructor of the class
var Dog = function(name){
  this._name = name
  this.age = 3
  // make a better.js for _name
  Bjs.Property(this, '_name', {
    private : true
  })
}

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

// Let's try to use a public property
console.log('my dog age is', dog.age)
// Display 'my dog age is 3'... All is OK

// Let's try to use _name from outside
console.log('my dog name is', dog._name)

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

// better.js detect unauthorized access! Perfect!

Detect Unauthorized Access

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

Features With Better.JS Without Better.JS
Detect Wrong Type for Properties
Detect Unauthorized access to Private Properties

type

Description

type option declares type of this property. Every time this property is used, better.js will check its type remains valid.

Values

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

Examples

// define an object
var foo = {
  bar : 'bla'
};

// Make a Better.js for it
Bjs.Property(foo, 'bar', {
  type  : String,
})

private

Description

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

Values

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

Examples

// Constructor of the class
var Dog = function(name){
  this._name = name
  this.age = 3
  
  // init this class for privates
  Bjs.Privatize.prepare(this)
  Bjs.Privatize.privatize(this)

  // make a better.js for _name
  Bjs.Property(this, '_name', {
    private : true
  })
}
Fork me on GitHub