Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript typeof error

    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:

    Code:
    	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.

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,021
    Thanks
    15
    Thanked 239 Times in 239 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,078
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Socrates View Post
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •