我有一個在Node.js應用程序中使用的外部庫(Objection.js)。我創建了一個基礎模型類,Model為我的實體模型擴展了Objection的類:const { Model } = require('objection')class ModelBase extends Model { // implementation not important for this question}在擴展基礎的模型類中,有時,尤其是在對進行編碼時relationMappings,我必須訪問Model基礎類上的屬性/枚舉。我可以在這樣的擴展模型中做到這一點:const ModelBase = require('./model-base')class SomeModel extends ModelBase { static get relationMappings () { const SomeOtherModel = require('./some-other-model') return { someOtherModel: { relation: ModelBase.BelongsToOneRelation, modelClass: SomeOtherModel, // etc. } } }}注意這一relation: ModelBase.BelongsToOneRelation行。這行得通,但我認為這具有誤導性,因為BelongsToOneRelation它不是的成員ModelBase。在我看來,更明確,更正確的方法是Model從Objection導入/請求from,以便我可以BelongsToOneRelation從那里訪問該對象,例如:const { Model } = require('objection')// other code just like above until the relationMappings... relation: Model.BelongsToOneRelation我更喜歡這種方法。如果導入/需要繼承鏈中已經存在的類,是否會引起問題,例如require循環或循環依賴的JavaScript版本?
要求/導入在繼承鏈上端已經導入的類是錯誤的嗎?
牛魔王的故事
2021-04-23 10:11:35