node.js - JavaScript .bind(this) causes function to lose name property - bug/feature? -


in node js app, have callback have scope instantiated function, traditionally use .bind(this) register callback. if have name on function (which necessary) .name property lost , function appears [function] incorrect.

the example code is:

{   let http = require('http');   let server = http.createserver();    function myrequestlistener() {     console.log(this, this.name); // [function], ''   }    server.on('request', myrequestlistener.bind(myfakeclass));   server.listen(1811); } 

without .bind(myfakeclass) name property expected.

to see full code base (my real world problem) can view code here @#38

is bug within js/v8 or feature? can't use arguments.callee.caller.name because i'm using node js harmony features meaning run whole thing in strict. affects versions of node 0.11.7 0.11.12

you're not logging name of function name of context, myfakeclass.

the name of function is copied when build new function using bind can seen simple code (look @ stack trace) :

function a(){    throw "stack";  } a.bind({}).call([]); 

you can check logging arguments.callee.name.

here's fiddle test : http://jsbin.com/nuresaru/1/edit

i checked works same on last chrome, firefox, , on node v0.10.26.


edit : after having seen fiddle : careful can't change name of function setting name property.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -