HTML and CSS Reference
In-Depth Information
...
app.utility.validator = {
...
/**
* Checks to see whether a value is a type of object
* Returns true if it is, or false if it isn't
* @param {Object} value
* @param {Object} type
* @return {Bool}
*/
isTypeOf: function(value, type){
if(value instanceof type){
return true;
}
return false;
}
}
This is great, but what happens when you want to check primitive types like
Booleans, strings, and numbers? Creating a new method just to check those
would be confusing and slightly annoying. To get around this, you can use the
typeof operator. This will check the type of a variable. As strings, numbers, and
Booleans are primitive types, you can check for these by allowing the type
parameter to accept a string. To do this, simply add the following code.
...
app.utility.validator = {
...
/**
* Checks to see whether a value is a type of object
* Returns true if it is, or false if it isn't
* @param {Object} value
* @param {Object} type
* @return {Bool}
*/
isTypeOf: function(value, type){
// First check to see if the type is a string
if(typeof type == "string"){
// If it is, we're probably checking against a primative type
if(typeof value == type){
return true;
}
} else {
// We're dealing with an object comparison
if(value instanceof type){
return true;
}
Search WWH ::




Custom Search