1

I would like to variably assign a name to a new function, is this possible?

function returnFunctionWithName(str){
  return function(){
    return srt
  }
}

var x = returnFunctionWithName("hello")

console.log(x) // => [Function]

What I want is something like this: (this doesn't work)

function returnFunctionWithName(str){
  return function [str](){
    return srt
  }
}

var x = returnFunctionWithName("hello")

console.log(x) // => [Function: hello]

Just tried this too with no effect:

function returnFunctionWithName(str){
  var x = function(){
    return str
  }
  x.name = str
  return x
}

var x = returnFunctionWithName("hello")

console.log(x) // => [Function: hello]
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
  • 1
    Before anything else, explain what you want to achieve. – Tomalak Jul 25 '15 at 04:56
  • The question is a bit cryptic at the moment. Can you describe what problem you're really trying to solve so rather than comment on your proposed solution, we can actually offer a solution to the real problem. – jfriend00 Jul 25 '15 at 04:56
  • http://jsfiddle.net/arunpjohny/vfc99eg4/ – Arun P Johny Jul 25 '15 at 04:57
  • @jfriend00 The code above is what I'm trying to do. I have a function that returns an anonymous function and I'd like to name it before I return it. – ThomasReggi Jul 25 '15 at 05:03
  • But why do you want a *dynamic* name? A function with a fixed body surely needs only one name. – Tomalak Jul 25 '15 at 05:07
  • @Tomalak not in the case I'm using it for, sorry I don't have more context. – ThomasReggi Jul 25 '15 at 05:08
  • 2
    @ThomasReggi - you are describing your attempted solution to your problem. You are not describing the actual problem that makes you think you need a solution like this. It seems unlikely to us that you need multiple names for the exact same function or that whatever issue you have couldn't be solved a completely different way. You will ALWAYS give yourself a better chance of getting helpful answers if you describe the actual real problem, not just issues with your solution. This is known as [the XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – jfriend00 Jul 25 '15 at 05:14
  • @jfriend00 Right now I'm passing function(s) as arguments into functions as [Object methods](http://stackoverflow.com/questions/31623026/what-do-you-call-an-object-with-functions-in-it) which is the right way to do this. I need the functions to be named for the consuming function to work. I was hoping to skip that step and just pass in or assign names directly to functions. – ThomasReggi Jul 25 '15 at 05:17
  • @jfriend00 I'm literally sitting in bed because I can't go to sleep, thinking of different ways to push the language. – ThomasReggi Jul 25 '15 at 05:18
  • Why does a function have to have a name for the consuming function to work? That part has me baffled. If you just want a unique identifier for a function, you can always just create your own property on the function since functions are objects that can have custom properties. – jfriend00 Jul 25 '15 at 05:23
  • Your right I could have added properties to the function for metadata like `name`. However then I'd have to invent my own schema for how that data is treated / consumed. Rather then inventing that I wanted to know the limitation of the language. Now that I know naming a function like this isn't feasible (not messing with `exec`), I can use Object Methods or attach properties to the function. – ThomasReggi Jul 25 '15 at 05:29
  • I really wish people would let questions be instead of trying to find the 'Z'. Even if this doesn't solve the 'Y', *the 'X' was clearly asked about*. Answer it or (after a simple comment aside) leave it well-enough alone.. – user2864740 Jul 25 '15 at 10:40

2 Answers2

0

You can technically set a function's name property as follows:

Object.defineProperty(theFunction, 'name', {
    get: function() { 
        return 'NewFunctionName'; 
    }
});

However, I don't know if this solves your problem, as in my quick tests this didn't actually render the expected name when console.log'ing the function.

The reason that x.name = str doesn't work is that Function#name isn't writable. It is, however, "Configurable", which means you can mess with it via Object.defineProperty().

jmar777
  • 38,796
  • 11
  • 66
  • 64
  • Cool idea! This is giving me `TypeError: Cannot redefine property: name` in nodejs. – ThomasReggi Jul 25 '15 at 05:07
  • While this is true, it doesn't apply to many environments... Some protect the name property allowing no modification whatsoever. – Fuzzical Logic Jul 25 '15 at 05:10
  • Hmm, just verified that error in node.js as well.... so, what @FuzzicalLogic said. :\ – jmar777 Jul 25 '15 at 05:11
  • It's probably worth noting that the `name` property, although it's been around in most engines for ages, wasn't standardized until ES6, so I guess whichever version of V8 is being used in the versions of Node we're testing against haven't implemented the Configurable requirement yet. – jmar777 Jul 25 '15 at 05:12
  • I was previously testing in Node v0.10.38. I just verified that it still doesn't work as desired in v0.12.7, either. – jmar777 Jul 25 '15 at 05:13
-1

The only cross-browser way I have found to do what you are asking is actually very unsafe in many circumstances... So, with care, the answer is below.

 function makeNewFunction(name) {
    return eval('(function(){ return function ' + name + '() {' +
        ' // code as a string' +
        + '}; }) ();');
 }

I had to use something like this awhile ago using a technique I call evilclassing.

Fuzzical Logic
  • 12,947
  • 2
  • 30
  • 58