nodejs:process.stdout.write的岸别名 [英] nodejs: shore alias for process.stdout.write

查看:147
本文介绍了nodejs:process.stdout.write的岸别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习nodejs(我喜欢它!)。我试图找出如何为 console.log 设置更短的别名,我发现我可以使用 var cout = console.log 并使用 cout('[string]')。然后,当我想使用 process.stdout.write 时,我也试图为它创建一个简短的别名,使用 var out = process.stdout。写。但是当我使用 out('[string]')时,我收到以下错误:

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:无法读取未定义的属性'_writableState'

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

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

    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)

在Module._compile(module.js:571:32)

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

在Object.Module._extensions..js(module.js:580:10)
在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)
在运行时(bootstrap_node.js:423:7)

    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)

这里有什么问题?
如何为 process.stdout.write 正确创建短别名?
谢谢

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

推荐答案

你不应该做这种短别名。它非常混乱,阅读代码的人不会理解为什么使用随机函数名而不是 console.log 。但是,如果您真的想要创建函数别名,请考虑使用函数

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.

编辑:
它无法工作的原因在于Node.JS的源代码。您回溯的堆栈跟踪指向行:

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

它试图引用一个名为的变量 _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.

这意味着,指的是进程。 stdout 当你调用 process.stdout.write 时,当你从你的别名中调用它时,它是未定义的。因此,你得到无法读取未定义异常的属性'_writableState'(因为 undefined 不包含该变量,这是对于要执行的函数很重要。

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

这篇关于nodejs:process.stdout.write的岸别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆