Answer the question
In order to leave comments, you need to log in
How to index the functions themselves?
There is a method to which a certain function is passed.
I need to keep track of how many times the same function has been passed to this method. Functions in the method come in a large number and often different ones, but among this flow there are calls to the same function.
MyClass.prototype.method = function(fn){
fn.apply(this, []);
};
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 ){
// Функцию уже вызывали
found=True; break;
}
}
if( !found ) functions_array.push(fn);
fn.apply(this, []);
};
var functions_storage = {};
MyClass.prototype.method = function(fn){
var found = false;
var key = index_function(fn);
if( functions_storage[key] !== undefined ){
// Функцию уже вызывали
found=True;
}
if( !found ) functions_storage[key] = fn;
fn.apply(this, []);
};
Answer the question
In order to leave comments, you need to log in
function MyClass() {
this.fnMap = new WeakMap();
}
MyClass.prototype.method = function(fn) {
if (this.fnMap[fn]) {
return;
}
this.fnMap[fn] = true;
fn.apply(this, []);
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question