使用 ES6 Class 语法,如何从其他文件实现 class 方法?
Using ES6 Class syntax, how to implement class methods from other files?
A class 我正在用 NodeJS v8.10(内置 Webpack)编写,看起来它会变得非常大。我想将这些方法分解到它们自己的文件中,但我也想保留 ES6 Class 语法,因为我来自 OOP 背景。
是否有更好的 ES6 语法来从其他文件中实现 class 的方法?
我目前正在根据下面的代码扩展原型,但是最好将所有内容 包含在 class 大括号“{}”中。
const fnClose = require('./close');
// about 20 more methods required in here
class Door {
constructor() {}
// close: require('./close'); // this would be nice!
}
/*
it doesn't seem to matter if the exports line comes
BEFORE the prototype extensions; it still exports the
'close' method with the Door class.
*/
// module.exports = Door; // works, just looks wrong here
Door.prototype.close = fnClose;
// about 20 more methods added to class here
module.exports = Door; // the natural place for exports
更新
根据 Oliver 在他下面的回答中提供的火花,可以重构此代码以引入方法 "inside the braces" 像这样。这并不像我希望的那样 "ES6";更简洁的语法会很好。但这确实完成了工作!
const fnClose = require('./close');
// about 20 more methods required in here
class Door {
constructor(...args) {
// PROPERTIES
this.species = 'Oak';
// METHODS - FROM THEIR OWN FILES!
this.close = fnClose; // how to close the door
// CONSTRUCTOR CODE
// <do stuff with args>
}
}
module.exports = Door;
/*
And thats it. everything tucked inside the
class, no need for prototype extenstions.
Does not appear to be a need for Babel though.
*/
正如 James Thorpe 指出的那样,可能是您的 class 本身变得太大了。话虽这么说,如果你使用的是 babel,那么你可以 class 字段来实现至少在我看来会达到相同效果的东西:
function test() {
console.log('called test')
console.log(this.value)
}
class TestClass {
value = "This is the test value";
runTest = test;
}
const x = new TestClass();
x.runTest()
没有 babel,你不能使用 class 变量,因为它们在 js 中还不被支持。 There is a proposal 在撰写本文时处于第 3 阶段,babel 可以为我们转译它。
上面的代码片段使用 babel 来让事情正常进行。您在评论中询问 babel 是否只是将其转换为与您拥有的代码相同的代码。它很相似,但在几个关键方面有所不同。 Babel 将它转译成这个(使用他们的沙箱):
"use strict";
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function test() {
console.log('called test');
console.log(this.value);
}
var TestClass = function TestClass() {
_classCallCheck(this, TestClass);
this.value = "This is the test value";
this.runTest = test;
};
var x = new TestClass();
x.runTest();
所以它根本没有使用 class 语法。请记住 javascript 中的 class
在任何情况下都只是语法糖,因此当您在任何情况下使用 class
时,在幕后都会发生与此类似的事情。
Babel 似乎确实需要一个插件,可以找到详细信息here.
A class 我正在用 NodeJS v8.10(内置 Webpack)编写,看起来它会变得非常大。我想将这些方法分解到它们自己的文件中,但我也想保留 ES6 Class 语法,因为我来自 OOP 背景。
是否有更好的 ES6 语法来从其他文件中实现 class 的方法?
我目前正在根据下面的代码扩展原型,但是最好将所有内容 包含在 class 大括号“{}”中。
const fnClose = require('./close');
// about 20 more methods required in here
class Door {
constructor() {}
// close: require('./close'); // this would be nice!
}
/*
it doesn't seem to matter if the exports line comes
BEFORE the prototype extensions; it still exports the
'close' method with the Door class.
*/
// module.exports = Door; // works, just looks wrong here
Door.prototype.close = fnClose;
// about 20 more methods added to class here
module.exports = Door; // the natural place for exports
更新
根据 Oliver 在他下面的回答中提供的火花,可以重构此代码以引入方法 "inside the braces" 像这样。这并不像我希望的那样 "ES6";更简洁的语法会很好。但这确实完成了工作!
const fnClose = require('./close');
// about 20 more methods required in here
class Door {
constructor(...args) {
// PROPERTIES
this.species = 'Oak';
// METHODS - FROM THEIR OWN FILES!
this.close = fnClose; // how to close the door
// CONSTRUCTOR CODE
// <do stuff with args>
}
}
module.exports = Door;
/*
And thats it. everything tucked inside the
class, no need for prototype extenstions.
Does not appear to be a need for Babel though.
*/
正如 James Thorpe 指出的那样,可能是您的 class 本身变得太大了。话虽这么说,如果你使用的是 babel,那么你可以 class 字段来实现至少在我看来会达到相同效果的东西:
function test() {
console.log('called test')
console.log(this.value)
}
class TestClass {
value = "This is the test value";
runTest = test;
}
const x = new TestClass();
x.runTest()
没有 babel,你不能使用 class 变量,因为它们在 js 中还不被支持。 There is a proposal 在撰写本文时处于第 3 阶段,babel 可以为我们转译它。
上面的代码片段使用 babel 来让事情正常进行。您在评论中询问 babel 是否只是将其转换为与您拥有的代码相同的代码。它很相似,但在几个关键方面有所不同。 Babel 将它转译成这个(使用他们的沙箱):
"use strict";
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function test() {
console.log('called test');
console.log(this.value);
}
var TestClass = function TestClass() {
_classCallCheck(this, TestClass);
this.value = "This is the test value";
this.runTest = test;
};
var x = new TestClass();
x.runTest();
所以它根本没有使用 class 语法。请记住 javascript 中的 class
在任何情况下都只是语法糖,因此当您在任何情况下使用 class
时,在幕后都会发生与此类似的事情。
Babel 似乎确实需要一个插件,可以找到详细信息here.