0

我对下面的代码有点困惑,我们可以这样在js中创建类吗?

module.exports = function testname(paramas){
  testname.test = function(req, res){
  //some code here
  }
  return testname;
}

我们不应该使用 this.test 代替函数 name.test 吗?

4

1 回答 1

0

ClassName.methodname创建静态成员,同时this.methodname创建实例成员

function MyClass () {
  this.fn = function () {
    // instance member function
  }
}

MyClass.anotherFn = function () {
  // Static member function
}

如果要创建一些在创建类对象时应封装在类对象中的变量,请使用this.var.

但是,如果您希望在同一类的所有实例之间共享数据成员,则使用ClassName.var

于 2013-01-29T12:07:34.560 回答