node.js - Passing a function as parameter in javascript, undefined error -


i have function part of object called server, i.e server.log function use log data. there other properties of server not want pass other functions, though want server.log available functions in other files.

function test() {     testingthis(server.log); }  function testingthis(logf) {     logf("test123"); } 

i error saying

cannot read property 'emit' of undefined

i using happy console module log (server.log works fine in test function).

presumably server.log expects this refer server. however, way call function, this refers global object or undefined (in strict mode).

bind function server:

testingthis(server.log.bind(server)); 

see how access correct `this` context inside callback?.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -