Skip to content

Instantly share code, notes, and snippets.

@dsingleton
Created April 6, 2011 10:24
Show Gist options
  • Save dsingleton/905442 to your computer and use it in GitHub Desktop.
Save dsingleton/905442 to your computer and use it in GitHub Desktop.
(function(src) {
document.body.appendChild(document.createElement('script')).setAttribute('src', src);
})(prompt("Script URL to load", "http://example.com/script.js"));
// You could drop the self executing function now src is at the end of the line, it's just as easy to edit.
@jwheare
Copy link

jwheare commented Apr 9, 2011

What's wrong with local variables? How is passing in a document any shorter? Surely it makes the overall code longer even if the function is shorter?

@dsingleton
Copy link
Author

Fair point, I was getting overly code-golfy. Not that passing document in helps, you're right.

Nothing against local vars, but i like the style of passing args, and avoiding a var declaration. All very cosmetic. Just re-writing it now to be cleaner and shorter.

@jwheare
Copy link

jwheare commented Apr 9, 2011

I like the pattern of passing in variables for dependency injection to make functions more reusable/testable, but not sure there's a benefit in this case. You only care about the local scope inside the function, so a local variable is probably the best solution. Fore!

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