Last active
August 29, 2015 14:08
-
-
Save felipecwb/6cc04e506c14e1ab228c to your computer and use it in GitHub Desktop.
How I write my javascript!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
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