5

I agree that using a namespace is essential for JavaScript libraries, but what about those functions that reside in HTML files in a script tag? Basically these are "local" functions that are never shared with any other pages. Do you guys still use a namespace for those? If so, do you use the same namespace as your shared custom library?

5 Answers 5

2

I would say that a local namespace can help guard against any possible collisions with 3rd party JavaScript libraries, but it need not be imperative.

Sign up to request clarification or add additional context in comments.

Comments

1

You don't need a namespace, because as you say, there is no potential naming conflict.

The only potential conflict is if you are importing a library that does not use namespaces, and it introduces a variable which conflicts with a name you placed in the script file. But if you are following the namespaces practice in all the libraries, then it doesn't matter.

Comments

1

First of all. Placing javascripts in HTML is not something you should do. Javascripts should go in separate file(s). With obvious exception of loader, such as head.js or similar, if you use one.

I personally see no need to namespace local scripts. It never happened that one script I wrote collided with any plugin I have used.

3 Comments

Regarding placing all JavaScript code in .js files, doesn't it mean that almost every standalone HTML file would have its own .js file? Wouldn't it be a maintenance problem down the road?
@Tom Tucker - usually, you put all the scripts in one or two big files. Even if it doesn't apply to every page it's loaded on, it only gets downloaded once.
Yes, @aaronasterling is correct. It makes maintenance easier actually. You don't have a separate file for each page. What you have is one or two files which are used by all of your pages. You write your functionality there and html uses them on load. You should check out articles.sitepoint.com/article/simply-javascript
1

While it might not be imperative, there is really no reason NOT to namespace those. It can only help prevent issues.

Here's a good article on Script Junkie about different methods of namespacing: http://msdn.microsoft.com/en-us/scriptjunkie/gg578608.aspx

Comments

1

It has to depend on where the html that the scripts are embedding in is coming from. If it's in a context where the code's controlling the local scripts will be aware of what the other is doing, then I suppose there's no need for a namepace.

If on the other hand, the scripts will be running in the context of something like a CMS then there could be various server side plugins emmitting local javascript without the client side coder necessarily understanding the intricacies of the code. Even there, if that code is all namespaced, then you should be fine. Accidentally forgetting one var keyword can break a namespace though.

All in all, a namespace doesn't hurt things and isn't that hard. It's a lot easier to maintain and think about javascript when it's not all mixed in with html.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.