Struct
generates classes to be used similarly to C's struct
construct. Inspired by Ruby's Struct class
Generate a Struct
class with the constructor
var Car = new Struct('make','model','year') // <#Class:Car>
var myCar = new Car('Chrysler','Cirrus',2000) // <#Car:myCar>
myCar.getMake() // 'Chrysler'
myCar.getModel() // 'Cirrus'
myCar.getYear() // 2000
myCar.setModel('Sebring') // <#Car:myCar>
myCar.getModel() // 'Sebring'
Undefined values will be ignored
var Car = new Struct('make','model','year') // <#Class:Car>
var myCar = new Car('Chrysler','Cirrus') // <#Car:myCar>
myCar.getMake() // 'Chrysler'
myCar.getYear() // null
myCar.setYear(2000) // <#Car:myCar>
myCar.getYear() // 2000
Optionally, one may set the prefixes for the getter and setter methods when generating the class
var Car = new Struct('make','model','year',{
getterPrefix: '',
setterPrefix: 'change'
}) // <#Class:Car>
var myCar = new Car('Chrysler','Cirrus',2000) // <#Car:myCar>
myCar.make() // 'Chrysler'
myCar.year() // 2000
myCar.changeYear(2010) // <#Car:myCar>
myCar.year() // 2010
An Array
may also be used to specify Struct
members
var Car = new Struct(['make','model','year']) // <#Class:Car>
var Car2 = new Struct('make','model','year') // <#Class:Car2>
Car.members() // ['make','model','year']
Car2.members() // ['make','model','year']
In the following examples, assume the following Struct
has been
defined
var Car = new Struct('make','model','year') // <#Class:Car>
Retrieve a hash representation of the Struct
and its contents
(new Car('Chrysler','Cirrus',2000)).toHash()
// {make:'Chrysler',model:'Cirrus',year:2000}
(new Car('Chrysler','Cirrus')).toHash()
// {make:'Chrysler',model:'Cirrus',year:undefined}
Retrieve the Struct
's properties
(new Car()).members() // ['make','model','year']
Passes the given function to the each
function of the hash
representation of the Struct
var cadillac = new Car('Cadillac','CTS',2010) // <#Car:cadillac>
cadillac.each(function(val,key){
console.log(key+' => '+val.toString())
})
// Console Output:
// make => Cadillac
// model => CTS
// year => 2010
Checks if two instances of a class generated by Struct
have the
same values for the available properties. Does not check that the
constructors are the same reference, but does check that each
object's constructor takes the same parameters. This ensures
duck-typing equality rather than type equality.
var myCar = new Car('Chrysler','Cirrus',2000),
cadillac = new Car('Cadillac','CTS',2010),
anotherCaddy = new Car('Cadillac','CTS',2010)
myCar.equals(cadillac) // false (unfortunately)
cadillac.equals(anotherCaddy) // true
Converts the Array
instance to a Struct
class, using the Array
entries as Struct
properties. Passes an optional Hash
/Object
to
the Struct
constructor as options
var Car = ['make','model','year'].toStruct({getterPrefix:''}) // <#Class:Car>
Retrieve the Struct
's properties
Car.members() // ['make','model','year']
There are currently no known issues.
I am always open for feature requests or any feedback. I can be reached at Github.
Thanks to the Ruby community for the original idea and implementation.