[英]ES6 class method not present in instance
I am updating my old node.js project to include ES6 features in order to eventually migrate to typescript, but I can't get this class to work. 我正在更新旧的node.js项目以包括ES6功能,以便最终迁移到打字稿,但是我无法使此类工作。 I have defined a class in a separate file:
我在单独的文件中定义了一个类:
class Database {
constructor(connectionPool) {
this._connectionPool = connectionPool;
}
_getConnection () {
console.log("getting connection");
return new Promise((resolve, reject) => {
this._connectionPool.getConnection((err, connection) => {
if (err) {
reject(err);
console.log("connection rejected");
} else {
console.log("connection resolved");
resolve(connection);
}
});
});
}
}
module.exports = Database;
I am then importing this class into another file and trying to create a Database instance: 然后,我将此类导入另一个文件并尝试创建一个数据库实例:
const mysql = require('mysql');
const Database = require('./database');
module.exports = databaseInitializer;
function databaseInitializer(config) {
const pool = mysql.createPool(config);
const db = new Database(pool);
console.log(Database.toString());
console.log(db.__proto__);
return db;
}
Finally, i am using above function to get the database instance: 最后,我正在使用上述功能来获取数据库实例:
const database = require('./models/databaseInitializer')(mysqlConfig);
console.log(database);
Now, my database instance has a _connectionPool
property, but no prototype and more importantly, no _getConnection
property. 现在,我的数据库实例具有
_connectionPool
属性,但是没有原型,更重要的是,没有_getConnection
属性。 What am I missing? 我想念什么?
Thanks and best regards 谢谢和最好的问候
_getConnection
property belongs to the prototype of database
and hence this property is the non-enumerable property of database
. _getConnection
属性属于的原型database
, 因此这个属性是的不可枚举属性database
。
You can access this property by doing 您可以通过以下方式访问此属性
console.log(database._getConnection) //should show the function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.