Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 971453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:58:07+00:00 2026-05-16T02:58:07+00:00

What’s the shortest way (characters) of creating a new function alias. Basically this is

  • 0

What’s the shortest way (characters) of creating a “new function” alias.

Basically this is for code golf and minifying code beyond reason.
So when you usually would write:

a=function(a,b,c){return a+b+c;}

You could write something like (also let’s abstract return keyword as well with global variable R):

a=$("a,b,c","R=a+b+c")
a=$(a,b,c){R=a+b+c}

(Not sure if the second one is possible.)

For the first example the best I’ve come up with is:

$=function(a,b){return new Function(a,"R=0;"+b+";return R")}

Both the sizes (usage, declaration) matter but usage size is more important.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:58:07+00:00Added an answer on May 16, 2026 at 2:58 am

    I don’t think new Function() is of viable use for most functions even without performance concerns because unlike function() {} no closure is created. The compiled function will only have access to its own local scope and the global object. Think about that for a second. Javascript without closures is like Java without classes or C without pointers. Clearly everything would break.

    Anyways, if you only intend to use this alias for short lambada like expressions that don’t need clousers, one obvious way to make things even more terse is to leave off the parameters deceleration. Simply assume that a = arguments[0]; b = arguments[1]; etc…

    $=function(b){return new Function('a,b,c,d,e,f,g,h,i,j', b);};
    

    Another way would be to automatically return the value of the last expression, instead of needing it to be explicitly declared

    $=function(body) {
      return function(a,b,c,d,e,f,g,h,i,j) { return eval(body); };
    };
    

    Horrifying isn’t it? This works because eval()…well it returns the value of the last evaluated expression. No return needed. Of course this method is an even bigger hit to performance, as the code in body is reevaluated each time the function is called, while with new Function the code is (or should be) only compiled once.

    Anyways performance be dammed, with the previous method your function declaration is down to this

    var myeyes = $('a+b+c');
    alert(myeyes(1,2,3)); //6
    

    Purdy huh? Still, I think it would look better like this

    'a+b+c'
    

    Looks like a string yes? Well…it is a sting, but in the hands of the right function, it could be evaluated as if it were a full on function literal

    //Lets just assume that IE does not exist mmkay?
    var funcs = ['map', 'filter', 'every', 'some'];
    for (var i=0; i<funcs.length; i++) {
       (function() {
          //Store original function
          var name = funcs[i] 
          var _super = Array.prototype[name];
          Array.prototype[name] = function() {
            //$ !== jQuery
            if (typeof arguments[0] == 'string') arguments[0] = $(arguments[0]);
    
            return _super.apply(this, arguments);
          };
        }());
     }
    

    Now you can write

    [1,2,3,4,5].map('a*a');
    

    instead of

    [1,2,3,4,5].map(function(a){return a*a;});
    

    which is even better than Firefox’s Expression Closure

    [1,2,3,4,5].map(function(a) a*a);
    

    Try it out here: http://jsbin.com/iyogu3/edit

    If only we could actually write function expressions like this without calling upon the eval monster. Really, one of my main gripes with Javascript syntax is the requirement to spam function(){} throughout your code. Some kind of shorthand function literal syntax (not the aforementioned half-assed expression closure) that was interpreted the same as a regular verbose function literal would go a long way to making my code look a little less ridiculous. And it might help minifying a tiny bit as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.