![]() |
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>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. |
oh, der.
I think I got it... Code:
var namespace= { |
Yes, both snippets are essentially the same. The second one simply declares an object, the first one is a little bit trickier: It defines a function that returns an object and then immediately calls itself (so-called self-invoking function).
You're also right about the purpose: Except for the namespace (i.e. the name of your module) nothing will be global, which means that you don't break other code and just as important, other code won't break yours (unless it is loaded after yours and explictly overwrites your namespace object). The only thing to look out for now is to ensure that namespaces won't collide. This is already fairly unlikely and even if it happens, it's an easy fix. In other languages like Java, for example, it's common practice to use reverse domain notation as the "namespace" (technically it's more a subpackage structure, but the idea is the same). The Google Play Store even uses this as the unique identification for an app! The tricky thing about this in JavaScript is a concept called closures. A simple example: Code:
var counter = (function () { |
The simplest way to avoid namespace clashes in JavaScript if you don't jumble the JavaScript with the HTML as you have in your example is to wrap all of the code for a script inside an anonymous function so as to effectively create a separate unnamed namespace for that script. Only library references need to use global variables so as to be accessible to multiple scripts in the page.
For example: Code:
(function() {Code:
jQuery.noConflict();To avoid such clashes you should replace the JavaScript code in the HTML with an id attribute and then add code into the separate JavaScript to run the code you removed from the HTML but using an event listener rather than an event handler - so that having a second script add code for the same event will not cause a clash. With the code you currently have using the namespace simply adding a second script that contains the following statement would break the first script even with it using a namespace - because the event processing is outside the namespace. Code:
document.getElementsByTagName('input')[0].onclick = function() {alert('not hello');}; |
Yes, Stephen. I think I have heard you say something similar before. Like I said - it was a basic example. Although I must say I haven't seen this before:
Quote:
|
I'm invisible.
|
sorry, Airblader. Thank you for your detailed explanation. It clears up many mysteries for me :thumbsup:
|
Quote:
|
| All times are GMT +1. The time now is 01:15 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.