如果我理解您的问题,那么是的。
function MyClass(options) {
// something like a constructor in java
this.myProperty = 'foo'; // instance property
}
MyClass.prototype.doSometing(what)
// this is an instance method (because of prototype, this is generated for each instance, can have a reference to `this`)
}
MyClass.doSomethingElse(andWhat)
// this is a static method, which is not available for an instantiated object (only via MyClass.doSometingElse();)
}
MyClass.staticProperty = 'bar'; // static property;
module.exports = MyClass;
我希望这是您想知道的。它们都是公开的。不可能创建私有静态属性,因为这些属性始终是公共的。此外,私有实例属性通常不是私有的或不是不动产。它只是一个局部变量,只能在定义它的函数内访问。如果您可以通过this 访问它,那么它也是公开的。
题外话:在导出此模块之前,您可以创建一个实例并导出该实例。这将在 Java 中创建类似单例的东西。所以每当你请求这个模块时,你都会得到相同的实例:
MyClass = new MyClass();
module.exports = MyClass;
但这将删除所有静态引用,因为在这种情况下 MyClass 将是一个实例。