Skip to content

Instantly share code, notes, and snippets.

@codyromano
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codyromano/f510fb0d96e1a0f6756a to your computer and use it in GitHub Desktop.
Save codyromano/f510fb0d96e1a0f6756a to your computer and use it in GitHub Desktop.
function strong (obj, propertyName, type, initValue) {
var propertyValue,
typeErrorMsg = "Property " + propertyName + " must be a " + type;
if (initValue !== undefined && typeof initValue !== type) {
throw new TypeError(typeErrorMsg);
}
propertyValue = initValue;
Object.defineProperty(obj, propertyName, {
get: function () {
return propertyValue;
},
set: function (newPropValue) {
if (typeof newPropValue === type) {
propertyValue = newPropValue;
} else {
throw new TypeError(typeErrorMsg);
}
}
});
}
@AdrianCann
Copy link

That's my buddy putting the Java in JavaScript. Same guy that wears sunblock when he goes outside and brings earplugs to a concert. He is trying to make JavaScript as rigid as he is, people.

@AdrianCann
Copy link

Jk, nice! 😄 😊

@codyromano
Copy link
Author

It's not true that I wear sunblock to go outside. I just don't go outside.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment