Skip to content

Instantly share code, notes, and snippets.

@matthewstokeley
Last active February 13, 2020 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewstokeley/36b088f008e9c3ab4c36 to your computer and use it in GitHub Desktop.
Save matthewstokeley/36b088f008e9c3ab4c36 to your computer and use it in GitHub Desktop.
execute a function by string name in a namespace without eval
// 'pojo' idiom for namespacing javascript
var namespace = {};
var x = 'fn';
namespace.fn = function() {
console.log('fin');
}
// execute function by string name
var execute = function(context, name, args) {
return context[name].apply(context, args);
};
//execute(namespace, x);
// attached to the string prototype
String.prototype.execute = function(context, args) {
if (!context[this]){
return false;
}
context[this].apply(context, args);
};
x.execute(namespace);
@matthewstokeley
Copy link
Author

matthewstokeley commented Feb 12, 2020

JavaScript idiom for namespacing before es6 / commonjs modules

@matthewstokeley
Copy link
Author

Breaking all rules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment