0

I'm in a situation where I need to sort an array of objects by an attribute of the object, that attribute being a function reference. To do that, the simplest way was to do use the function reference as an object key like this:

function funky() {}

var someObject = {};
var key = {"func":funky, "some_other_stuff":5};
someObject[key.func] = key;

and it seems to work fine in the latest FF and chrome. Question is though - is this standard behavior? Can I expect this to keep working?

user81993
  • 6,167
  • 6
  • 32
  • 64
  • Interesting. Why would you need to sort on function references? They essentially just pointers. – Wainage May 15 '16 at 22:32
  • Consider what happens when you do `funky.toString()`, i.e. `o = {}; o[function () {}] = 1; o; // Object {"function () {}": 1}` – Paul S. May 15 '16 at 22:33
  • 1
    It "seems to work fine" because the functions will be converted to strings. If you try it with a built–in or host function, where *toString* return something like `function getElementById() { [native code] }`, things will be a little different. – RobG May 15 '16 at 22:33
  • In modern browsers you can use arbitrarily objects as keys in Map instances. You cannot use objects as property names however. The runtime will coerce your object reference to a string by calling the `.toString()` method on it. Whatever that returns will be the actual property name. – Pointy May 15 '16 at 22:35
  • @Wainage Its for a framework that aims to automate filling HTML based on data models. This particular bit is about allowing for data interpreting functions and error functions that need to be flattened in a certain way and can't have duplicates. Looping them into an object is a 2 for 1 solution! – user81993 May 15 '16 at 22:45
  • If you'd explain more about what you're actually trying to accomplish, we could help you better. Sorting by a function reference seems a bit odd and hard to understand what you're accomplishing. How is one function greater or less than another function? If you're trying to remove or detect duplicates, you can use an ES6 `Map` or `Set` which will work with a function as a key. – jfriend00 May 15 '16 at 22:56
  • @jfriend00 they are greater or lesser by their calling object position in the hierarchy but that is irrelevant to the question since the answer is also useful information by its lonesome. – user81993 May 15 '16 at 23:00
  • Then, just add a property to the function that represents their position and sort by that property. I still don't understand what you're trying to do. Explaining the real problem you're trying to solve rather than explain your attempt at a solution gives you a lot fewer answer possibilities and eliminates us providing a much better answer than you've even thought of. Basic rule here. Best answers come when you describe the actual problem, not just an issue with your attempted solution. – jfriend00 May 15 '16 at 23:05
  • @jfriend00 I don't have any trouble with solving the problem, I know there is a bunch of ways to do it. I'm just trying to assess the viability of this particular solution. – user81993 May 15 '16 at 23:14
  • You don't show us any sorting code so I don't know how we can assess viability of your sorting. And, you don't describe what the objective of the sort is either so we could assess whether you're meeting that reliably in different browsers, – jfriend00 May 15 '16 at 23:15
  • @jfriend00 That's because the question wasn't about the sorting or how it looks to anybody else but this specific practice. – user81993 May 15 '16 at 23:19
  • Well, there are no such things as non-strings as object keys in Javascript (except for symbols in ES6). Everything you try to convert to a key will be auto-converted to a string. Still not really sure what you're asking. A function auto-conversion to a string is generally not a smart practice and is something I would reject in a code review with my team. – jfriend00 May 16 '16 at 01:04

0 Answers0