Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
geoffrey
28,736 PointsDon't understand $.inArray();
Hello, It might sound stupid for you, but I don't understand that "selector", not sure I have to call it this way, as I don't see any css selector in it but what I mean is I can understand easily things like $(".required") or ("#main p") but I donet get the
$.inArray() !
function containsBlanks(){
var blanks = $required.map(function(){
return $(this).val() == "";
});
return $.inArray(true,blanks) != -1; // why $. what does it represent ?
}
To me, I would be more logical to do directly inArray(true,blanks); But that doesn't work this way obviously...
2 Answers
Paolo Scamardella
24,828 Points$ is a shortcut for jQuery function, which in Javascript are objects too. If you think about it and have done OOP before, the way you access a method or properties of an object is using the "." notation sign for most languages. In this case, $.inArray is the same thing for jQuery.inArray(). As a result, the $ or jQuery is an object that is accessing the inArray method with .inArray().
Hope this helps!
geoffrey
28,736 PointsThank you to both of you for taking the time to answer me. I realize my misunderstanding comes from a stupid thing. I thought inArray () was as well a native method of Javascript, so at first I tried tu use inArray() without the "$", as I thought it exists in pure JS. I probably missed something along the video. But once again, thank for your explanations, I got it ! :)
David Skurnick
7,654 PointsDavid Skurnick
7,654 PointsThe inArray() function is a bit different than some of the other jQuery functions you may have seen so far. Because it doesn't operate on an html element, it doesn't have the $("#selector") syntax that you're used to. Instead, it has the following syntax: $.inArray(value, arrayName); where 'value' is the value you're searching for in arrayName.
The type of syntax you were hoping for is more likely to be found in a non-object-oriented language. Even if you were using raw javascript, the syntax would be arrayName.indexOf(value); or something like that.