有多少方法可以实例化一个新的可读流对象?

时间:2014-02-04 03:39:21

标签: javascript node.js stream

我已经看到很多在Node.js中实例化可读流的方式,我有点困惑。

var Readable = require('stream').Readable;
var rs1 = Readable(); // the first way
var rs2 = new Readable; // the second way
var rs3 = new Readable(); // the third way

它们完全一样吗?如果没有,有什么区别?

1 个答案:

答案 0 :(得分:2)

对于stream.Readable,它们都是等效的。

当被称为function(没有new)时,new实例will be created anyways

function Readable(options) {
  if (!(this instanceof Readable))
    return new Readable(options);

  // ...
}

并且,当作为构造函数(使用new)调用时,parenthesis are simply optional在没有给出参数的情况下。

  句法
new constructor[([arguments])]
相关问题