Skip to content

Instantly share code, notes, and snippets.

@felipecwb
Last active August 29, 2015 14:08
Show Gist options
  • Save felipecwb/6cc04e506c14e1ab228c to your computer and use it in GitHub Desktop.
Save felipecwb/6cc04e506c14e1ab228c to your computer and use it in GitHub Desktop.
How I write my javascript!
(function (global) {
"use strict";
// namespace
global.Space = {
entities: {}
};
// class
global.Space.entities.UserEntity = function (theName, theAge) {
// private properties
var name,
age;
// construct
this.construct = function (theName, theAge) {
this.setName(theName);
this.setAge(theAge);
};
// public methods
this.setName = function (theName) {
if (
! theName instanceof String
|| ! (typeof theName === 'string')
) {
throw new TypeError("Name must be a String!");
}
var l = theName.length;
if (l < 3 || l > 60) {
throw new RangeError("Name must be between 3-60!")
}
name = theName;
}
this.getName = function () {
return name;
};
this.setAge = function (theAge) {
if (
! theAge instanceof Number
|| ! (typeof theAge === 'number')
) {
throw new TypeError("Age must be a Number!");
}
if (theAge < 15 || theAge > 120) {
throw new RangeError("Age must be between 15-120!")
}
age = theAge;
};
this.getAge = function () {
return age;
};
// call constructor
this.construct(theName, theAge);
};
})(global);
// ...
JavaScript.loadScript('codeStyle.js');
// Use
// another Scope
(function () {
"use strict";
try {
var user = new Space.entities.UserEntity("Felipe", 20);
console.log(user.getName());
console.log(user.getAge());
} catch (error) {
console.log("Exception:");
console.log(error);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment