Tag: es6 class

如何使用ES6 super在子构造函数中调用父方法?

情况: 我使用其他属性(timestamp,id)来扩展Node.js(v.8.4.0)Error对象,然后扩展这个对象以获得更精细的error handling。 class MyError extends Error { constructor (msg) { super(msg); this.id = uuid(); this.timestamp = Date.now(); // I reckon this can be replaced by this.init(this) ? this.name = this.constructor.name; Error.captureStackTrace && Error.captureStackTrace(this, this.constructor); } init (self) { self.name = self.constructor.name; Error.captureStackTrace && Error.captureStackTrace(self, self.constructor); } } 我希望能够在子错误中不重复Error.captureStackTrace和this.name调用。 所以我创build了一个我在孩子中使用的init函数: class GranularError extends MyError { […]

有没有一种方法在es6inheritance自错误,并没有堆栈跟踪中的构造函数?

我试图通过扩展Error来编写一个HTTPError类: class HTTPError extends Error { constructor(codeArg, message){ let code = codeArg || 500; super(message || http.STATUS_CODES[code]); // first line in stack trace this.code = code; } } 这大部分工作正常,但是当我throw这样的错误时, super被调用的行是堆栈跟踪中的第一行(假设nodejs): > const HTTPError = require('./HTTPError') undefined > let e = new HTTPError(418) undefined > throw e Error: I'm a teapot at HTTPError (/home/pat/Scripts/js/HTTPError.js:6:6) at repl:1:9 […]

JavaScript如何从超类获得子类“__dirname”

我想在父类中创build一个方法,它将返回扩展它的每个子类的位置。 // Class A dir is '/classes/a/ class A{ getPath(){ console.log(__dirname); return (__dirname); } } // Class B dir is '/classes/b' class B extends A{ } new A().getPath(); // Prints '/classes/a' new B().getPath(); // Prints '/classes/a' (I want here '/classes/b'); 我明白为什么B类打印A的位置,但我正在寻找一个选项,使父类的方法,将打印子类的位置。 我不想重写每个子类中的方法,因为它忽略了这一点 我也尝试process.pwd() 。

在JavaScript中,特别是NodeJS上的ES6,我可以直接在运行时操作类的getter和setter函数吗?

可以说我有一个类定义如下: class MyClass { constructor(a, b) { this._a = a; this._b = b; } get a() { return this._a; } set a(val) { this._a = val; } add() { return this._a + this._b; } } 我希望能够直接在运行时访问和操作getter和setter函数,以便将它们包含在其他debugging代码中。 使用“添加”function,我可以这样做: let oldAdd = MyClass.prototype.add; MyClass.prototype.add = function() { console.log('add called!'); let result = oldAdd.call(this); console.log('add result: ' + […]

获得一个类中的所有静态获得者

比方说,我有这个类(我使用像一个枚举): class Color { static get Red() { return 0; } static get Black() { return 1; } } 有没有类似于Object.keys来获得['Red', 'Black'] ? 我正在使用Node.js v6.5.0,这意味着一些function可能会丢失。

ECMAScript 6类属性下划线前缀

我所见过的类模式很像这样: class Foo { constructor(x, y, z) { this._x = x; this._y = y; this._z = z; } get x() { return this._x; } set x(value) { //I acctually do some stuff here this._x = value; } get y() { return this._y; } set y(value) { //I acctually do some stuff here this._y = value; […]

在Node.js的ES6中定义它的类中不能调用一个方法

我正在使用Node.js,Express.js和MongoDB制作应用程序。 我正在使用MVC模式,也有单独的路由文件。 我想我做一个控制器类,其中一个方法调用其中声明的另一个方法。 但我似乎无法做到这一点。 我得到“无法读取未定义的属性”。 index.js文件 let express = require('express'); let app = express(); let productController = require('../controllers/ProductController'); app.post('/product', productController.create); http.createServer(app).listen('3000'); ProductController.js文件 class ProductController { constructor(){} create(){ console.log('Checking if the following logs:'); this.callme(); } callme(){ console.log('yes'); } } module.exports = new ProductController(); 当我运行这个我得到以下错误信息: Cannot read property 'callme' of undefined 我已经自己运行这个代码,稍作修改,如下所示。 class ProductController { constructor(){} create(){ […]

如何克隆一个JavaScript的ES6类实例

如何使用ES6克隆Javascript类实例。 我对基于jquery或$ extend的解决scheme不感兴趣。 我曾经见过很多关于对象克隆的讨论,认为这个问题相当复杂,但是用ES6,一个非常简单的解决scheme就出现了 – 我会把它放在下面,看看人们是否认为它是令人满意的。 编辑:这是build议我的问题是重复的; 我看到了这个答案,但是它已经7岁了,并且使用ES6之前的版本来解释非常复杂的答案。 我build议我的问题,它允许ES6,有一个更简单的解决scheme。