Created
February 3, 2013 06:00
-
-
Save rauschma/4700689 to your computer and use it in GitHub Desktop.
This file contains 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
// Proof of concept only! | |
function isInstance(value, Type) { | |
if (typeof Type.hasInstance === 'function') { | |
return Type.hasInstance(value); | |
} else { | |
return value instanceof Type; | |
} | |
} | |
isInstance.Primitive = { | |
hasInstance: function (value) { | |
if (value === null) { | |
return true; | |
} | |
switch(typeof value) { | |
case 'undefined': | |
case 'boolean': | |
case 'number': | |
case 'string': | |
return true; | |
default: | |
return false; | |
} | |
} | |
}; | |
isInstance.PrimitiveBoolean = { | |
hasInstance: function (value) { | |
return typeof value === 'boolean'; | |
} | |
}; | |
isInstance.PrimitiveNumber = { | |
hasInstance: function (value) { | |
return typeof value === 'number'; | |
} | |
}; | |
isInstance.PrimitiveString = { | |
hasInstance: function (value) { | |
return typeof value === 'string'; | |
} | |
}; | |
isInstance.ValueType = isInstance.Primitive; // for now | |
isInstance.ReferenceType = { | |
hasInstance: function (value) { | |
switch(value) { | |
case 'array': | |
return true; | |
case 'object': | |
return value !== null; | |
default: | |
return false; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment