View Single Post
Old 02-01-2013, 04:07 PM   PM User | #1
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
how do you write production code

hello,

I am rewriting some code at the moment and it is written in a style that I am unfamiliar with, which I think is called production style. Here's a very basic example:

Code:
<!doctype html>
<html lang="en">
    
    <head>
	<style>
	</style>
    </head>
    
    <body>
<input type="button" value="hi" onclick="namespace.hello()"/>
<input type="button" value="bye" onclick="namespace.goodbye()"/> 
	 <script>
var namespace = function () {
    return {
        hello: function () {
            alert("hello")
        },

        goodbye: function () {
            alert("goodbye")
        }
    }
}();
		</script>
    </body>
</html>
which works, but I wonder about the return in there - other examples I have seen around the net don't use that return, but if I take it out or try to replace it I get all sorts of errors.

My questions:

- Is the return really necessary, and if not how would you make that code work without?

- I assume that the page is coded that way to avoid polluting the global namespace. But I have two functions within the same page that need access to the same variables. I see that declaring a variable in the "namespace" function makes it available to both the hello and goodbye functions, but it doesn't actually become globally global, does it?

thanks in advance for any thoughts.
xelawho is offline   Reply With Quote