Like others have said, "this" is different in test and the anonymous function passed to click().
test is a global function, therefore, "this" is a reference to the window (global) object. What you are actually doing is setting a global variable, probably not an intended side-effect. (use alert(window.rating) to see what I mean)
For your example, there is no need to use "this", though I think your example is just to demonstrate a point. If it were real code, tt should be converted to:
function test () {
var rating = 0;
$j('#star_rating a').click(function() {
alert(rating); //Use the closure defined in the outer function
});
}
The point is that you shouldn't use "this" from global functions.