472,133 Members | 1,167 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

checking type?

Howdy All!

How can you tell the type of an object?

I am not talking about using "typeof" ..

I am talking about being able to determine if an object is a Date or String
....

Thanks for any advice!

Rob
:)
Jul 20 '05 #1
8 94876

"Robert Mark Bram" <ro*********@yourshoesinfotech.monash.edu.au> schreef in
bericht news:3f**********************@news.optusnet.com.au ...
Howdy All!

How can you tell the type of an object?

I am not talking about using "typeof" ..

I am talking about being able to determine if an object is a Date or String ...


You could test for methods:

function test (obj) {
var type = typeof obj;
if (type == 'object') {
if (obj.getDate) return 'Date';
if (obj.split) return 'String';
return object;
}
return type;
}

alert(test('test')); // Alerts 'string'
alert(test(new String('test'))); // Alerts 'String';
alert(test(new Date())); // Alerts 'Date';
JW

Jul 20 '05 #2

"Janwillem Borleffs" <jw*@jwbfoto.demon.nl> schreef in bericht
news:3f***********************@news.euronet.nl...

function test (obj) {
var type = typeof obj;
if (type == 'object') {
if (obj.getDate) return 'Date';
if (obj.split) return 'String';
return object;
}
return type;
}


Typo here, the statement:
return object;

should be:
return 'object';

JW

Jul 20 '05 #3

"Janwillem Borleffs" <jw*@jwbfoto.demon.nl> schreef in bericht
news:3f***********************@news.euronet.nl...

"Janwillem Borleffs" <jw*@jwbfoto.demon.nl> schreef in bericht
news:3f***********************@news.euronet.nl...

function test (obj) {
var type = typeof obj;
if (type == 'object') {
if (obj.getDate) return 'Date';
if (obj.split) return 'String';
return object;
}
return type;
}


And to make it more compact:

function test (obj) {
var type = typeof obj;
if (type == 'object') {
if (obj.getDate) return 'Date';
if (obj.split) return 'String';
}
return type;
}
:-)

JW

Jul 20 '05 #4


Robert Mark Bram wrote:
Howdy All!

How can you tell the type of an object?

I am not talking about using "typeof" ..

I am talking about being able to determine if an object is a Date or String
...


The strings you encounter in JavaScript are not usually objects at all
but primitive string values. And
typeof varName == 'string'
tells you whether something is a string value.
As for Date objects
if (typeof varName == 'object' && varName.constructor == Date)

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #5
"Robert Mark Bram" <ro*********@yourshoesinfotech.monash.edu.au> writes:
How can you tell the type of an object? I am not talking about using "typeof" ..
Yes, that would just give "object".
I am talking about being able to determine if an object is a Date or String


All built in constructors (String, Boolean, Number, Array, Date, RegExp, etc.)
have a prototype with a property called "constructor" that points to itself.

That is
var x = new String("foo");
alert(x.constructor == String);
alerts true. You can use this to recognize the objects constructed by these
constructors *unless* someone fiddled with the constructor property. There
is nothing that prevents you from doing:
String.prototype.constructor = Array
(except common sense). So, the method is not fool proof, if the fools are
too inventive.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6
"Janwillem Borleffs" <jw*@jwbfoto.demon.nl> wrote in message
news:3f***********************@news.euronet.nl...
<snip>
function test (obj) {
var type = typeof obj;
if (type == 'object') {
if (obj.getDate) return 'Date';
if (obj.split) return 'String';
}
return type;
}


If - obj - happened to be - null - then - type - would still be "object"
but the property testing would cause errors. Maybe:-

if((obj)&&(type == 'object')){

- would be safer.

Richard.
Jul 20 '05 #7
> If - obj - happened to be - null - then - type - would still be "object"
but the property testing would cause errors. Maybe:-

if ((obj) && (type == 'object')){


Yup. I put stuff like that into so functions. For example,

function isFunction(a) {
return typeof a == 'function';
}
function isNull(a) {
return typeof a == 'object' && !a;
}
function isNumber(a) {
return typeof a == 'number' && isFinite(a);
}
function isObject(a) {
return (typeof a == 'object' && a) || isFunction(a);
}

I find that typeof's own understanding of types is too fuzzy.

http://www.crockford.com/javascript/remedial.html

Jul 20 '05 #8
Robert Mark Bram wrote:
Howdy All!

How can you tell the type of an object?

I am not talking about using "typeof" ..

I am talking about being able to determine if an object is a Date or String
...


You can use the instanceof operator:
var now = new Date();
alert(now instanceof Date);
Jul 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by sashan | last post: by
3 posts views Thread by George Sakkis | last post: by
5 posts views Thread by Tongu? Yumruk | last post: by
6 posts views Thread by Web Developer | last post: by
3 posts views Thread by Vinodh Kumar P | last post: by
22 posts views Thread by Qopit | last post: by
20 posts views Thread by Xiaoshen Li | last post: by
4 posts views Thread by Patient Guy | last post: by
3 posts views Thread by w.m.gardella.sambeth | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.