Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs: short alias for process.stdout.write

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._compile (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

like image 303
massivefermion Avatar asked Apr 12 '17 06:04

massivefermion


2 Answers

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).

like image 100
Benni Avatar answered Sep 23 '22 02:09

Benni


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.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind

like image 28
anthonygood Avatar answered Sep 23 '22 02:09

anthonygood