Compare functions in Javascript -


i have api takes function input, , inside api, intent add function array if function not added array.

the call api of form:

myapihandle.addifunique(function(){     myresource.get(myobj); }); 

the api is:

myapihandle.addifunique(myfunc) {    if (myarray.indexof(myfunc) === -1) {        return;    } // add array } 

now not work expected, since each time new function being passed in.

my question is: there way pass in function myapihandle.addifunique call allow me compare existing functions in array function passed in? comparison should compare function name , object, , if both same, not add function array. want avoid adding argument addifunique call if @ possible.

in other words, below possible:

myapicall.addifunique (somefunc) { } 

if so, somefunc. , logic inside api detect if function exists in myarray?

the same problem occurs addeventlistener , removeeventlistener, callback must identical (in === sense) removeeventlistener remove it.

as you've found, if call addifunique this:

addifunique(function() { }) 

the function passed each time unique object. solution create function once:

var fn = function() { }; addifunique(fn); addifunique(fn); 

a related problem occurs when function being passed in method invocation, need bind it:

var x = { val: 42, method: function() { console.log(this.val); } }; 

i want pass bound version of it, so

addifunique(x.method.bind(x)); addifunique(x.method.bind(x)); 

but again, each call x.method.bind(x) return separate function. need pre-bind:

var boundmethod = x.method.bind(x); addifunique(boundmethod); addifunique(boundmethod); 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo