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) // [Object:myCar] console.log(myCar.getMake()) // 'Chrysler' console.log(myCar.getModel()) // 'Cirrus' console.log(myCar.getYear()) // 2000 console.log(myCar.setModel('Sebring')) // [Object:myCar] console.log(myCar.getModel()) // 'Sebring'
Undefined values will be ignored
var Car = new Struct('make','model','year') // [Class:Car] var myCar = new Car('Chrysler','Cirrus') // [Object:myCar] console.log(myCar.getMake()) // 'Chrysler' console.log(myCar.getYear()) // null console.log(myCar.setYear(2000)) // [Object:myCar] console.log(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) // [Object:myCar] console.log(myCar.make()) // 'Chrysler' console.log(myCar.year()) // 2000 console.log(myCar.changeYear(2010)) // [Object:myCar] console.log(myCar.year()) // 2010
An Array may also be used to specify Struct members
var Car = new Struct(['make','model','year']) var Car2 = new Struct('make','model','year') 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) 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.
A note on comments here: These comments are moderated. No comments will show up until they are approved. Comments that are not productive (i.e. inflammatory, rude, etc) will not be approved.
Found a bug in this plugin? Please report it this repository's Github Issues.
blog comments powered by Disqus