-3

Is this a smart alternative to creating a object of functions? Benefits? Downsides?

var _ = require("underscore")

function functionName(fn){
  //http://stackoverflow.com/a/17923727/340688
  return /^function\s+([\w\$]+)\s*\(/.exec(fn.toString())[1]
}

function objecfify(arr){
  return _.chain(arr)
    .map(function(fn){
      return [functionName(fn), fn]
    })
    .object()
    .value()
}

var model = objecfify([
  function create(){
    return "create"
  },
  function read(){
    return "read"
  },
  function update(){
    return "update"
  },
  function remove(){
    return "delete"
  }
])

console.log(model)
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
  • Don't parse JS using regexps. –  Jul 20 '15 at 04:08
  • Can you describe the actual problem to be solved? What is the desired output? – jfriend00 Jul 20 '15 at 04:19
  • @jfriend00 just would like to know if this is a valid alternative to writing objects containing functions. – ThomasReggi Jul 20 '15 at 04:29
  • 1
    Seems like something perfect for code review – Downgoat Jul 20 '15 at 04:33
  • 1
    You haven't described any reason or motivation to do something that is not immediately recognizable as to what it's trying to do and certainly not as obvious as just writing a static object with function properties. I guess I have no idea why anyone would ever even attempt this? So, without understanding any benefit to doing it this way and immediately seeing plenty of downsides, I personally would not care to call this a smart alternative. If you share a motivation or benefit to this way or a problem with the traditional way that this solves, that might help. – jfriend00 Jul 20 '15 at 05:00
  • @jfriend00 many javascript style guides frown upon using anon functions. This allows you to write an object without being super redundant and naming both the property and function individually. – ThomasReggi Jul 20 '15 at 05:05
  • Please show me a Javascript style guide that frowns on anonymous functions and offers a decent reason for such. I have never heard that before. FYI, you have an anonymous function in your `objecfify()` function. Honestly, if I saw code like yours I would literally have no idea what it did. It does not pass a readability test for someone who's very familiar with Javascript. It would take some learning and study to figure out what it does. That alone kills it as a notion I'd be interested in when the alternative is immediately recognizable. – jfriend00 Jul 20 '15 at 05:26
  • @jfriend00 Totally makes sense. Here's the [Airbnb guide](https://github.com/airbnb/javascript/tree/master/es5#functions) that has it. They don't give a good reason. – ThomasReggi Jul 20 '15 at 05:29
  • I don't see anywhere in that guide where it says that anonymous functions should be avoided. I'm still not sure where you got that. – jfriend00 Jul 20 '15 at 05:33
  • Apologies, I saw they had the definition and thought that's what people were doing these days. – ThomasReggi Jul 20 '15 at 05:35

1 Answers1

1

Try to use fn.name to get function name:

var functions = [
  function create(){
    return "create"
  },
  function read(){
    return "read"
  },
  function update(){
    return "update"
  },
  function remove(){
    return "delete"
  }
];

var result = functions.map(function(fn) {
  return fn.name;
});

document.write(JSON.stringify(result));
monkey
  • 1,279
  • 14
  • 15