...

View Full Version : Javascript typeof error



Socrates
12-06-2010, 11:37 PM
Hi, I have been programming javascript for a couple months now and this is the first problem I've run into with no readily available resolution.

I'm trying to get an inheritance chain setup, so that methods can be overriden. I want to use the prototype to contain the functions, but to have every instance have different variables. I tried doing this by checking the type of every variable in the class using a for loop, but something in this for loop is crashes Mozilla entirely and crashes the tab in Chrome.

Here's the for loop that is bothering me:


for (var key in newClass)
{
if (typeof(newClass[key]) === 'function')
{
JClass.prototype[key] = newClass[key];
}
else
{
JClass[key] = newClass[key];
}
}

Let me know if the full class would be better.

DrDOS
12-07-2010, 04:14 AM
This line is semantically strange;

if (typeof(newClass[key]) === 'function')

since === means 'type of and has the same value', using the typeof function in conjunction with it is redundant.

Socrates
12-07-2010, 03:55 PM
The typeof function returns a string, so while it is redundant, this shouldn't be a problem, since 'function' is also a string. However, I did change it to a double equals, and the result was the same.

However, I changed the quotes around 'function' to be double quotes ("") instead, and this seemed to work. Is there a difference between using single quotes and double quotes for strings in JS?

Philip M
12-07-2010, 04:08 PM
However, I changed the quotes around 'function' to be double quotes ("") instead, and this seemed to work. Is there a difference between using single quotes and double quotes for strings in JS?

No, there is no difference. You must have made some other alteration which cleared the bug.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum