[英]Extend class to a multiple class instances Javascript
I have UserModel
class which has static methods, and I want to extend or inherit to multiple class instances.我有UserModel
class,它有 static 个方法,我想扩展或继承到多个 class 实例。
UserModel.js用户模型.js
const tableName = "Users";
const primaryKey = "UserID";
const secondaryTables = [{ id: "RoleID", name: "Roles", relation: " INNER JOIN " }];
const getterModel = new GettersModel(tableName, primaryKey, secondaryTables);
const deleteModel = new DeleteModel(tableName, primaryKey);
class UserModel {
constructor() {
}
static async insertUser({ username, password, roleId }) {
...
}
...
}
Object.setPrototypeOf(UserModel, getterModel);
Object.setPrototypeOf(UserModel, deleteModel);
DeleteModel.js删除模型.js
class DeleteModel {
constructor(tableName, primaryKey) {
this.tableName = tableName;
this.primaryKey = primaryKey;
}
async deleteRow(id) {
try {
const result = await mysql_conn.delete(this.tableName, `where
${this.primaryKey}=?`, [id]);
return result;
} catch (err) {
console.error(err);
return false;
}
}
}
UserService.js用户服务.js
async function deleteUser(userID) {
let ret = await UserModel.deleteRow(userID);
if (ret == false) {
return { status: false }
} else {
return { status: true }
}
}
But the problem is GettersModel
gets replaced by the prototype of DeleteModel
when setting this:但问题是GettersModel
在设置时被DeleteModel
的原型所取代:
Object.setPrototypeOf(UserModel, getterModel);
Object.setPrototypeOf(UserModel, deleteModel);
And also, there's a warning in MDN website about .setPrototypeOf()
:而且,MDN 网站上有一条关于.setPrototypeOf()
的警告:
Warning : Changing the [[Prototype]] of an object is, by the nature of how modern JavaScript engines optimize property accesses, currently a very slow operation in every browser and JavaScript engine .警告:根据现代 JavaScript 引擎如何优化属性访问的本质,更改 object 的 [[Prototype]]目前在每个浏览器和 JavaScript 引擎中都是非常缓慢的操作。 In addition, the effects of altering inheritance are subtle and far-flung, and are not limited to simply the time spent in the Object.setPrototypeOf(...) statement, but may extend to any code that has access to any object whose [[Prototype]] has been altered.此外,改变 inheritance 的影响是微妙而广泛的,并且不仅仅限于在 Object.setPrototypeOf(...) 语句中花费的时间,而是可以扩展到可以访问任何 object 的任何代码 [ [原型]] 已更改。
Because this feature is a part of the language, it is still the burden on engine developers to implement that feature performantly (ideally).因为此功能是语言的一部分,所以引擎开发人员仍然有责任以高性能(理想情况下)实现该功能。 Until engine developers address this issue, if you are concerned about performance, you should avoid setting the [[Prototype]] of an object. Instead, create a new object with the desired [[Prototype]] using Object.create().在引擎开发人员解决此问题之前,如果您担心性能,则应避免设置 object 的 [[Prototype]]。相反,使用 Object.create() 创建一个具有所需 [[Prototype]] 的新 object。
So how can I do this?那我该怎么做呢? Thanks谢谢
You should not use inheritance here.你不应该在这里使用 inheritance。 The UserModel
class should use the deleteModel
instance. UserModel
class 应该使用deleteModel
实例。
To expose UserModel.deleteRow
, you can use要公开UserModel.deleteRow
,您可以使用
class UserModel {
constructor() {…}
static async insertUser(…) { … }
static deleteRow = deleteModel.deleteRow.bind(deleteModel)
}
or或者
class UserModel {
constructor() {…}
static async insertUser(…) { … }
}
UserModel.deleteRow = deleteModel.deleteRow.bind(deleteModel);
However, I would suggest that you should not expose a deleteRow
method on your UserModel
- what is a row?但是,我建议您不要在UserModel
上公开deleteRow
方法 - 什么是行? You should instead write你应该改为写
class UserModel {
constructor() {…}
static async insertUser(…) { … }
static async deleteUser(userID) {
return {status: await deleteModel.deleteRow(userID)};
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.