Mozilla JavaScript Question

You are here:  SillyDog701 > Message Centre > Web Design and Page Coding > [sdt=12282]
SillyDog701 Forums
Author Message
duncan0001
new member

Duncan Disorderly
Joined: 27 Oct 2006
Posts: 1
27 Oct, 2006 2:55 am Mozilla JavaScript Question [sdp=78897]  

Anybody know why the Javascript typeof() function returns 'function' instead of 'object' for variables derived via the document getElementByID method? Seems to get it right with other datatypes.

Example:

var newObj = new Object > typeof returns 'Object' as datatype

var newObj = document.getElementById("myembed") > returns 'function' datatype

Its as though, in the second, case newObj is declared with the type associated with
the getElementById method.

It is affecting my ability to access properties and methods of an embedded object.

Anyone else seen this?

Curious.

Thanks

Duncan Disorderly

UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; FunWebProducts; .NET CLR 1.1.4322)
Back to top profile
gumlor
new member


Joined: 19 Jan 2007
Posts: 1
Location: Vancouver, BC Canada
19 Jan, 2007 1:39 pm Javascript Class: "Object" or "Function" [sdp=81473]  

Hello Duncan,

What you are describing is normal. Here are the behavior, tested on IE6 & FF1.5

- When you use "document.getElementById()", it returns a DOM element as an "object" datatype.

- When you use "new Object()", it returns an "object" datatype.

- When you use "{ foo: 'value', bar: function(){} }", known as "object literal syntax" to declare a class, it returns an "object" datatype.

- When you use "function () { this.foo='value'; this.bar=function(){}; }" to declare a class, it returns a "function" datatype.

(RUN THE "EXAMPLE SCRIPT" AT THE BOTTOM TO SEE THIS IN ACTION)

The above shows the 3 different ways to declare a class. Depending of the way of declaration, the object return different datatype as either "object" or "function". I think it's because of the flexibility in javascript to build a class. You can build a class as an object or as a function.

Using "typeof" to inspect if an object a user-defined class is too generic (well javascript functions are almost all generic anyway). I think you'll need to customize your own class property to check if an user-defined class a class, e.g.,

var myClass = new Object();
myClass.isClass = true;
myClass.foo = 'value';
myClass.bar = function(){};

And use myClass.isClass to verify if it's a class.




<!--- [START] EXAMPLE SCRIPT --->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>

<div id="foo">bar</div>

<script type="text/javascript">

// get DOM element
var obj1 = document.getElementById("foo");
alert(typeof obj1); // alert 'object'

// create an Object element
var obj2 = new Object();
obj2.cap = false;
obj2.openMe = function() { cap=true; }
alert(typeof obj2); // alert 'object'

// create an object using the object literal syntax
var obj3 = {
cap: false,
openMe: function(){ cap=true; }
};
alert(typeof obj3); // alert 'object'


// create an object as a function object
var obj4 = function() {
this.cap = false;
this.openMe = function(){ cap=true; }
};
alert(typeof obj4); // alert 'function'

</script>

</body>
</html>
<!--- [END] --->

UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; (R1 1.5))

my current project: brides community site
Back to top profile website
Display posts from previous:   
Reply to topic    Forum Index > Web Design and Page Coding All times are CST (GMT -6)
page 1 of 1
To add your questions, comments, and for more features and more, please join SillyDog701 Message Centre. It's free! This is SillyDog 701 Message Centre (SD701 Forums).

App Store Your favourite music, radio, music videos, TV shows, movies and more...
Download on iTunes

*Search | FAQ | Rules and Policies | MozInfo701 - Mozilla Information Centre | SD701 Open Directory | Message Board Map | download Netscape