473,756 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

finding the type of an object? ("typeof" doesn't work)

Is there any way to find a string representing an object's class,
which will work in Internet Explorer 6?

"typeof" doesn't work -- it returns "object" for all objects:

x = window.open('ht tp://www.yahoo.com/');
alert(typeof x);

And I found this page:
http://www.mozilla.org/js/language/j...pressions.html
which claims: "To get the type of an object x, use x.class".

However, that doesn't work in IE 6 so it must be Mozilla-only.

Can it be done?

-Bennett
Jul 23 '05 #1
7 42575


Bennett Haselton wrote:
Is there any way to find a string representing an object's class,
which will work in Internet Explorer 6?

"typeof" doesn't work -- it returns "object" for all objects:

x = window.open('ht tp://www.yahoo.com/');
alert(typeof x);


Well, JavaScript 1.x doesn't have classes so I am not sure what you are
looking for. In terms of JavaScript/ECMAScript edition 3 x is of type
Object.
--

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

Jul 23 '05 #2
Fox


Bennett Haselton wrote:

Is there any way to find a string representing an object's class,
which will work in Internet Explorer 6?

"typeof" doesn't work -- it returns "object" for all objects:

x = window.open('ht tp://www.yahoo.com/');
alert(typeof x);

And I found this page:
http://www.mozilla.org/js/language/j...pressions.html
which claims: "To get the type of an object x, use x.class".
You can't do this yet -- .class is a proposed addition for JS2

However, that doesn't work in IE 6 so it must be Mozilla-only.

Can it be done?


Not for window or document -- not in IE.

Normally, you would parse the constructor, but window.construc tor and
document.constr uctor are both "undefined" in IE (it's not JavaScript -
it's JScript). In Mozilla, the constructors are returned as "[Window]"
and "[HTMLDocument]" {*not* function Window(...etc.. . as a "regular"
JavaScript constructor} Furthermore, in IE, window and document are
synonymous (but not exactly equal to each other).

proof:

alert( window == document );

// IE => true; Moz => false
Considering that you cannot instantiate a Window or Document object from
within JavaScript, it really doesn't matter one way or the other.
for all other javascript objects:

Object.prototyp e.objectType = function()
{
var obType = String(this.con structor).match (/function\s+(\w+ )/);

if(obType) return obType[1];
return "undefined" ; // just in case...
}
examples:

function
_myObject()
{
this.anything=" ";
}

var mo = new -myObject();

alert(mo.object Type()); // type => _myObject

some others (as literals)

alert( (123).objectTyp e()); // => Number

alert( ([1,2,3]).objectType()) ; // => Array

alert( ("123").objectT ype()); // => String

alert( (true).objectTy pe()); // => Boolean

and:

function
myFunc()
{
var nada = "";
}

alert( myFunc.objectTy pe() ); // => Function

etc...
if this isn't what you meant... then never mind...
Jul 23 '05 #3
On Sun, 02 May 2004 22:43:28 -0500, Fox <fo*@fxmahoney. com> wrote:
Normally, you would parse the constructor, but window.construc tor and
document.const ructor are both "undefined" in IE (it's not JavaScript -
it's JScript).
They're host objects, so the language is irrelevant. You get the same
behaviour in DScript, and if anyone packaged up SpiderMonkey as an
ActiveScripting language you would there to.
Furthermore, in IE, window and document are
synonymous (but not exactly equal to each other).

proof:

alert( window == document );

// IE => true; Moz => false
That they get type converted to something that == each other in IE is
not proof that they are synonymous.
Considering that you cannot instantiate a Window or Document object from
within JavaScript, it really doesn't matter one way or the other.


Indeed!

Jim.
--
comp.lang.javas cript FAQ - http://jibbering.com/faq/

Jul 23 '05 #4
Bennett Haselton wrote:
Is there any way to find a string representing an object's class,
which will work in Internet Explorer 6?
No, since ECMAScript up to ed. 3 and thus JavaScript up to version 1.5
and JScript up to version 5.6 do not provide a class-based object model
but a prototype-based one.
"typeof" doesn't work --
It does.
it returns "object" for all objects:
As it is supposed to.
x = window.open('ht tp://www.yahoo.com/');
Have you declared the variable previously? If not, you better use the
`var' keyword here.
alert(typeof x);

And I found this page:
http://www.mozilla.org/js/language/j...pressions.html
which claims: "To get the type of an object x, use x.class".
ECMAScript 4 is yet to be standardized and JavaScript 2.0 to be the base
of it is yet to be implemented in UAs. If you would have read the above
thoroughly you would have noticed that there is still only one
implementation of ECMAScript 4 available -- Epimetheus. (Which could
become a prophetical choice since it is entirely possible that ECMAScript
4/JavaScript 2.0 will never be finished as AOLTW had temporarily closed the
Netscape browser division recently and, as probably a result, Netscape is
no longer a member of ECMA.)
However, that doesn't work in IE 6
IE resp. the Windows Script Host supports, among other script languages
like VBScript, JScript -- Microsoft's implementation of ECMAScript up to
ed. 3. So a JavaScript spec/doc is simply the wrong place to look, no
matter how current it is.
so it must be Mozilla-only.
It does not work in current Mozillas either.
Can it be done?


That depends on what you are looking for and where. In ECMAScript 3 and
thus JavaScript 1.5 each object has a "constructo r" property (inherited
from the Object prototype) referring to the constructor used to create the
object. Since that constructor function (in fact, *every* named function
statement) is also the definition for an object prototype, objects created
using the Foo() constructor can be referred to as "Foo objects".
window.open() returns a reference to a Window object if successful. One
could test for this in Gecko-based UAs with

if (x && x.constructor && x.constructor == Window)
{
// ...
}

But since window.open() never returns an object of a type different
from Window if successful and not every UA provides a public prototype
for all of its host objects (as Window objects are), that test appears
to be only academical. AFAIK

if (x)
{
// ...
}

always sufficed to date. If there are statements between the
window.open() call and the test, the latter should be changed to

if (x && !x.closed)
{
// ...
}

Both solutions have been pointed out to numerous times in this newsgroup
before. Please search before you post, see the FAQ.
PointedEars
Jul 23 '05 #5
Thomas 'PointedEars' Lahn <Po*********@nu rfuerspam.de> writes:
Bennett Haselton wrote:
If you would have read the above thoroughly you would have noticed
that there is still only one implementation of ECMAScript 4
available -- Epimetheus.
I believe JScript.NET is also a (perhaps partial) implementation of
ECMAScript v4.
That depends on what you are looking for and where. In ECMAScript 3 and
thus JavaScript 1.5 each object has a "constructo r" property (inherited
from the Object prototype) referring to the constructor used to create the
object.
The constructor isn't necessarily inherited. When a user declared
function is created, its prototype object is also created and assigned
to the function's "prototype" property. After that, the function is
assigned to the prototype object's "constructo r" property.
I.e.,
function foo(){};
also creates the new object
foo.prototype
and makes the assignment
foo.prototype.c onstuctor = foo;

In this case, the constructor property is not inherited.

In the case of host objects, all bets are off, as usual. In my
browser, Opera, it is correct that window.constuct or is inherited (it
is Object). In Mozilla, where there are available constructors for
most host objects, window.construc tor is the global function Window.
Since that constructor function (in fact, *every* named function
statement) is also the definition for an object prototype, objects created
using the Foo() constructor can be referred to as "Foo objects".


Yes, as long as one makes sure not to break the relationship by manually
manipulating prototypes.
---
function Foo(){ this.x = 42; }
function Bar(){ this.y = 37; };
var foo = new Foo();
var bar = new Bar();
alert([foo instanceof Foo,
foo instanceof Bar,
bar instanceof Foo,
bar instanceof Bar]); // true,false,true ,false
// swap:
var fprot = Foo.prototype;
Foo.prototype = Bar.prototype;
Bar.prototype = fprot;
Foo.prototype.c onstructor = Foo;
Bar.prototype.c onstructor = Bar;
// and they are swapped.
alert([foo instanceof Foo,
foo instanceof Bar,
bar instanceof Foo,
bar instanceof Bar]); // false,true,fals e,true
---
The connection between an object and its constructor is really a connection
between the object and the constructor function's prototype object, because
inhertance happens between objects. The constructor functions merely
facilitate the creation and initialization of new objects based on an
old object.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #6
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@nu rfuerspam.de> writes:
If you would have read the above thoroughly you would have noticed
that there is still only one implementation of ECMAScript 4
available -- Epimetheus.
I believe JScript.NET is also a (perhaps partial) implementation of
ECMAScript v4.


Indeed!

<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/jscript7/html/jsgrpecmafeatur es.asp>
That depends on what you are looking for and where. In ECMAScript 3 and
thus JavaScript 1.5 each object has a "constructo r" property (inherited
from the Object prototype) referring to the constructor used to create the
object.


The constructor isn't necessarily inherited. When a user declared
function is created, its prototype object is also created and assigned
to the function's "prototype" property. After that, the function is
assigned to the prototype object's "constructo r" property.
I.e.,
function foo(){};
also creates the new object
foo.prototype
and makes the assignment
foo.prototype.c onstuctor = foo;


There is an "r" missing.
In this case, the constructor property is not inherited.
Sorry, I fail to see the difference.
Since that constructor function (in fact, *every* named function
statement) is also the definition for an object prototype, objects created
using the Foo() constructor can be referred to as "Foo objects".


Yes, as long as one makes sure not to break the relationship by manually
manipulating prototypes.


There are always ways to meddle with the defined workings of the object
model. Taking every possible way into account every time one posts a
followup/reply would by far extend the purpose of this newsgroup.
---
function Foo(){ this.x = 42; }
function Bar(){ this.y = 37; };
var foo = new Foo();
var bar = new Bar();
alert([foo instanceof Foo,
foo instanceof Bar,
bar instanceof Foo,
bar instanceof Bar]); // true,false,true ,false
// swap:
var fprot = Foo.prototype;
Foo.prototype = Bar.prototype;
Bar.prototype = fprot;


The proper way is

Bar.prototype = new Foo;
<http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/guide/obj2.html#10083 88>
PointedEars
Jul 23 '05 #7
Thomas 'PointedEars' Lahn <Po*********@nu rfuerspam.de> writes:

The proper way is

Bar.prototype = new Foo;
The proper way to what?
<http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/guide/obj2.html#10083 88>


I think this is a bad way of trying (and failing) to emulate class
based inheritance in a non-class based language. Instead of inheriting
from the generic class, you inherit from a single instance (which is
what prototype based inheritance is all about). You lack the call to
the superclass' constructor, and all your instances share the properties
of the prototype.

Example where it fails:
---
function Stack() {
this.stack = [];
}
Stack.prototype .push = function(x){thi s.stack.push(x) ;}
Stack.prototype .pop = function(){retu rn this.stack.pop; }

function CountableStack( ) {}
CountableStack. prototype = new Stack();
CountableStack. prototype.count = function() {return this.stack.leng th;}
---
This looks plausible, if one reads the Netscape link above. It
fails terribly, since all CountableStack' s use the same internal
stack.
---
var s1 = new CountableStack( );
var s2 = new CountableStack( );
s1.push(42);
s2.push(37);
alert(s1.count( ));
---

So, IMO, it's *not* a propert way to do anything.

A closer to proper way to make class-like inheritance in Javascript is
(for Bar(x,y,z) extending Foo(x,y)):
---
function Bar(x,y,z) {
Foo.call(this,x ,y);
this.z=z;
}
Bar.prototype = clone(Foo.proto type);
---
where clone is
---
function clone(obj) {
function Cloner(){};
Cloner.prototyp e = obj;
return new Cloner();
}
---
(or *maybe* just use an instance of Foo as prototype, if you know
that it doesn't matter that it has been initialized once).
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
16592
by: Roland | last post by:
Hello, the example code is the following(the number in parentheses at the beginning are just for reference)(The complete HTML file is at the end of this article): (1)window.location = 'http://www.google.com'; (2)alert("I'm still here!"); (3)window.open("http://www.slashdot.com", "_blank");
3
3633
by: Marco | last post by:
Howdy! Given that: Type type = Type.GetType("System.Windows.Forms.Button"); Question: How do I obtain a Button object by only knowing the type object. In other words, how do I fire the constructor of a type object (Button(), in this case) ??? Thank you and God bless.
6
1989
by: Derrick | last post by:
Hello all; Since I do have working code, this is more for my curiosity only. I'm creating a "Plugin" architecture, following some of the many examples on the 'net. Basically what I have is this: - a DLL which contains the interface that every plugin must implement (IPlugin). This DLL also contains a class with the ability to search for DLLs in the calling applications working directory that contain classes implementing IPlugin (I...
6
5325
by: ucasesoftware | last post by:
All SendKeys.Send like ^C, ^V, ^X work in my form with textboxes but SendKeys.Send("^A") doesn't work ! ?? i have a bip error windows ! ?
9
1939
by: Ben | last post by:
Hello, I'm not a developper, so sorry if it's a stupid question... I'm trying to develop an application in vb.net and I have the following problem: I have some information in an array: sdist(i). The information is a string. When I run the application, I don"t have problem for compilation but during te execution, I have the error Cast from type 'Object()' to type 'String' is not valid on the line: sdistinguishedname = Sdist(i). Or...
13
25872
by: Fredrik Strandberg | last post by:
Hi! I receive an object as a System.Object argument to a method. I need to check if the object is a Type object or not (that is, not a specific type, but if the object is a type object in general). How can I do this? Is there a way of checking if my object inherits from the System.Type class? sub myMethod(obj as Object)
5
8864
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the created methods are not "registered" and only available through the DynamicMethod class. So a method lookup finds the origin property. A little test: public class DerivedClass : BaseClass {
5
3173
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a instance is created by "calling" a class object.
2
10013
boss32178
by: boss32178 | last post by:
Ok I get this error when i try to run the web app. foreach statement cannot operte on variables of type 'object' does not contain a public definition for 'GetEnumerator' here is the code #region Number String public string MyNum(System.Object MyString) { string temp; foreach (string mylet in MyString)
0
9271
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10031
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...
1
9838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9708
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...
1
7242
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
6534
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
5140
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3805
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
2
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.