With the code below, other than specifying manually, is there a way to export only the functions and variables whose name doesn't start with an underscore?
var myapp = myapp || {};
myapp.utils = (function() {
var
CONSTANT_A = "FOO",
CONSTANT_B = "BAR";
function func() {}
function _privateFunc() {}
return {//return all variables and functions whose name does not have the "_" prefix.}
}());
With the code below, other than specifying manually, is there a way to export only the functions and variables whose name doesn't start with an underscore?
var myapp = myapp || {};
myapp.utils = (function() {
var
CONSTANT_A = "FOO",
CONSTANT_B = "BAR";
function func() {}
function _privateFunc() {}
return {//return all variables and functions whose name does not have the "_" prefix.}
}());
Share
Improve this question
asked Jan 18, 2013 at 21:36
tamakisquaretamakisquare
17.1k28 gold badges91 silver badges132 bronze badges
7
-
4
I don't think you can access the current stack frame except for
arguments
. If you're doing this by convention, you could flip it around, use an "exports" object called_
, and export functions by doing_.func = function() { ... }
– millimoose Commented Jan 18, 2013 at 21:40 - 2 Have you considered just setting up a separate object for all the functions and properties you wish to export? – kinsho Commented Jan 18, 2013 at 21:41
-
@millimoose - That's another approach that I have been considering. The only pitfall with that approach is that I would have to include the namespace name (
_
in this case) when accessing the variables in the functions. Ex. Infunc()
, I would have to call_.CONSTANT_A
instead of justCONSTANT_A
. It's just a minor trouble, though. – tamakisquare Commented Jan 18, 2013 at 21:55 - @kinsho - Your suggestion is basically similar to millimoose's. It looks like there's no ways to achieve what I am looking for, so I would probably need to go with your/millimoose's suggestion. Thx. – tamakisquare Commented Jan 18, 2013 at 21:59
- 1 Why not just define the functions you want to export as member methods of the returned object literal?? – natlee75 Commented Jan 18, 2013 at 22:00
1 Answer
Reset to default 6Your idea requires being able to list all of the variables in the local scope. Unfortunately, JavaScript is not capable of doing that. See this related question.
There are two ways I've seen this be done:
1) Attach every variable when they're defined to an object to be exported:
var myapp = myapp || {};
myapp.utils = (function () {
var exports = {};
exports.CONSTANT_A = "FOO",
exports.CONSTANT_B = "BAR";
exports.func = function func() {}
function _privateFunc() {}
return exports;
}());
2) Or list all the exports at the end in an object literal:
var myapp = myapp || {};
myapp.utils = (function () {
var
CONSTANT_A = "FOO",
CONSTANT_B = "BAR";
function func() {}
function _privateFunc() {}
return {
CONSTANT_A: CONSTANT_A,
CONSTANT_B: CONSTANT_B,
func: func
};
}());
I've seen both (and even mixes of the two) used in practice. The second may seem more pedantic, but also allows a reader to look at a single segment of code and see the entire interface returned by that function.