Test if variable is a specific bound function in using javascript's Function.prototype.bind() -


i trying work out how test if variable instance of specific bound function. consider following example:

var func = function( arg ) {     // code here }  myfunc = func.bind( null, 'val' );  if( myfunc == func ) {     console.log( true ); } else {     console.log( false ); } 

unfortunately results in false. there sort of way of testing variable find out function bound to?

no, there not way this. .bind() returns new function internally calls original one. there no interface on new function retrieve original one.

per ecmascript specification 15.3.4.5, returned "bound" function have internal properties [[targetfunction]], [[boundthis]] , [[boundargs]], properties not public.

if tell higher level problem you're trying solve, might able come different type of solution.


if control .bind() operation, put original function on bound function property , test property:

var func = function( arg ) {     // code here }  myfunc = func.bind( null, 'val' ); myfunc.origfn = func;  if( myfunc === func || myfunc.origfn === func) {     console.log( true ); } else {     console.log( false ); } 

demo: http://jsfiddle.net/jfriend00/e2gq6n8y/

you make own .bind() replacement did automatically.

function bind2(fn) {     // make copy of args , remove fn argument     var args = array.prototype.slice.call(arguments, 1);     var b = fn.bind.apply(fn, args);     b.origfn = fn;     return b; } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -