new-struct

Structs inspired from Golang

Structs inspred from Golang

newStruct = require('new-struct')
 
Animal = newStruct({
  name: '',
  type: '',
  age: 0,
  run: run
})
 
function run (animal) {
  console.log('%s is running', animal.name)
}
 
dongdong = Animal('dongdong', 'cat', 4)
blackbear = Animal('blackbear', 'cat', 3)
 
dongdong.run()
// => dongdong is running 
 
blackbear.run()
// => blackbear is running 
$ npm install new-struct

You can pass the values as an object, too;

dongdong = Animal({ type: 'cat', foobar: 'dfjh' })

Methods can also be defined later;

Animal.method('jump', function(animal){
  console.log('%s is jumping!!', animal.name)
})

To extend an existing struct:

Cat = Animal.extend({
  type: 'cat',
  grrr: grrr
})
 
function grrr(cat) {
  console.log('grrrrrr')
}
 
dongdong = Cat('dongdong', 2) // notice how 'type' property got eleminated from parameter order.