我想在模块中导出一个带有静态方法的类,以及其他函数。我正在努力
module.exports = {
fun: function(){},
class: MyClass
}
class MyClass {
static get prop() {
return 'property';
}
}
但它不起作用。有没有办法将类导出为module.exports对象的一部分?
答案 0 :(得分:4)
不会挂起类定义,这意味着在声明这些导出时,您的类不在范围内。将它们向下移动到定义之下。
~ Desktop $ unalias ls
~ Desktop $ unalias l
~ Desktop $ source ~/.bash_aliases
~ Desktop $ l
total 8
drwxr-xr-x 3 astagl staff .
drwxr-xr-x 34 astagl staff ..
-rw-r--r-- 1 astagl staff 2015-06-07_10.50.54.inline editor.html
~ Desktop $ ls
total 8
drwxr-xr-x 3 astagl staff .
drwxr-xr-x 34 astagl staff ..
-rw-r--r-- 1 astagl staff 2015-06-07_10.50.54.inline editor.html
~ Desktop $
您还需要在导出的变量上修复案例。
alias l="\ls -a"
alias ls="\ls -al"
根据您的Javascript环境,如果您尝试使用保留字class MyClass {
static get prop() {
return 'property';
}
}
module.exports = {
fun: function(){},
class: myClass
}
作为文字对象属性,则可能存在编译时错误。您可以将其包装在字符串中以避免这种情况。
module.exports = {
fun: function(){},
class: MyClass
}