1

I'm debugging a piece of jquery and found this snippet which i don't understand.

Would someone mind breaking it down so that I can research the individual parts?

 window.specific = $('.' + specific_class);
2
  • By prefixing window., the variable will be globally defined. This makes it easier to debug the variable's value (via the console, for instance). Commented Dec 2, 2012 at 11:30
  • 1
    It simply caches the list of elements matched by .specific_class in a global variable. Commented Dec 2, 2012 at 11:43

3 Answers 3

1

specific is defined in "window scope" outside the function. specific_class would have some class name in it and it is used in jQuery selector and matched elements are assigned to window.specific.

Live Demo

window.specific = "";
specific_class =  'someclass';

function myfun()
{
    window.specific = $('.' + specific_class);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the fiddle, so window.specific is a global object which refers to all instances of .specific_class. But since you can already target every '.specific_class' i'm curious as to why this is necessary - I'm sure this will become clear though...
1

This sets the 'specific' property of the window object to a class defined by the specific_class variable.

Let's say that the specific_class variable contained the text 'myClass', then window.specific would be equal to:-

window.specific = $('.myClass');

Which in turn would refer to all instances of the myClass class.

Comments

0

Basically it looks for a class...

. is the argument for a class selector. specific_class is a variable which will refer to the class name. Look for that variable and you'll find what the class means.

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.