473,595 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 94975

"Robert Mark Bram" <ro*********@yo urshoesinfotech .monash.edu.au> schreef in
bericht news:3f******** **************@ news.optusnet.c om.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('tes t')); // Alerts 'string'
alert(test(new String('test')) ); // Alerts 'String';
alert(test(new Date())); // Alerts 'Date';
JW

Jul 20 '05 #2

"Janwillem Borleffs" <jw*@jwbfoto.de mon.nl> schreef in bericht
news:3f******** *************** @news.euronet.n l...

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.de mon.nl> schreef in bericht
news:3f******** *************** @news.euronet.n l...

"Janwillem Borleffs" <jw*@jwbfoto.de mon.nl> schreef in bericht
news:3f******** *************** @news.euronet.n l...

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.constru ctor == Date)

--

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

Jul 20 '05 #5
"Robert Mark Bram" <ro*********@yo urshoesinfotech .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 "constructo r" that points to itself.

That is
var x = new String("foo");
alert(x.constru ctor == 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.prototyp e.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.de mon.nl> wrote in message
news:3f******** *************** @news.euronet.n l...
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
2831
by: sashan | last post by:
I'm a Python newbie. I have been using c++ for 5 years and before that I was programming in Pascal. The one thing that annoys me about python is dynamic typing because I find myself making typographical errors of the sort that would under C++ be picked up by the compiler at compiler time. With Python I have to wait for the error to appear at runtime in order for me to correct it. I find this inefficient. Any advice about how to get around...
3
2346
by: George Sakkis | last post by:
Explicit type checking is not typically seen in Python code, and usually that's not a big problem; most typing errors are likely to raise a TypeError or AttributeError sooner than later. There are cases, though, where typing errors are not caught (at least not early) because different classes happen to have methods with the same name; that's really subtle with the special methods like __eq__, __hash__, etc. that are common to all (or at...
5
2990
by: Tongu? Yumruk | last post by:
I have a little proposal about type checking in python. I'll be glad if you read and comment on it. Sorry for my bad english (I'm not a native English speaker) A Little Stricter Typing in Python - A Proposal As we all know, one of the best things about python and other scripting languages is dynamic typing (yes I know it has advantages and disadvantages but I will not discuss them now). Dynamic typing allows us to change types of...
6
14310
by: Web Developer | last post by:
Hi, I come across the term "type checking" very often in my readings on C++, and have never heard it in Java. Besides the simplistic answer that it checks the "type", what more does it mean? WD
3
2109
by: Vinodh Kumar P | last post by:
Whenever I read any C++ literature I find the words "C++ is statically type checked".OK.Agreed. Is there any language that supports "Dynamic type checking"? In such a case any type can be assigned to any other type during compile time?
22
4898
by: Qopit | last post by:
Hi there, I'm pretty new to Python and am trying to figure out how to get "will this code compile?"-like code checking. To me this is a pretty basic language/environment requirement, especially when working with large projects. It is *much* better to catch errors at "compile-time" rather than at run-time. One thing I've "found" is the PyChecker module (conveniently embedded in SPE), but it doesn't seem to do that great of a job. ...
20
2075
by: Xiaoshen Li | last post by:
Hi, I am reading the book "Concepts of programming languages" by R. W. Sebesta. He said: "One of the most important reasons why C is both liked and disliked is its lack of complete type checking. For example, functions can be written for which parameters are not type checked. Those who like C appreciate the flexibility; those who do not like it find it too insecure."
4
2369
by: Patient Guy | last post by:
Does anyone have any coding rules they follow when doing argument checking? When arguments fail during check, do you return from the call with an ambiguous return value, or do you throw exceptions?
3
2448
by: w.m.gardella.sambeth | last post by:
Hi all, I am more or less new to Python, and currently am making my first "serious" program. The application is a Clinical History manager (for my wife) which stores its data on a sqlite database. After googling on this newsgroup, I have read several threads where is stated that the LBYL way of testing parameters is not a pythonic way to work, and that is preferable catch the exceptions generated trying to use an invalid parameter passed...
0
7954
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8250
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5837
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5416
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3908
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2391
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1222
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.