简体   繁体   中英

Using “this” in a function in Node.js

I'm trying to get access to this in a function, but it's undefined.

function processEachPath(element, index, list) {
    logger.debug(this);

}

...

_.each(config, processEachPath);

You need to explicitly bind it to the function:

function processEachPath(element, index, list) {
    logger.debug(_this);
}

// ...

(processEachPath.bind(this))();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM