Skip to content

Instantly share code, notes, and snippets.

@Magomogo
Last active August 29, 2015 13:56
Show Gist options
  • Save Magomogo/9002316 to your computer and use it in GitHub Desktop.
Save Magomogo/9002316 to your computer and use it in GitHub Desktop.
JS data types
$http.get('hostname/remote/service').success(function (data) {
// this way we document remote data type
var resultInterval = new DateInterval(data);
});
(function () {
'use strict';
module.exports = function () {
var state = arguments[0] || {};
return Object.seal({
id: state.id,
name: state.name
});
};
})();
var Hotel = require ('./Hotel.js'),
hotelArosa = new Hotel({
id: '123456',
name: 'Hotel Arosa'
}),
newHotel = new Hotel();
(function () {
'use strict';
function DateInterval() {
var state = arguments[0] || {};
Object.defineProperties(state, {
"from": {
value: state.from,
writable: true,
enumerable: true
},
"to": {
value: state.to,
writable: true,
enumerable: true
}
});
return Object.seal(state);
}
var i1 = new DateInterval(),
i2 = new DateInterval({from: '2014-01-02', to: '2014-01-03'});
// mutable
i1.from = (new Date()).toJSON();
i1.to = (new Date()).toJSON();
console.log(i1);
console.log(i2);
// serializable
console.log(JSON.stringify(i1));
// protected from accidental mistakes
console.log(i1.undefined);
i1.newProperty = {};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment