簡體   English   中英

如何在 NodeJS 中使用 Arrays 創建 class

[英]How can i create a class with Arrays in NodeJS

我有一些 Class 文件用於生成我的請求文檔以向我的后端發出請求。 主 Class 擁有一個數組,而 class 擁有另一個數組。 下面是我的 class 的樣子

class RsaInitial{
    constructor(clientId, subjectName, subjectCredentials, context){
        this.clientId = clientId;
        this.subjectName = subjectName;
        this.subjectCredentials = subjectCredentials;
        this.context = context;
    }
}
class RsaContext{
    constructor(authnAttemptId, messageId, inResponseTo ){
        this.authnAttemptId = authnAttemptId
        this.messageId = messageId
        this.inResponseTo = inResponseTo
    }
}
class RsaSubjectCredentials{
    constructor(methodId, collectedInputs ){
        this.methodId = methodId
        this.collectedInputs = collectedInputs
    }
}
class RsaCollectedInputs{
    constructor(name, value ){
        this.name = name
        this.value = value
    }
}
module.exports = {
  RsaInitial,
  RsaContext,
  RsaCollectedInputs,
  RsaSubjectCredentials
}

因此,當我想創建新實例時,我還必須聲明該項目是一個空數組。 當我基於 class 啟動 object 時,class 文件中是否有一種方法可以將變量聲明為空數組,或者是我創建新的 ZA8CFFDE6698631CBD596699861DZ 時的唯一方法?

這就是我現在的方式..

  let MyRSA = new rsa.RsaInitial
    let MyRSACont = new rsa.RsaContext
    let MyRSASubject = new rsa.RsaSubjectCredentials
    let MyRSACollected = new rsa.RsaCollectedInputs

    MyRSACollected.name = 'testUser'
    MyRSACollected.value = '123456'

    MyRSASubject.collectedInputs = []
    MyRSASubject.methodId = 'SECURID'
    MyRSASubject.collectedInputs.push(MyRSACollected)

    MyRSACont.messageId =  uuidv4()
    MyRSACont.authnAttemptId = ''
    MyRSACont.inResponseTo = ''

    MyRSA.subjectCredentials = []
    MyRSA.clientId = 'crmApi'
    MyRSA.subjectName = 'test'
    MyRSA.subjectCredentials.push(MyRSASubject)
    MyRSA.context = MyRSACont 
  

你叫new錯了。 new應具有以下語法:

new ClassName      // no params
new ClassName()    // no params
new ClassName(...) // params

您的構造函數需要您未傳遞的參數,因此它們都未定義。

這是一個固定版本:

    let MyRSACont = new rsa.RsaContext(uuidv4(), '', '')
    let MyRSACollected = new rsa.RsaCollectedInputs('testUser', '123456')
    let MyRSASubject = new rsa.RsaSubjectCredentials([MyRSACollected], 'SECURID')
    let MyRSA = new rsa.RsaInitial([MyRSASubject], 'crmApi', 'test', MyRSACont)

讓我們說:

class Car {
  constructor(name, year) {
    this.name = name;
    this.year = year;
  }
}

所以,表達式應該是: let myCar1 = new Car("Ford", 2014);


您的代碼應如下所示:

let MyRSACont = new rsa.RsaContext(uuidv4(), '', '')
let MyRSACollected = new rsa.RsaCollectedInputs('testUser', 123456)
let MyRSASubject = new rsa.RsaSubjectCredentials('SECURID', [MyRSACollected])
let MyRSA = new rsa.RsaInitial('crmApi', 'test', [MyRSASubject], [MyRSACont])

快樂編碼:)

實際上,解決方案比我想象的要簡單。 我所要做的就是把我的電話改成這個

class RsaInitial{
    constructor(clientId, subjectName, subjectCredentials, context){
        this.clientId = clientId;
        this.subjectName = subjectName;
        this.subjectCredentials = [];
        this.context = context;
    }

這樣它就被初始化為一個空數組,我可以在不先初始化數組的情況下將對象推入其中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM