简体   繁体   English

Javascript - 将类实例保存到哈希表/对象中并使用关键实例函数

[英]Javascript - Saving class instances into hashtable/object and using key instance function

I have 2 classes Key and Session that output unique "this.session/this.key" values on every class instance:我有 2 个 Key 和 Session 类,它们在每个类实例上输出唯一的“this.session/this.key”值:

//self executing function or IIFE(Immediately Invoked Function Expression) 
//classes that return unique key and sessions on every instance

const Session = (() => {
    let lastSession = 80
    return class Session{
        constructor(){
            this.session = lastSession
            lastSession++
        }

        printSession(){
            console.log(this.session)
        }
    }
})()

const Key = (() => {
    let lastKey = 0

    return class Key{
        constructor(){
            this.key = lastKey
            lastKey++
        }

        printKey(){
            console.log(this.key)
        }
    }
})()

I have a third class Manager that adds {Key:Session} instance pairs to a Object/Hashtable:我有一个第三类管理器,它将 {Key:Session} 实例对添加到对象/哈希表中:

class Manager{
    constructor(){
        this.allSessions = {}
    }

    addSession(){
        this.allSessions[new Key] = new Session
    }

    printSessions(){
        for(let key in this.allSessions){
            console.log(`${key} : ${this.allSessions[key]}`)
            //key.printSession()
            //console.log(key.key)
            //this.allSessions[key].printSession()

        }
    }

}

There are 2 problems:有2个问题:

1)The hashtable/object only has 1 {Key:Session} pair at any given time. 1)哈希表/对象在任何给定时间只有 1 个 {Key:Session} 对。 No matter how many addSession() calls I make.无论我进行了多少次 addSession() 调用。 Is this because it treats all the Key instances as the same?这是因为它将所有 Key 实例视为相同吗? How do I fix this.我该如何解决。

2)In the printSessions() function within Manager class, I can't seem to print out the key using key.printKey() that I defined in the key class? 2) 在 Manager 类中的 printSessions() 函数中,我似乎无法使用我在密钥类中定义的 key.printKey() 打印出密钥? Why is that?这是为什么? I also tried console.log(key.key) which also did not work.我也试过 console.log(key.key) 也没有用。

You're trying to use instances of the Key class as object property names.您正在尝试使用Key类的实例作为对象属性名称。 That doesn't work;那行不通; property names are always strings, so the objects (the Key objects) are converted to strings implicitly.属性名称始终是字符串,因此对象( Key对象)会隐式转换为字符串。 That always results in the string "[object Object]".这总是导致字符串“[object Object]”。

You can instead try你可以试试

    this.allSessions[new Key().key] = new Session

Plain JavaScript objects indeed seem like hash tables, but the keys can only be strings.普通的 JavaScript 对象确实看起来像哈希表,但键只能是字符串。 You might want to explore the Map type.您可能想要探索 Map 类型。

The fact that you call the instance key and that instance also has a property called key is maybe the reason you made this error.您调用实例key并且该实例还有一个名为key的属性这一事实可能是您犯此错误的原因。

Also, printing this.allSessions[key] is not really helpful as that is an object.此外,打印this.allSessions[key]并不是很有帮助,因为它是一个对象。 Maybe you intended: this.allSessions[key].session :也许你打算: this.allSessions[key].session

class Manager{
    constructor(){
        this.allSessions = {}
    }

    addSession(){
        this.allSessions[(new Key).key] = new Session
    }

    printSessions(){
        for(let key in this.allSessions){
            console.log(`${key} : ${this.allSessions[key].session}`)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM