最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - nodejs: short alias for process.stdout.write - Stack Overflow

programmeradmin6浏览0评论

I'm learning nodejs (and I like it!). I tried to figure out how to have shorter alias for console.log and I found out that I can use var cout=console.log and use cout('[string]') from then on. Then when I wanted to use process.stdout.write and I tried to make a short alias for it too, using var out=process.stdout.write. But when I use out('[string]'), I get the following error:

_stream_writable.js:220   var state = this._writableState;                   ^   TypeError: Cannot read property '_writableState' of undefined

    at Writable.write (_stream_writable.js:220:19)     at Socket.write (net.js:670:40)     at Object. (/home/shayan/Desktop/nodejs/server.js:12:1)

    at Module._pile (module.js:571:32)

    at Object.Module._extensions..js (module.js:580:10)     at Module.load (module.js:488:32)     at tryModuleLoad (module.js:447:12)     at Function.Module._load (module.js:439:3)     at Module.runMain (module.js:605:10)     at run (bootstrap_node.js:423:7)

What is wrong here? How can I correctly create a short alias for process.stdout.write? Thanks

I'm learning nodejs (and I like it!). I tried to figure out how to have shorter alias for console.log and I found out that I can use var cout=console.log and use cout('[string]') from then on. Then when I wanted to use process.stdout.write and I tried to make a short alias for it too, using var out=process.stdout.write. But when I use out('[string]'), I get the following error:

_stream_writable.js:220   var state = this._writableState;                   ^   TypeError: Cannot read property '_writableState' of undefined

    at Writable.write (_stream_writable.js:220:19)     at Socket.write (net.js:670:40)     at Object. (/home/shayan/Desktop/nodejs/server.js:12:1)

    at Module._pile (module.js:571:32)

    at Object.Module._extensions..js (module.js:580:10)     at Module.load (module.js:488:32)     at tryModuleLoad (module.js:447:12)     at Function.Module._load (module.js:439:3)     at Module.runMain (module.js:605:10)     at run (bootstrap_node.js:423:7)

What is wrong here? How can I correctly create a short alias for process.stdout.write? Thanks

Share Improve this question edited Dec 8, 2019 at 19:28 Charles Merriam 20.5k7 gold badges75 silver badges84 bronze badges asked Apr 12, 2017 at 6:46 massivefermionmassivefermion 6751 gold badge10 silver badges24 bronze badges 1
  • This is an underrated question – AturSams Commented Mar 5, 2021 at 11:04
Add a ment  | 

2 Answers 2

Reset to default 13

You should not do this kind of "short alias". It's very messy and people reading your code won't understand why you use random function names instead of console.log. However, if you really want to create function aliases, consider using a function:

function out(text) {
    //    ^    ^- argument accepted by the function
    //    |------ the function name
    process.stdout.write(text)
    //                     ^- pass the argument you accepted in your new function to the long function
}

I added some explanation in case you don't know how a function works, you can safely remove it.

Edit: The reason why it's not working is in the source code of Node.JS. The stacktrace you are getting back points to this line:

Writable.prototype.write = function(chunk, encoding, cb) {
    var state = this._writableState;
    // ...
}

It tries to reference a variable called _writableState from this. As written here:

Inside a function, the value of this depends on how the function is called.

This means, that this refers to process.stdout when you call process.stdout.write, however it is undefined, when you call it from your alias. Therefore you get a Cannot read property '_writableState' of undefined exception (as undefined does not contain that variable, which is important for the write function to execute).

Aside from a function declaration you can also use Function.prototype.bind:

const out = process.stdout.write.bind(process.stdout);
out('foo');

bind returns a new function with the context (this) bound to whatever value you pass. https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind

发布评论

评论列表(0)

  1. 暂无评论