First of all, your code works and what a valid way to create a function in JavaScript (aside from jQuery), but because you declare a function inside another function (anonymous in this case), "MyBlah" will not be accessible from the global scope.
Here is an example:
$(document).ready( function () { var MyBlah = function($blah) { alert($blah); }; MyBlah("Hello this works")
This is (sometimes) desirable behavior, since we do not pollute the global namespace , so if your function does not need to be called from another part of your code, this is the way to go.
Declaring it outside an anonymous function places it in the global namespace and is accessible to it everywhere.
Finally, $ is not required at the beginning of the variable name, and is sometimes used as a jQuery convention when the variable is an instance of the jQuery object itself (not necessary in this case).
Perhaps you need to create a jQuery plugin, it is very easy and useful, as it will allow you to do something like this:
$('div#message').myBlah("hello")
Pablo Fernandez May 25 '09 at 18:38 2009-05-25 18:38
source share