How to index the functions themselves?

There is a method that receives a function.

I need to track how many times this method was transferred one and the same function. Functions in a method are received in large numbers and often different, but among this thread there are calls to the same function.

MyClass.prototype.method = function(fn){
 fn.apply(this, []);
};


You can add all the features in a regular array, and each call to run it entirely, comparing the functions, but it uzhos and brakes:

var functions_array = [];
MyClass.prototype.method = function(fn){
 var found = false;
 for(var i=0; i<functions_array.length; i++){ if( functions_array[i]="==" fn ){ Function already caused found="True;" break; } !found ) functions_array.push(fn); fn.apply(this, []); };< code></functions_array.length;>
br><br> As you can probably somehow to index the function and generate a key?<br><br><pre><code class="javascript">var functions_storage = {}; MyClass.prototype.method = function(fn){ var found = false; var key = index_function(fn); if( functions_storage[key] !== undefined ){ // Function called found=True; } if( !found ) functions_storage[key] = fn; fn.apply(this, []); };</code></pre><br><br> Here is how I create a `key` for the function to have accurately identified this feature and were caught <b>it</b> subsequent calls?
June 8th 19 at 16:52
1 answer
June 8th 19 at 16:54
Solution
function MyClass() {
 this.fnMap = new WeakMap();
}

MyClass.prototype.method = function(fn) {
 if (this.fnMap[fn]) {
return;
}
 this.fnMap[fn] = true;
 fn.apply(this, []);
};
Wow! Well, thank you! Did not know about this :) - keagan_Schuster commented on June 8th 19 at 16:57

Find more questions by tags JavaScript