InvalidType
Thrown to indicate that the type of a value isn't expected.
- Source:
- InvalidType.js, line 40
Examples
function greet(name) {
var checked = InvalidType.check(name, 'string');
alert('Hello, ' + checked);
}
greet('John'); // "Hello, John"
greet(1234); // Throws: "unexpected type of value ..."
var value = 'ABC';
InvalidType.check(value, 'boolean', 'string', 'object'); // OK
class Beagle extends Dog {
...
}
var oliver = new Beagle();
InvalidType.check(oliver, Dog); // OK
InvalidType.check(oliver, Beagle); // OK
InvalidType.check(oliver, ShibaInu); // Throws
value = 123;
try {
InvalidType.check(value, 'string', 'object');
} catch (e) {
console.debug( e.info.checked ); // 123
console.debug( e.info.expected ); // ['string', 'object']
console.debug( e.info.actual ); // 'number'
}
Extends
ExceptionMembers
readonlyinfo any
Additional informations for debug.
- Inherited From:
- Exception#info
- Source:
- Exception.js, line 23
Methods
staticcheck(value, …expected) → any
Checks if the type of value
matches with expected
. If it does, just returns value
.
Otherwise, triggers
an exception.
The triggered exception holds value
and expected
as .info.checked
and .info.expected
.
And the actual type is stored in .info.actual
.
Parameters:
Name | Type | Attributes | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
value |
any | A value to check the type |
|||||||||||||
expected |
string | class |
<repeatable> |
The expected type(s) Available Types
|
- Source:
- InvalidType.js, line 77
Returns:
Just returns the value
argument if there's no problem
- Type:
- any
expects(type) → boolean
Returns if this exception expected type
.
Parameters:
Name | Type | Description |
---|---|---|
type |
string | class |
- Overrides:
- Exception#expects
- Source:
- InvalidType.js, line 47
- See:
- InvalidType.check
Returns:
- Type:
- boolean
trigger() → any
Throws this exception if handler
option is not set.
If handler
option is set, calls it and returns the result.
- Inherited From:
- Exception#trigger
- Source:
- Exception.js, line 41
- See:
- Exception.option
Throws:
This exception
Returns:
The result of option.handler
function
- Type:
- any