2

I want to know if it's possible to do something like:

var f = function;

and than use f like it would be the js function keyword

if something like this would be possible I would use it for js code minimization, cuz I have a lot of function in my code

11
  • 1
    this may be helpful: stackoverflow.com/q/336859/1507210 Commented Oct 30, 2012 at 22:02
  • 1
    Have you run said code through an actual minifier??? Commented Oct 30, 2012 at 22:02
  • 3
    When Chuck Norris declares functions, he only writes the first letter. Commented Oct 30, 2012 at 22:04
  • 1
    You may also be happy to hear about JavaScript Fat Arrow syntax which was approved by the group in charge of delivering ES6. javascriptweblog.wordpress.com/2012/04/09/javascript-fat-city Commented Oct 30, 2012 at 22:04
  • 1
    It is "possible" if you're going nuts for some sort of programming contest: you can alias "f" to the Function constructor, but then all your functions will have to be expressed as strings, which is kind-of a pain. Commented Oct 30, 2012 at 22:04

4 Answers 4

3

Similar to what Pointy pointed out in the comments, what you can do is use the function constructor to create a new function and pass it as string. In other words:

function f(s) { return new Function(s) };
var foo = f('var s = "hello"; return s');
alert(foo()); //=> "hello"

But again, I think this is unnecessary.

Edit: To add parameters you'd use the arguments object.

function f() {
  var args = Array.prototype.slice.call(arguments);
  var func = args.pop();
  return new Function(args.join(','), func);
}

var add = f('a,b', 'return a + b');
Sign up to request clarification or add additional context in comments.

4 Comments

how do you pass parameters to your created function?
Impressive. Both shows your javascript knowledge, and reaffirms that something like this should not be done.
lol, agree. This is a proof of concept more than anything else and I would definetely not use it in production but it's useful to know if you need to write a parser for example where eval and new Function are necessary evils.
I wanted to write a proof of concept like that, but could not think of how to handle the arguments dynamically
2

It is not possible to do as you describe.

The closest I can think of is to make a function for generating functions, using eval to parse a passed string. Using eval however, is generally evil and should be avoided. Overall, I would not recommend doing something like this at all.

Also, it is worth noting that there is very little point in doing what you want, as javascript sent over the wire should be compressed first, so the the word function will be represented by one token, regardless of how long the word is.

If you want your source code to be more readable and concise, I would recommend coffee-script which compiles to javascript, and allows you to define functions without using even the first letter of function. Chuck Norris would approve!

Summary of conclusions and recommendations

  • use coffee-script for cruft free source
  • compile it to verbose javascript you never look at
  • minify that javascript (uglify is a great compressor, or google's closure)
  • gzip it (will compress repetitive strings well)
  • send it

The key issue here is that after it is gzipped, the size of the word function becomes irrelevant, so the only remaining issue is the readability of your source code.

Comments

1

Yeah...to be quite honest, I wouldn't worry about trying to shorten the function keyword to 'f' which gives you back very little for the work and it would also hurt the readability of your code. And, unless you have something like a million functions, I wouldn't focus on that. As @prodigitalson said - run it through a minifier. That should take care of the real minifications that are possible in your code and still maintain readability in your code base (which is more important than saving a few bytes).

Also, per my comment, there is something (potentially) on its way to replace the long 'function' keyword (something Brendan Eich has said he would have considered changing - you have to remember that he designed the language in 10 days or so). Read more about it here: Fat Arrow Syntax Of course, these sorts of things can always change...but the standards definition bodies are looking at it.

Comments

0

You could do this with the hygenic macro functionality of http://sweetjs.org/

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.