Mozilla JavaScript Question

Need technical advice on coding your web pages? Covers HTML, JavaScript, CSS, and some server side technologies. Also the issue of some webpages not displayed well.

Moderator: Don_HH2K

Mozilla JavaScript Question

Postby duncan0001 » Fri 27 Oct, 2006 3:55 am

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)
duncan0001
new member
new member
 
Posts: 1
Joined: Fri 27 Oct, 2006 3:39 am

Javascript Class: "Object" or "Function"

Postby gumlor » Fri 19 Jan, 2007 2:39 pm

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
gumlor
new member
new member
 
Posts: 1
Joined: Fri 19 Jan, 2007 2:19 pm
Location: Vancouver, BC Canada


Return to Web Design and Page Coding

Who is online

Registered users: Google [Bot]