0

I have a namespace Utilities in JavaScript, it looks sort of like this:

var Utilities = Utilities || {
    printTest: function() { print("test"); }
}

I can call my printTest function with Utilities.printTest(); but I am wondering if I can call it with something like

var Utilities.....
using Utilities;
printTest();

I want it to work similar to how C++ implements namespaces into your code with the using statement. Is there anything similar for JavaScript?

Thanks

1
  • 1
    No you can't, and what you have is just a regular object Commented Mar 9, 2016 at 22:40

4 Answers 4

2

The with statement:

var a, x, y;
var r = 10;

with (Math) {
  a = PI * r * r;
  x = r * cos(PI);
  y = r * sin(PI / 2);
}

Note that it is not allowed in strict mode.

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

4 Comments

There's a reason it isn't allowed in strict mode. It's not a good idea to use it.
@MikeC It's not deprecated - that blog post is heavily biased for no good reason.
Thanks, this is exactly what I was looking for
@Izkata It throws an error in strict mode. As far as I'm concerned that's deprecated.
1

Nope, that's not possible. The closest you can get (assuming the this context isn't important) is assigning the functions to individual variables.

var printTest = Utilities.printTest;
var otherMethod = Utilities.otherMethod;
...

Comments

1

There is no 'using' keyword in JavaScript like there is in C# or other languages.

You can pull in the module or library via a script tag for the client. Node.js has a 'require' keyword that will you can use in your Node.js application like this: require("body-parser");

You can put that statement at the top of a file and Node.js will look up your file structure for that module. It's methods are then available in that file.

Comments

1

The following would give something of a similar functionality (when this is defined), but there's a reason using is infrequently used in C++: It oftentimes defeats the purpose of having a namespace in the first place. Namespaces exist to isolate independent parts of a design, and if they need to be mashed together like this, it might be worth considering a different structure. That being said, there's a reason the keyword exists, so there are certainly legitimate uses!

function using(namespace) {
  if (typeof namespace === 'object' && this !== null && this !== undefined) {
    for (var i in namespace) {
      this[i] = namespace[i]
    }
  }
}

using.bind(this,myNamespace);//attaches the values of namespace to this

Again, this pattern isn't really recommended for most cases.

4 Comments

You have a syntax error (remove the =>) and this doesn't really work like a using statement.
Wow that's embarrassing. Changing now! And it works in scenarios where this is defined. In JSFiddle, it's apparently undefined in the global, so I'll add a sentence about that. (All the more reason for this not to be a pattern.) Thanks for the catch.
While it may be true that the global scope is undefined, that's not entirely the problem. You can't access the members directly by name without a preceding this.. A using statment doesn't require this. so it's a close approximation but not quite right.
Absolutely. More of a forced attempt than anything else, which really doesn't fit the spirit of using in the first place.

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.