This is in part due to the fact that every line of code we write must be browser compatible, and that adds lots of "ifs" to our code.
So I guess it's understandable to think that choosing a framework that can abstract these kind of things for us is good. By using a framework we can concentrate on other kind of problems, more like high-level-usability-pattern problems.
Most of the people don't do this today. You can see posts about call and apply, closures and private members patterns very often in Reddit and Ajaxian, and each time that post appears lots of other people upmod it. So that means that most of the people today probably use the bind Function method without really knowing what it does.
This is a very simple example (and interesting interview question also).
So, how would you write them?
Basically we add one space at the beginning and end of each string, and see if the className property of the domElement contains the className string provided.
Why adding those spaces? Well, if we don't add spaces then we could have problems with names containing the given className.
Also, the code is concise and most of the work is done by the built-in indexOf method, which is good for performance.
Did you get that answer well? Good! How about your removeClass function?
The answer is this:
Ok, so that's simple enough.
So this regexp means that either we're at the beginning of the string (^) and searching for our className having a space at the end ('^myclassname\\s') or simply ending with that className ('^myclassname$') or we're looking for our className string having a space at the beginning and end ('\\smyclassname\\s') or we're at the end of the string looking for our className having (or not) a space at the beginning of the string ('\\smyclassname$' or '^myclassname$').
This regexp is equivalent to (if we had startsWith and endsWith methods):
Ok, so once the RegExp matches it's replaced by '$1', which means the first capturing group, in this case the (^|\\s) group.
So back to our example, the domElement.className was: 'myclassname yeah' so the RegExp that matches is ^myclassname\\s and the capturing group is ^, so the returned string is 'yeah', just as espected.
What (?: ...) does is not to make a capturing group from the parenthesis match. I think this is good for performance f you're not using that group in the string replacement:
Non-Capturing Parentheses are of the form `(?:<regex>)' and facilitate grouping only and do not incur the overhead of normal capturing parentheses. They should not be counted when determining numbers for capturing parentheses which are used with backreferences and substitutions. Because of the limit on the number of capturing parentheses allowed in a regex, it is advisable to use non-capturing parentheses when possible.