1

I need to test whether a given function object belongs to a certain class.

Under Firefox and Chromium/Chrome, I can create a dictionary of function names, and then test whether somefunction.name in class_a_functions using function.name. This does not work in IE.

I can't seem to create a dictionary of function objects to test for a functions containment.

What is a cross-browser way to achieve this that does not involve hacking out the name from the function's toString representation and using that as a function identity. as expressed here?

Community
  • 1
  • 1
ealfonso
  • 6,622
  • 5
  • 39
  • 67
  • IE does allow such constructs... perhaps if you could share the code associated we can better understand the problem. – Anantha Sharma Aug 08 '14 at 04:22
  • name is not available in ie. – Bhojendra Rauniyar Aug 08 '14 at 04:22
  • @AnanthaSharma I am talking about function invocation, but about identity testing of functions and containment in some kind of set. – ealfonso Aug 08 '14 at 04:24
  • possible duplicate: http://stackoverflow.com/questions/6903762/function-name-not-supported-in-ie – Bhojendra Rauniyar Aug 08 '14 at 04:24
  • @C-linkNepal I am aware that name is not available in IE, as I stated in my question. I've also read that question you linked. Instead of working around in order to obtain a function name, I simply need a cross browser way of determining whether a function is in a given set – ealfonso Aug 08 '14 at 04:25
  • that's what dup question has... – Bhojendra Rauniyar Aug 08 '14 at 04:30
  • I specifically don't want to hack the name out of the function. – ealfonso Aug 08 '14 at 04:32
  • @Felix King did you bother to read the question before marking it as duplicate? – ealfonso Aug 08 '14 at 04:34
  • If you don't want to extract the function name from the source, then you can simply have an array of functions, and iterate over the array to see whether it contains a given function (or use `indexOf` if available). And if your comment was reference to the bold text in your question, I hope you are aware that you added that part after I closed the question. – Felix Kling Aug 08 '14 at 05:18
  • The question wasn't a duplicate and you should be more careful with your moderator authority – ealfonso Aug 08 '14 at 05:20
  • 1
    Don't take it personal. – Felix Kling Aug 08 '14 at 05:23

1 Answers1

0

You can apply the same procedure as for other object/values: Iterate over the set and compare each value against a given value.

Using an object as set:

var haystack = {...};
var needle = someFunction;
var isContained = false;
for (var key in haystack) {
  if (needle === haystack[key]) {
    isContained = true;
    break;
  }   
}

Using an array as set:

var haystack = [...];
var needle = someFunction;
var isContained = false;
for (var i = 0; i < haystack.length; i++) {
  if (needle === haystack[i]) {
    isContained = true;
    break;
  }   
}

// if indexOf is available:
var isContained = haystack.indexOf(needle) > -1;
// if some is available
var isContained = haystack.some(function(v) { return v === needle; });

Related:

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143