TypeName: Difference between revisions
Jump to navigation
Jump to search
→Example: Add javascript snippet |
|||
Line 28: | Line 28: | ||
|} | |} | ||
== Example == | == Example (Basic) == | ||
<pre> | <pre> | ||
Line 38: | Line 38: | ||
nSingle = CSng(99.44) | nSingle = CSng(99.44) | ||
Print 99.44 & " is a/an " & TypeName(nSingle) | Print 99.44 & " is a/an " & TypeName(nSingle) | ||
</pre> | |||
== Example (JavaScript) == | |||
<pre> | |||
// TypeName Example | |||
/* TypeName returns variable type as a string */ | |||
function TypeName(o) { | |||
var nameTypes= { | |||
'undefined' : 'Undefined', | |||
'[object null]' : 'Null', | |||
'number' : 'Number', | |||
'[object Date]' : 'Date', | |||
'string' : 'String', | |||
'[object Error]' : 'Error', | |||
'boolean' : 'Boolean', | |||
'[object Array]' : 'Array' | |||
}, | |||
TOSTRING = Object.prototype.toString; | |||
if (nameTypes[typeof o] || nameTypes[TOSTRING.call(o)] || (o ? 'object' : 'null')==2) { | |||
if (o.toString().indexOf('.')>-1) { return 'Double'; } //JavaScript only deals with doubles, not singles | |||
} | |||
if (o==null) { return 'Null'; } | |||
return nameTypes[typeof o] || nameTypes[TOSTRING.call(o)] || (o ? 'object' : 'null'); | |||
} | |||
var nInteger, nSingle; | |||
nInteger = parseInt(44); | |||
NSB.Print((44) + " is a/an " + TypeName(nInteger)); | |||
nSingle = parseFloat(99.44); | |||
NSB.Print((99.44) + " is a/an " + TypeName(nSingle)); | |||
</pre> | |||
== Output == | == Output == |
Revision as of 04:07, 19 May 2013
TypeName(variable)
Description
TypeName returns a string specifying the type of a variable. The required parameter, variable, is any variable.
Table 24: TypeName return values
Return value | Description |
---|---|
Boolean | Boolean value |
Currency | Currency value |
Date | Date value |
Double | Double-precision floating-point value |
Integer | Integer value |
Object | Generic object |
String | String value |
Array | Array |
Example (Basic)
Rem TypeName Example 'TypeName returns variable type as a string Dim nInteger, nSingle nInteger = CInt(44) Print 44 & " is a/an " & TypeName(nInteger) nSingle = CSng(99.44) Print 99.44 & " is a/an " & TypeName(nSingle)
Example (JavaScript)
// TypeName Example /* TypeName returns variable type as a string */ function TypeName(o) { var nameTypes= { 'undefined' : 'Undefined', '[object null]' : 'Null', 'number' : 'Number', '[object Date]' : 'Date', 'string' : 'String', '[object Error]' : 'Error', 'boolean' : 'Boolean', '[object Array]' : 'Array' }, TOSTRING = Object.prototype.toString; if (nameTypes[typeof o] || nameTypes[TOSTRING.call(o)] || (o ? 'object' : 'null')==2) { if (o.toString().indexOf('.')>-1) { return 'Double'; } //JavaScript only deals with doubles, not singles } if (o==null) { return 'Null'; } return nameTypes[typeof o] || nameTypes[TOSTRING.call(o)] || (o ? 'object' : 'null'); } var nInteger, nSingle; nInteger = parseInt(44); NSB.Print((44) + " is a/an " + TypeName(nInteger)); nSingle = parseFloat(99.44); NSB.Print((99.44) + " is a/an " + TypeName(nSingle));
Output
44 is a/an Integer 99.44 is a/an Double