1

I'm new to Object-Orientated-Programming. I am trying to pass a element class as a parameter to a function. I know I've missed something...see code below:

var n = new Object();

n.mousePosition = function(class, y){   
    $(document).mousemove(function(e){
        if(e.pageY < y){ $(class).slideDown(200); }
        if(e.pageY > y){ $(class).slideUp(200); }
    });
}
n.mousePosition('.nav', 100);

The .nav is the element class name which I'm trying to pass to my function as the class parameter, the $(class).slideDown... is not picking it up

Any help would be greatly appreciated, thanks

2
  • 4
    class is a reserved word in JavaScript, what happens if you use a different variable name (elemClass, for example)? Commented Dec 5, 2011 at 22:07
  • No worries, that was the central theme of several answers, below, too. So if it's solved, you should accept one of those =) Commented Dec 5, 2011 at 22:12

5 Answers 5

2

class is a reserved keyword in JavaScript. Use something different, e.g. className as parameter name instead.

Sign up to request clarification or add additional context in comments.

Comments

1

Class is a reserved word, preface it with something else to avoid that issue, ie... myClass.

In addition you don't need to preface the class with the . when passing it in as a parameter.

n.mousePosition('nav', 100);

3 Comments

but then he will need to add a dot(.) in the function, best is to pass .nav because later on it can be changed to some other selector
+1 for reserved word, -1 because jQuery is being used, so yes he does need to prefix the className with .
@AaronMcIver thanks for your input. DavidThomas's comment did the trick
0

Class is a reserved word in JavaScript. You cannot use class as a variable name. For other reserved words you can visit: http://www.quackit.com/javascript/javascript_reserved_words.cfm

Comments

0

class is a reserved keyword use something else e.g. klass

var n = new Object();

n.mousePosition = function(klass, y){   
    $(document).mousemove(function(e){
        if(e.pageY < y){ $(klass).slideDown(200); }
        if(e.pageY > y){ $(klass).slideUp(200); }
    });
}
n.mousePosition('.nav', 100);

see jsfiddle example in action http://jsfiddle.net/anuraguniyal/LBfBn/4/

Comments

0

Note that class is a reserved word:

function t(class) }
    console.log(class);
}

t("test")

Result (Firefox 8.0):

SyntaxError: class is a reserved identifier

Try renaming to className (or similar).

The more general advice I'd give is that you could have easily caught this error by running the snippet in Firebug or jsfiddle or by debugging in some other environment that would have exposed the error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.